-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Cardano key derivation according to CIP3-Icarus (#158)
* Add `cip3Icarus` fixtures * Add `ed25519Bip32` derivation curve * Differentiate master node derivation based on `curve` `masterNodeGenerationSpec` * Support 64 byte long private keys * Support fingerprints from 32 byte long public keys * Allow `littleEndian` in `numberToUint32` util * Add `cip3Icarus` deriver * Add `CIP3IcarusNode` * Generate and test `cip3Icarus` vectors * Add `cip3Icarus` test vectors * Rename `cip3Icarus` to `cip3` To simplify the interface * fixup! rename `cip3Icarus` to `cip3` in derivation test vectors * Add `compressedPublicKeyLength` curve parameter So we can validate against it when creating fingerprint * Refactor `bip39` deriveChildKey `masterNodeGenerationSpec` condition to switch * fixup! curve specification to separate type * fixup! remove underscore * fixup! inline unnecessary functions * fixup! add JSDOCs to ed25519Bip32 file * fixup! quotes in src/curves/ed25519Bip32.test.ts Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com> * fixup! quotes in src/curves/ed25519Bip32.test.ts Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com> * fixyup! empty line in src/derivation.test.ts Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com> * fixup! `eslint-enable no-bitwise` in src/derivers/bip39.ts Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com> * fixup! add empty lines between test in `cip3` file * fixup! add JSDOCs to `cip3` file * fixup! `_` in src/curves/ed25519Bip32.ts Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com> * fixup! improve error message in src/derivers/cip3.ts Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com> * fixup! improve error message in src/utils.ts Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com> * fixup! fix quotes and test in fixes from github * Test all fns in `ed25519Bip32` * Test all fns in `cip3` * fixup! add test to have 100% coverage * fixup! test coverage * fixup! lint --------- Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com>
- Loading branch information
Showing
25 changed files
with
38,933 additions
and
45 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
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 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 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,90 @@ | ||
import { bytesToHex, hexToBytes } from '@metamask/utils'; | ||
|
||
import { ed25519Bip32 } from '.'; | ||
import fixtures from '../../test/fixtures'; | ||
import { | ||
bytesToNumberLE, | ||
compressPublicKey, | ||
decompressPublicKey, | ||
isValidPrivateKey, | ||
multiplyWithBase, | ||
} from './ed25519Bip32'; | ||
|
||
describe('getPublicKey', () => { | ||
fixtures.cip3.forEach((fixture) => { | ||
Object.values(fixture.nodes).forEach((node) => { | ||
it('returns correct public key from private key', async () => { | ||
const publicKey = await ed25519Bip32.getPublicKey( | ||
hexToBytes(node.privateKey), | ||
); | ||
|
||
expect(bytesToHex(publicKey)).toBe(node.publicKey); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('publicAdd', () => { | ||
it('returns correct public key from private key', async () => { | ||
const publicKey = hexToBytes(fixtures.cip3[0].nodes.bip39Node.publicKey); | ||
const tweak = hexToBytes(fixtures.cip3[0].nodes.purposeNode.publicKey); | ||
const added = ed25519Bip32.publicAdd(publicKey, tweak); | ||
|
||
expect(bytesToHex(added)).toBe( | ||
'0xf78d2a445afe9c961ac196fbac282b499d9ab6bbe8801354ee06fc22d46503e2', | ||
); | ||
}); | ||
}); | ||
|
||
describe('isValidPrivateKey', () => { | ||
it('returns true for bigint input', () => { | ||
const { privateKey } = fixtures.cip3[0].nodes.bip39Node; | ||
expect(isValidPrivateKey(privateKey)).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('compressPublicKey', () => { | ||
it('returns the same Uint8Array that was input', () => { | ||
const publicKey = Uint8Array.from( | ||
Buffer.from(fixtures.cip3[0].nodes.bip39Node.publicKey, 'hex'), | ||
); | ||
expect(compressPublicKey(publicKey)).toStrictEqual(publicKey); | ||
}); | ||
}); | ||
|
||
describe('decompressPublicKey', () => { | ||
it('returns the same Uint8Array that was input', () => { | ||
const publicKey = Uint8Array.from( | ||
Buffer.from(fixtures.cip3[0].nodes.bip39Node.publicKey, 'hex'), | ||
); | ||
expect(decompressPublicKey(publicKey)).toStrictEqual(publicKey); | ||
}); | ||
}); | ||
|
||
describe('bytesToNumberLE', () => { | ||
it('converts bytes to little endian bignumber', () => { | ||
const bytes = Uint8Array.from([ | ||
240, 230, 228, 13, 229, 184, 174, 13, 156, 72, 248, 206, 127, 130, 146, | ||
49, 175, 244, 32, 215, 146, 255, 153, 93, 197, 96, 64, 249, 123, 140, 119, | ||
72, | ||
]); | ||
expect(bytesToNumberLE(bytes)).toBe( | ||
32777749485515042639882960539696351427945957558989008047469858024981459691248n, | ||
); | ||
}); | ||
}); | ||
|
||
describe('multiplyWithBase', () => { | ||
it('multiplies bytes with the curve base', () => { | ||
const bytes = Uint8Array.from([ | ||
240, 230, 228, 13, 229, 184, 174, 13, 156, 72, 248, 206, 127, 130, 146, | ||
49, 175, 244, 32, 215, 146, 255, 153, 93, 197, 96, 64, 249, 123, 140, 119, | ||
72, | ||
]); | ||
const expectedResult = Uint8Array.from([ | ||
64, 197, 223, 88, 143, 127, 45, 60, 205, 81, 148, 125, 195, 249, 173, 214, | ||
27, 176, 227, 21, 216, 243, 146, 168, 189, 206, 85, 135, 89, 11, 210, 27, | ||
]); | ||
expect(multiplyWithBase(bytes)).toStrictEqual(expectedResult); | ||
}); | ||
}); |
Oops, something went wrong.