From ecb15976a838ad9fbdb9b167d17e188352e2c1cc Mon Sep 17 00:00:00 2001 From: Antoine Estienne Date: Tue, 7 Dec 2021 17:48:38 +0100 Subject: [PATCH] Add eth test for extension signature (#909) * add eth test for extension signature * cleanup --- .../src/background/handlers/Extension.test.ts | 87 ++++++++++++++++--- 1 file changed, 73 insertions(+), 14 deletions(-) diff --git a/packages/extension-base/src/background/handlers/Extension.test.ts b/packages/extension-base/src/background/handlers/Extension.test.ts index 6f51a59b..1b3085c9 100644 --- a/packages/extension-base/src/background/handlers/Extension.test.ts +++ b/packages/extension-base/src/background/handlers/Extension.test.ts @@ -8,6 +8,7 @@ import type { MetadataDef } from '@polkadot/extension-inject/types'; import type { KeyringPair } from '@polkadot/keyring/types'; import type { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types'; import type { SignerPayloadJSON } from '@polkadot/types/types'; +import type { KeypairType } from '@polkadot/util-crypto/types'; import { TypeRegistry } from '@polkadot/types'; import keyring from '@polkadot/ui-keyring'; @@ -45,15 +46,27 @@ describe('Extension', () => { return new Extension(state); } - const createAccount = async (): Promise => { - await extension.handle('id', 'pri(accounts.create.suri)', { - name: 'parent', - password, - suri - }, {} as chrome.runtime.Port); - const { address } = await extension.handle('id', 'pri(seed.validate)', { - suri - }, {} as chrome.runtime.Port); + const createAccount = async (type?: KeypairType): Promise => { + await extension.handle('id', 'pri(accounts.create.suri)', type && type === 'ethereum' + ? { + name: 'parent', + password, + suri, + type + } + : { + name: 'parent', + password, + suri + }, {} as chrome.runtime.Port); + const { address } = await extension.handle('id', 'pri(seed.validate)', type && type === 'ethereum' + ? { + suri, + type + } + : { + suri + }, {} as chrome.runtime.Port); return address; }; @@ -182,10 +195,10 @@ describe('Extension', () => { nonce: '0x0000000000000000', signedExtensions: ['CheckSpecVersion', 'CheckTxVersion', 'CheckGenesis', 'CheckMortality', 'CheckNonce', 'CheckWeight', 'ChargeTransactionPayment'], specVersion: '0x00000026', - tip: null, + tip: '0x00000000000000000000000000000000', transactionVersion: '0x00000005', version: 4 - } as unknown as SignerPayloadJSON; + }; }); test('signs with default signed extensions', async () => { @@ -208,6 +221,52 @@ describe('Extension', () => { }, {} as chrome.runtime.Port)).resolves.toEqual(true); }); + test('signs with default signed extensions - ethereum', async () => { + const ethAddress = await createAccount('ethereum'); + const ethPair = keyring.getPair(ethAddress); + + ethPair.decodePkcs8(password); + const ethPayload: SignerPayloadJSON = { + address: ethAddress, + blockHash: '0xf9fc354edc3ff49f43d5e2c14e3c609a0c4ba469ed091edf893d672993dc9bc0', + blockNumber: '0x00000393', + era: '0x3601', + genesisHash: '0xf9fc354edc3ff49f43d5e2c14e3c609a0c4ba469ed091edf893d672993dc9bc0', + method: '0x03003cd0a705a2dc65e5b1e1205896baa2be8a07c6e0070010a5d4e8', + nonce: '0x00000000', + signedExtensions: [ + 'CheckSpecVersion', + 'CheckTxVersion', + 'CheckGenesis', + 'CheckMortality', + 'CheckNonce', + 'CheckWeight', + 'ChargeTransactionPayment' + ], + specVersion: '0x000003e9', + tip: '0x00000000000000000000000000000000', + transactionVersion: '0x00000002', + version: 4 + }; + const registry = new TypeRegistry(); + + registry.setSignedExtensions(payload.signedExtensions); + + const signatureExpected = registry + .createType('ExtrinsicPayload', ethPayload, { version: ethPayload.version }).sign(ethPair); + + tabs.handle('1615191860871.5', 'pub(extrinsic.sign)', ethPayload, 'http://localhost:3000', {} as chrome.runtime.Port) + .then((result) => { + expect((result as ResponseSigning)?.signature).toEqual(signatureExpected.signature); + }).catch((err) => console.log(err)); + + await expect(extension.handle('1615192072290.7', 'pri(signing.approve.password)', { + id: state.allSignRequests[0].id, + password, + savePass: false + }, {} as chrome.runtime.Port)).resolves.toEqual(true); + }); + test('signs with user extensions, known types', async () => { const types = {} as unknown as Record; @@ -235,7 +294,7 @@ describe('Extension', () => { state.saveMetadata(meta); - const payload = { + const payload: SignerPayloadJSON = { address, blockHash: '0xe1b1dda72998846487e4d858909d4f9a6bbd6e338e4588e5d809de16b1317b80', blockNumber: '0x00000393', @@ -245,10 +304,10 @@ describe('Extension', () => { nonce: '0x0000000000000000', signedExtensions: ['MyUserExtension'], specVersion: '0x00000026', - tip: null, + tip: '0x00000000000000000000000000000000', transactionVersion: '0x00000005', version: 4 - } as unknown as SignerPayloadJSON; + }; const registry = new TypeRegistry();