From 7721ac722d385c04ebc6e7faea06330cc9c50eb1 Mon Sep 17 00:00:00 2001 From: Antoine Estienne Date: Thu, 25 Feb 2021 19:18:03 +0100 Subject: [PATCH] Fix eth derivation (#894) * added gerald test address * added test for toJson * fix toJson * lint * fix ecdsa --- packages/keyring/src/index.spec.ts | 17 ++++++++++++++++- packages/keyring/src/keyring.ts | 1 + packages/keyring/src/pair/index.spec.ts | 23 +++++++++++++++++++++++ packages/keyring/src/pair/index.ts | 3 +-- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packages/keyring/src/index.spec.ts b/packages/keyring/src/index.spec.ts index e8273df36..1ae7f0be0 100644 --- a/packages/keyring/src/index.spec.ts +++ b/packages/keyring/src/index.spec.ts @@ -313,7 +313,7 @@ describe('keypair', (): void => { const pair = keyring.createFromUri(PHRASE); const json = pair.toJson('password'); - expect(json.address).toEqual('0x0381351b1b46d2602b0992bb5d5531f9c1696b0812feb2534b6884adc47e2e1d8b'); + expect(json.address).toEqual('0x0381351b1b46d2602b0992bb5d5531f9c1696b0812feb2534b6884adc47e2e1d8b'); // this is the public key (different from address for ethereum) expect(json.encoding).toEqual({ content: ['pkcs8', 'ethereum'], type: ['scrypt', 'xsalsa20-poly1305'], @@ -325,6 +325,21 @@ describe('keypair', (): void => { expect(newPair.publicKey).toEqual(pair.publicKey); }); + it('encodes a pair toJSON and back', (): void => { + const pairOriginal = keyring.createFromUri(PHRASE); + const json = pairOriginal.toJson('password'); + const pair = keyring.addFromJson( + json + ); + + expect(pair.address).toEqual(ETH_ADDRESS_ONE); + + pair.decodePkcs8('password'); + + expect(pair.isLocked).toBe(false); + expect(pair.address).toBe(ETH_ADDRESS_ONE); + }); + it('allows adding from JSON', (): void => { const pair = keyring.addFromJson( JSON.parse('{"address":"KWCv1L3QX9LDPwY4VzvLmarEmXjVJidUzZcinvVnmxAJJCBou","encoded":"U8qFEaghhmNV2PgFhjqzmhyUy37Ok7abfFU2MNsBd0sAgAAAAQAAAAgAAAA3+NniKogzNphiMNueB1X0sGA07B6CaXWfpXPx45iSXoTTprwzU5mOoSqUWO0GKHROI72LN+uJ8Yfv6Ll6JOOV3VPKfoVoFmYm+zDrrMPa0gk5E5kUuSijxADcE6zUrliPVr0Ix/qaghu5SJ7RtWDQLBf4Hp86SJ8Gg6gTSSk=","encoding":{"content":["pkcs8","ethereum"],"type":["scrypt","xsalsa20-poly1305"],"version":"3"},"meta":{}}') diff --git a/packages/keyring/src/keyring.ts b/packages/keyring/src/keyring.ts index 1c6ae5630..8d2a5826e 100644 --- a/packages/keyring/src/keyring.ts +++ b/packages/keyring/src/keyring.ts @@ -152,6 +152,7 @@ export class Keyring implements KeyringInstance { const encType = !Array.isArray(type) ? [type] : type; + // Here the address and publicKey are 32 bytes and isomorphic. This is why the address field needs to be the public key for ethereum type pairs const publicKey = isHex(address) ? hexToU8a(address) : this.decodeAddress(address, ignoreChecksum); diff --git a/packages/keyring/src/pair/index.spec.ts b/packages/keyring/src/pair/index.spec.ts index c63e74477..e54a07c99 100644 --- a/packages/keyring/src/pair/index.spec.ts +++ b/packages/keyring/src/pair/index.spec.ts @@ -142,13 +142,36 @@ describe('pair', (): void => { }); describe('ethereum', (): void => { + const PUBLICDERIVED = new Uint8Array([ + 3, 129, 53, 27, 27, 70, 210, 96, + 43, 9, 146, 187, 93, 85, 49, 249, + 193, 105, 107, 8, 18, 254, 178, 83, + 75, 104, 132, 173, 196, 126, 46, 29, + 139 + ]); + const SECRETDERIVED = new Uint8Array([ + 7, 13, 195, 17, 115, 0, 1, 25, + 24, 226, 107, 2, 23, 105, 69, 204, + 21, 195, 213, 72, 207, 73, 253, 132, + 24, 217, 127, 147, 175, 105, 158, 70 + ]); + it('has a valid address from a known public', (): void => { const pair = createPair({ toSS58, type: 'ethereum' }, { publicKey: hexToU8a('0x03b9dc646dd71118e5f7fda681ad9eca36eb3ee96f344f582fbe7b5bcdebb13077') }); expect(pair.address).toEqual('0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887'); expect(pair.addressRaw).toEqual(hexToU8a('0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887')); }); + it('converts to json', (): void => { + const pair = createPair({ toSS58, type: 'ethereum' }, { publicKey: PUBLICDERIVED, secretKey: SECRETDERIVED }); + const json = pair.toJson('password'); + expect(json.encoding).toEqual({ + content: ['pkcs8', 'ethereum'], + type: ['scrypt', 'xsalsa20-poly1305'], + version: '3' + }); + }); it('has Gerald as test address for Ethereum type parachains', (): void => { const keyringEthereum = createTestPairs({ type: 'ethereum' }, false); diff --git a/packages/keyring/src/pair/index.ts b/packages/keyring/src/pair/index.ts index 59bfe90ba..7a0315756 100644 --- a/packages/keyring/src/pair/index.ts +++ b/packages/keyring/src/pair/index.ts @@ -173,8 +173,7 @@ export function createPair ({ toSS58, type }: Setup, { publicKey, secretKey }: P ); }, toJson: (passphrase?: string): KeyringPair$Json => { - // NOTE: in the case of Ethereum and Ecdsa, we encode the ful publicKey - to allow extraction. This will NOT match - // e.g. an Ethereum address which is non-reversible, so not used here) + // For ecdsa and ethereum, address and publicKey are not the same (and not bijective). We want the publicKey here const address = ['ecdsa', 'ethereum'].includes(type) ? u8aToHex(secp256k1Compress(publicKey)) : encodeAddress();