|
| 1 | +import typedDataExample from '../../__mocks__/typedDataExample.json'; |
| 2 | +import { |
| 3 | + Call, |
| 4 | + DeclareSignerDetails, |
| 5 | + DeployAccountSignerDetails, |
| 6 | + EthSigner, |
| 7 | + InvocationsSignerDetails, |
| 8 | + RPC, |
| 9 | + constants, |
| 10 | + eth, |
| 11 | + num, |
| 12 | + stark, |
| 13 | +} from '../../src'; |
| 14 | + |
| 15 | +describe('Ethereum signatures', () => { |
| 16 | + describe('privk, pubK', () => { |
| 17 | + test('Generates random PK', () => { |
| 18 | + const privK = eth.ethRandomPrivateKey(); |
| 19 | + expect(privK.length).toBe(66); |
| 20 | + expect(num.isHex(privK)).toBe(true); |
| 21 | + }); |
| 22 | + |
| 23 | + test('Generates pubKey', async () => { |
| 24 | + const mySigner = new EthSigner( |
| 25 | + '0x525bc68475c0955fae83869beec0996114d4bb27b28b781ed2a20ef23121b8de' |
| 26 | + ); |
| 27 | + expect(await mySigner.getPubKey()).toBe( |
| 28 | + '0x020178bb97615b49070eefad71cb2f159392274404e41db748d9397147cb25cf59' |
| 29 | + ); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('Signatures', () => { |
| 34 | + test('Message signature', async () => { |
| 35 | + const myPrivateKey = '0x525bc68475c0955fae83869beec0996114d4bb27b28b781ed2a20ef23121b8de'; |
| 36 | + const myEthSigner = new EthSigner(myPrivateKey); |
| 37 | + const message = typedDataExample; |
| 38 | + const sig = await myEthSigner.signMessage( |
| 39 | + message, |
| 40 | + '0x65a822fbee1ae79e898688b5a4282dc79e0042cbed12f6169937fddb4c26641' |
| 41 | + ); |
| 42 | + expect(sig).toMatchObject({ |
| 43 | + r: 46302720252787165203319064060867586811009528414735725622252684979112343882634n, |
| 44 | + s: 44228007167516598548621407232357037139087111723794788802261070080184864735744n, |
| 45 | + recovery: 1, |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + // TODO : To update when a contract account handling ETHEREUM signatures will be available. |
| 50 | + test('Transaction signature', async () => { |
| 51 | + const myPrivateKey = '0x525bc68475c0955fae83869beec0996114d4bb27b28b781ed2a20ef23121b8de'; |
| 52 | + const myEthSigner = new EthSigner(myPrivateKey); |
| 53 | + const myCall: Call = { |
| 54 | + contractAddress: '0x65a822fbee1ae79e898688b5a4282dc79e0042cbed12f6169937fddb4c26641', |
| 55 | + entrypoint: 'test', |
| 56 | + calldata: [1, 2], |
| 57 | + }; |
| 58 | + const sig = await myEthSigner.signTransaction([myCall], { |
| 59 | + version: '0x2', |
| 60 | + walletAddress: '0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691', |
| 61 | + cairoVersion: '1', |
| 62 | + chainId: constants.StarknetChainId.SN_SEPOLIA, |
| 63 | + nonce: 45, |
| 64 | + maxFee: 10 ** 15, |
| 65 | + } as InvocationsSignerDetails); |
| 66 | + expect(sig).toMatchObject({ |
| 67 | + r: 7985353442887841088086521795914083018399735702575968460096442990678259802335n, |
| 68 | + s: 54448706138210541940611627632626053501325595041277792020051079616748389329289n, |
| 69 | + recovery: 0, |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + test('Deploy account signature', async () => { |
| 74 | + const myPrivateKey = '0x525bc68475c0955fae83869beec0996114d4bb27b28b781ed2a20ef23121b8de'; |
| 75 | + const myEthSigner = new EthSigner(myPrivateKey); |
| 76 | + const myDeployAcc: DeployAccountSignerDetails = { |
| 77 | + version: '0x2', |
| 78 | + contractAddress: '0x65a822fbee1ae79e898688b5a4282dc79e0042cbed12f6169937fddb4c26641', |
| 79 | + chainId: constants.StarknetChainId.SN_SEPOLIA, |
| 80 | + classHash: '0x5f3614e8671257aff9ac38e929c74d65b02d460ae966cd826c9f04a7fa8e0d4', |
| 81 | + constructorCalldata: [1, 2], |
| 82 | + addressSalt: 1234, |
| 83 | + nonce: 45, |
| 84 | + maxFee: 10 ** 15, |
| 85 | + |
| 86 | + tip: 0, |
| 87 | + paymasterData: [], |
| 88 | + accountDeploymentData: [], |
| 89 | + nonceDataAvailabilityMode: RPC.EDataAvailabilityMode.L1, |
| 90 | + feeDataAvailabilityMode: RPC.EDataAvailabilityMode.L1, |
| 91 | + resourceBounds: stark.estimateFeeToBounds(constants.ZERO), |
| 92 | + }; |
| 93 | + const sig = await myEthSigner.signDeployAccountTransaction(myDeployAcc); |
| 94 | + expect(sig).toMatchObject({ |
| 95 | + r: 61114347636551792612206610795983058940674613154346642566929862226007498517027n, |
| 96 | + s: 38870792724053768239218215863749216579253019684549941316832072720775828116206n, |
| 97 | + recovery: 1, |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + test('Declare signature', async () => { |
| 102 | + const myPrivateKey = '0x525bc68475c0955fae83869beec0996114d4bb27b28b781ed2a20ef23121b8de'; |
| 103 | + const myEthSigner = new EthSigner(myPrivateKey); |
| 104 | + const myDeclare: DeclareSignerDetails = { |
| 105 | + version: '0x2', |
| 106 | + chainId: constants.StarknetChainId.SN_SEPOLIA, |
| 107 | + senderAddress: '0x65a822fbee1ae79e898688b5a4282dc79e0042cbed12f6169937fddb4c26641', |
| 108 | + classHash: '0x5f3614e8671257aff9ac38e929c74d65b02d460ae966cd826c9f04a7fa8e0d4', |
| 109 | + nonce: 45, |
| 110 | + maxFee: 10 ** 15, |
| 111 | + |
| 112 | + tip: 0, |
| 113 | + paymasterData: [], |
| 114 | + accountDeploymentData: [], |
| 115 | + nonceDataAvailabilityMode: RPC.EDataAvailabilityMode.L1, |
| 116 | + feeDataAvailabilityMode: RPC.EDataAvailabilityMode.L1, |
| 117 | + resourceBounds: stark.estimateFeeToBounds(constants.ZERO), |
| 118 | + }; |
| 119 | + const sig = await myEthSigner.signDeclareTransaction(myDeclare); |
| 120 | + expect(sig).toMatchObject({ |
| 121 | + r: 38069596217315916583476609659691868035000959604311196895707605245620900872129n, |
| 122 | + s: 420191492562045858770062885997406552542950984883779606809355688615026963844n, |
| 123 | + recovery: 1, |
| 124 | + }); |
| 125 | + }); |
| 126 | + }); |
| 127 | +}); |
0 commit comments