-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* remove helper.createEcdsaFromMnemonic function, instead to use keys.fromMnemonic.
- Loading branch information
1 parent
7a4b2e3
commit 579c000
Showing
16 changed files
with
231 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@zcloak/did': minor | ||
--- | ||
|
||
remove helper.createEcdsaFromMnemonic function, instead to use keys.fromMnemonic. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2021-2023 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { DOCUMENTS, testKeyring } from 'test-support'; | ||
|
||
import { ethereumEncode, initCrypto } from '@zcloak/crypto'; | ||
|
||
import { fromMnemonic } from './from'; | ||
|
||
describe('create did use keys', (): void => { | ||
beforeAll(async () => { | ||
await initCrypto(); | ||
}); | ||
|
||
it('fromMnemonic with index 0 and type ecdsa', () => { | ||
const mnemonic = | ||
'health correct setup usage father decorate curious copper sorry recycle skin equal'; | ||
const did = fromMnemonic(testKeyring, mnemonic, 'ecdsa', 0); | ||
|
||
expect(did.getDocument()).toMatchObject(DOCUMENTS.alice); | ||
}); | ||
|
||
it.each(Array.from({ length: 10 }).map((_, index) => index))( | ||
'fromMnemonic with index: %p', | ||
(index) => { | ||
const mnemonic = | ||
'health correct setup usage father decorate curious copper sorry recycle skin equal'; | ||
const did = fromMnemonic(testKeyring, mnemonic, 'ecdsa', index); | ||
|
||
expect(did.identifier).toBe( | ||
ethereumEncode( | ||
testKeyring.createFromMnemonic(mnemonic, `/m/44'/60'/0'/0/${index}`, 'ecdsa').publicKey | ||
) | ||
); | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2021-2023 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { DidDocument, DidUrl } from '@zcloak/did-resolver/types'; | ||
import type { KeyringInstance } from '@zcloak/keyring/types'; | ||
import type { Did } from '../did'; | ||
|
||
import { base58Encode, ethereumEncode } from '@zcloak/crypto'; | ||
|
||
import { fromDidDocument } from '../did/helpers'; | ||
|
||
/** | ||
* @name fromMnemonic | ||
* @summary create Did from mnemonic. | ||
* @description | ||
* create Did like `did:zk:` from `mnemonic`. generate signing key by `signingKeyType`. Returns [[Did]] instance. | ||
* | ||
* @param index | ||
* controller path is `/m/44'/60'/0'/0/index` | ||
* | ||
* ecdsa path is `/m/44'/60'/0'/0/index/0` | ||
* | ||
* ed25519 path is `//index` | ||
* | ||
* x25519 path is `//index * 2` | ||
* @example | ||
* ```javascript | ||
* import { keys } from '@zcloak/did'; | ||
* | ||
* const mnemonic = '...'; | ||
* | ||
* const did = keys.fromMnemonic(keyring, mnemonic, 'ecdsa') | ||
* ``` | ||
*/ | ||
export function fromMnemonic( | ||
keyring: KeyringInstance, | ||
mnemonic: string, | ||
signingKeyType: 'ecdsa' | 'ed25519' = 'ecdsa', | ||
index = 0 | ||
): Did { | ||
const controllerPath = `/m/44'/60'/0'/0/${index}`; | ||
const ecdsaPath = `${controllerPath}/0`; | ||
const edPath = `//${index * 2}`; | ||
const xPath = `//${index * 2 + 1}`; | ||
|
||
const controller = keyring.addFromMnemonic(mnemonic, controllerPath, 'ecdsa'); | ||
const singingKey = keyring.addFromMnemonic( | ||
mnemonic, | ||
signingKeyType === 'ecdsa' ? ecdsaPath : edPath, | ||
signingKeyType | ||
); | ||
const encryptionKey = keyring.addFromMnemonic(mnemonic, xPath, 'x25519'); | ||
|
||
const didUri: DidUrl = `did:zk:${ethereumEncode(controller.publicKey)}`; | ||
const document: DidDocument = { | ||
'@context': ['https://www.w3.org/ns/did/v1'], | ||
id: didUri, | ||
controller: [didUri], | ||
verificationMethod: [ | ||
{ | ||
id: `${didUri}#key-0`, | ||
controller: [didUri], | ||
type: | ||
signingKeyType === 'ecdsa' | ||
? 'EcdsaSecp256k1VerificationKey2019' | ||
: 'Ed25519VerificationKey2020', | ||
publicKeyMultibase: base58Encode(singingKey.publicKey) | ||
}, | ||
{ | ||
id: `${didUri}#key-1`, | ||
controller: [didUri], | ||
type: 'X25519KeyAgreementKey2019', | ||
publicKeyMultibase: base58Encode(encryptionKey.publicKey) | ||
} | ||
], | ||
authentication: [`${didUri}#key-0`], | ||
assertionMethod: [`${didUri}#key-0`], | ||
keyAgreement: [`${didUri}#key-1`], | ||
capabilityInvocation: [`${didUri}#key-0`], | ||
capabilityDelegation: [`${didUri}#key-0`], | ||
service: [] | ||
}; | ||
|
||
return fromDidDocument(document, keyring); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,24 @@ | ||
// Copyright 2021-2023 zcloak authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { testKeyring } from 'test-support'; | ||
|
||
import { initCrypto, mnemonicGenerate } from '@zcloak/crypto'; | ||
import { Keyring } from '@zcloak/keyring'; | ||
|
||
import { createEcdsaFromMnemonic } from '../did/helpers'; | ||
import { backup } from './backup'; | ||
import { fromMnemonic } from './from'; | ||
import { restore } from './restore'; | ||
|
||
describe('Restore did', (): void => { | ||
const keyring = new Keyring(); | ||
|
||
beforeAll(async () => { | ||
await initCrypto(); | ||
}); | ||
|
||
it('backup did and restore', () => { | ||
const mnemonic = mnemonicGenerate(12); | ||
const did = createEcdsaFromMnemonic(mnemonic, keyring); | ||
|
||
const json = backup(keyring, did, '1234'); | ||
const did = fromMnemonic(testKeyring, mnemonic); | ||
const json = backup(testKeyring, did, '1234'); | ||
|
||
expect(restore(keyring, json, '1234')).toMatchObject(did); | ||
expect(restore(testKeyring, json, '1234')).toMatchObject(did); | ||
}); | ||
}); |
Oops, something went wrong.