Skip to content

Commit

Permalink
Allow to create keypairs from private keys, conversion tweaks (#507)
Browse files Browse the repository at this point in the history
* Allow to create keypairs from private keys, conversion tweaks

* More tests

* Tweak

* Improvements

* Naming

* Comments

* Add `dock` method

* Update DID mapping
  • Loading branch information
olegnn authored Jan 23, 2025
1 parent 99ae5cd commit cfc66a8
Show file tree
Hide file tree
Showing 28 changed files with 6,620 additions and 3,195 deletions.
13 changes: 13 additions & 0 deletions examples/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# @docknetwork/sdk-examples

## 0.11.0

### Minor Changes

- Allow to create keypairs from private keys, conversion tweaks

### Patch Changes

- Updated dependencies
- @docknetwork/dock-blockchain-modules@0.17.0
- @docknetwork/dock-blockchain-api@0.13.0
- @docknetwork/credential-sdk@0.28.0

## 0.10.0

### Minor Changes
Expand Down
8 changes: 4 additions & 4 deletions examples/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ async function main() {
const docHashes = [
utf8('{"example": "document"}'),
utf8('{"example": 2}'),
randomAsU8a(),
randomAsU8a(Math.floor(Math.random() * 64)),
utf8('{"example": 4}'),
].map((bytes) => Anchor.hash(bytes).bytes);
const proofs = await anchorBatched(anchor, docHashes);
assert((await checkBatched(anchor, docHashes[0], proofs[0])) !== null);
assert((await checkBatched(anchor, docHashes[0], proofs[1])) === null);

// single
const single = Anchor.hash(randomAsU8a()).bytes;
const single = Anchor.hash(randomAsU8a(Math.floor(Math.random() * 64))).bytes;
assert((await checkBatched(anchor, single, [])) === null);
await anchorBatched(anchor, [single]);
assert((await checkBatched(anchor, single, [])) !== null);
Expand Down Expand Up @@ -96,7 +96,7 @@ function benchSingleProofCreation(count) {
/* eslint-disable no-unused-vars */
const data = Array(count)
.fill(undefined)
.map((_, __) => randomAsU8a());
.map((_, __) => randomAsU8a(Math.floor(Math.random() * 64)));
const start = new Date().getTime();
const hashes = data.map((bytes) => Anchor.hash(bytes).bytes);
const pl = pack32(hashes);
Expand All @@ -118,7 +118,7 @@ function timeProofGeneration(count) {
/* eslint-disable no-unused-vars */
const data = Array(count)
.fill(undefined)
.map((_, __) => randomAsU8a());
.map((_, __) => randomAsU8a(Math.floor(Math.random() * 64)));
const start = new Date().getTime();
const hashes = data.map((bytes) => Anchor.hash(bytes).bytes);
const [root, proofs] = buildMerkleTreeAndProofs(hashes);
Expand Down
8 changes: 4 additions & 4 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@docknetwork/sdk-examples",
"private": true,
"type": "module",
"version": "0.10.0",
"version": "0.11.0",
"scripts": {
"bbs-dock-example": "babel-node ./bbs-dock.js",
"claim-deduction-example": "babel-node ./claim-deduction.js",
Expand All @@ -19,9 +19,9 @@
"lint": "eslint \"*.js\""
},
"dependencies": {
"@docknetwork/credential-sdk": "0.27.0",
"@docknetwork/dock-blockchain-api": "0.12.0",
"@docknetwork/dock-blockchain-modules": "0.16.0"
"@docknetwork/credential-sdk": "0.28.0",
"@docknetwork/dock-blockchain-api": "0.13.0",
"@docknetwork/dock-blockchain-modules": "0.17.0"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
Expand Down
11 changes: 11 additions & 0 deletions packages/cheqd-blockchain-api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @docknetwork/cheqd-blockchain-api

## 0.22.0

### Minor Changes

- Allow to create keypairs from private keys, conversion tweaks

### Patch Changes

- Updated dependencies
- @docknetwork/credential-sdk@0.28.0

## 0.21.0

### Minor Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/cheqd-blockchain-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/cheqd-blockchain-api",
"version": "0.21.0",
"version": "0.22.0",
"license": "MIT",
"main": "./dist/esm/index.js",
"type": "module",
Expand Down Expand Up @@ -34,7 +34,7 @@
},
"dependencies": {
"@cheqd/sdk": "cjs",
"@docknetwork/credential-sdk": "0.27.0"
"@docknetwork/credential-sdk": "0.28.0"
},
"devDependencies": {
"@babel/cli": "^7.24.1",
Expand Down
6 changes: 5 additions & 1 deletion packages/cheqd-blockchain-api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ import {
CheqdTestnetAccumulator,
CheqdMainnetAccumulator,
DockDidOrDidMethodKey,
CheqdMainnetVerificationMethodSignature,
CheqdTestnetVerificationMethodSignature,
} from '@docknetwork/credential-sdk/types';
import { TypedEnum } from '@docknetwork/credential-sdk/types/generic';

Expand Down Expand Up @@ -107,6 +109,7 @@ export class CheqdAPI extends AbstractApiProvider {
OffchainSignatureKeyRef: CheqdTestnetOffchainSignatureKeyRef,
BlobId: CheqdTestnetBlobId,
StatusListCredentialId: CheqdTestnetStatusListCredentialId,
VerificationMethodSignature: CheqdTestnetVerificationMethodSignature,
}),
[CheqdNetwork.Mainnet]: extendNull({
Did: CheqdMainnetDid,
Expand All @@ -119,6 +122,7 @@ export class CheqdAPI extends AbstractApiProvider {
OffchainSignatureKeyRef: CheqdMainnetOffchainSignatureKeyRef,
BlobId: CheqdMainnetBlobId,
StatusListCredentialId: CheqdMainnetStatusListCredentialId,
VerificationMethodSignature: CheqdMainnetVerificationMethodSignature,
}),
});

Expand Down Expand Up @@ -263,7 +267,7 @@ export class CheqdAPI extends AbstractApiProvider {
}

methods() {
return ['cheqd'];
return ['cheqd', 'dock'];
}

// eslint-disable-next-line
Expand Down
11 changes: 11 additions & 0 deletions packages/cheqd-blockchain-modules/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @docknetwork/cheqd-blockchain-modules

## 0.19.0

### Minor Changes

- Allow to create keypairs from private keys, conversion tweaks

### Patch Changes

- Updated dependencies
- @docknetwork/credential-sdk@0.28.0

## 0.18.0

### Minor Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/cheqd-blockchain-modules/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/cheqd-blockchain-modules",
"version": "0.18.0",
"version": "0.19.0",
"type": "module",
"license": "MIT",
"main": "./dist/esm/index.js",
Expand Down Expand Up @@ -33,7 +33,7 @@
"node": ">=18.0.0"
},
"dependencies": {
"@docknetwork/credential-sdk": "0.27.0"
"@docknetwork/credential-sdk": "0.28.0"
},
"devDependencies": {
"@babel/cli": "^7.24.1",
Expand All @@ -42,7 +42,7 @@
"@babel/plugin-syntax-import-attributes": "^7.25.6",
"@babel/plugin-transform-modules-commonjs": "^7.24.1",
"@babel/preset-env": "^7.24.3",
"@docknetwork/cheqd-blockchain-api": "0.21.0",
"@docknetwork/cheqd-blockchain-api": "0.22.0",
"@rollup/plugin-alias": "^4.0.2",
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^24.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { VerificationMethodSignature } from '@docknetwork/credential-sdk/types';
import { ensureInstanceOf } from '@docknetwork/credential-sdk/utils';
import { TypedUUID } from '@docknetwork/credential-sdk/types/generic';
import CheqdApiProvider from './cheqd-api-provider';
Expand All @@ -21,7 +20,10 @@ const createDIDMethodTx = (fnName) => {

const signatures = []
.concat(didKeypairs)
.map((didKeypair) => VerificationMethodSignature.fromDidKeypair(didKeypair, bytes));
.map((didKeypair) => root.types.VerificationMethodSignature.fromDidKeypair(
didKeypair,
bytes,
));

const value = {
payload,
Expand Down
6 changes: 6 additions & 0 deletions packages/credential-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @docknetwork/credential-sdk

## 0.28.0

### Minor Changes

- Allow to create keypairs from private keys, conversion tweaks

## 0.27.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/credential-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/credential-sdk",
"version": "0.27.0",
"version": "0.28.0",
"license": "MIT",
"type": "module",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/credential-sdk/src/keypairs/did-keypair.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DockKeypair from './keypair';
import DockKeypair from './dock-keypair';
import { DidKey } from '../types/did/onchain/did-key';
import VerificationMethodRefOrIdentRef from '../types/did/document/verification-method-ref-or-ident-ref';
import { ensureInstanceOf } from '../utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,51 @@ import {
import { randomAsHex } from '../utils/bytes';

/**
* Wrapped keypair used to sign byte sequences of arbitrary size.
* Abstract keypair used to sign byte sequences of arbitrary size.
*/
class DockKeypair {
/**
* Wraps supplied keypair into a `DockKeypair`.
* Instantiates new `DockKeypair` with the provided underlying keypair.
*
* @param {*} keyPair
*/
constructor(keyPair) {
this.keyPair = keyPair;
}

/**
* Generates `DockKeypair` from the supplied entropy.
* @param {Uint8Array} entropy
* @returns {DockKeypair}
*/
static fromEntropy(entropy) {
// eslint-disable-next-line no-underscore-dangle
return new this(entropy, 'entropy');
}

/**
* Generates `DockKeypair` from the supplied seed.
* @param {Uint8Array} seed
* @returns {DockKeypair}
*/
static fromSeed(seed) {
// eslint-disable-next-line no-underscore-dangle
return new this(seed, 'seed');
}

/**
* Generates `DockKeypair` from the supplied private key.
* @param {Uint8Array} privateKey
* @returns {DockKeypair}
*/
static fromPrivateKey(privateKey) {
// eslint-disable-next-line no-underscore-dangle
return new this(privateKey, 'private');
}

/**
* Generates random `DockKeypair`.
* @returns {}
* @returns {DockKeypair}
*/
static random() {
return new this(randomAsHex(this.SeedSize));
Expand Down Expand Up @@ -87,7 +117,7 @@ class DockKeypair {
export default withExtendedPrototypeProperties(
['privateKey', '_publicKey', '_sign'],
withExtendedStaticProperties(
['Signature', 'VerKeyType', 'SeedSize', 'verify'],
['Signature', 'VerKeyType', 'SeedSize', 'verify', 'constructor'],
DockKeypair,
),
);
2 changes: 1 addition & 1 deletion packages/credential-sdk/src/keypairs/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { default as DockKeypair } from './keypair';
export { default as DockKeypair } from './dock-keypair';
export { default as Ed25519Keypair } from './keypair-ed25519';
export { default as Secp256k1Keypair } from './keypair-secp256k1';
export { default as DidKeypair } from './did-keypair';
40 changes: 36 additions & 4 deletions packages/credential-sdk/src/keypairs/keypair-ed25519.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { generateKeyPairFromSeed, sign, verify } from '@stablelib/ed25519';
import {
generateKeyPairFromSeed,
sign,
verify,
extractPublicKeyFromSecretKey,
} from '@stablelib/ed25519';
import { SignatureEd25519 } from '../types/signatures';
import { Ed25519VerKeyName } from '../vc/crypto/constants';
import { normalizeToU8a, valueBytes } from '../utils';
import DockKeypair from './keypair';
import DockKeypair from './dock-keypair';

export default class Ed25519Keypair extends DockKeypair {
static Signature = SignatureEd25519;
Expand All @@ -11,8 +16,35 @@ export default class Ed25519Keypair extends DockKeypair {

static SeedSize = 32;

constructor(seed) {
super(generateKeyPairFromSeed(normalizeToU8a(seed)));
/**
*
* Instantiates new `Ed25519Keypair` from the provided source.
* It can have one of two types: "seed" or "private".
*
* @param {Uint8Array} seedOrPrivate
* @param {"seed"|"private"} sourceType
*/
constructor(seedOrPrivate, sourceType = 'seed') {
let kp;
switch (sourceType) {
case 'seed':
kp = generateKeyPairFromSeed(normalizeToU8a(seedOrPrivate));
break;
case 'private': {
const secretKey = normalizeToU8a(seedOrPrivate);
kp = {
secretKey,
publicKey: extractPublicKeyFromSecretKey(secretKey),
};
break;
}
default:
throw new Error(
`Unsupported source type: \`${sourceType}\`, it must be either "seed" or "private"`,
);
}

super(kp);
}

_publicKey() {
Expand Down
Loading

0 comments on commit cfc66a8

Please sign in to comment.