|
| 1 | +import 'test-matchers/toBeFrozenObject'; |
| 2 | + |
| 3 | +import { address, getAddressFromPublicKey } from '@solana/addresses'; |
| 4 | +import { generateKeyPair, SignatureBytes, signBytes } from '@solana/keys'; |
| 5 | +import { CompilableTransaction, signTransaction } from '@solana/transactions'; |
| 6 | + |
| 7 | +import { |
| 8 | + assertIsKeyPairSigner, |
| 9 | + createSignerFromKeyPair, |
| 10 | + generateKeyPairSigner, |
| 11 | + isKeyPairSigner, |
| 12 | + KeyPairSigner, |
| 13 | +} from '../keypair-signer'; |
| 14 | +import { createSignableMessage } from '../signable-message'; |
| 15 | + |
| 16 | +const getMockCryptoKeyPair = () => ({ privateKey: {}, publicKey: {} } as CryptoKeyPair); |
| 17 | + |
| 18 | +// Partial mocks. |
| 19 | +jest.mock('@solana/addresses', () => ({ |
| 20 | + ...jest.requireActual('@solana/addresses'), |
| 21 | + getAddressFromPublicKey: jest.fn(), |
| 22 | +})); |
| 23 | +jest.mock('@solana/keys', () => ({ |
| 24 | + ...jest.requireActual('@solana/keys'), |
| 25 | + generateKeyPair: jest.fn(), |
| 26 | + signBytes: jest.fn(), |
| 27 | +})); |
| 28 | +jest.mock('@solana/transactions', () => ({ |
| 29 | + ...jest.requireActual('@solana/transactions'), |
| 30 | + signTransaction: jest.fn(), |
| 31 | +})); |
| 32 | + |
| 33 | +describe('isKeyPairSigner', () => { |
| 34 | + it('checks whether a given value is a KeyPairSigner', () => { |
| 35 | + const myAddress = address('Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy'); |
| 36 | + const mySigner = { |
| 37 | + address: myAddress, |
| 38 | + keyPair: getMockCryptoKeyPair(), |
| 39 | + signMessages: async () => [], |
| 40 | + signTransactions: async () => [], |
| 41 | + } satisfies KeyPairSigner<'Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy'>; |
| 42 | + |
| 43 | + expect(isKeyPairSigner(mySigner)).toBe(true); |
| 44 | + expect(isKeyPairSigner({ address: myAddress })).toBe(false); |
| 45 | + expect(isKeyPairSigner({ ...mySigner, signMessages: 42 })).toBe(false); |
| 46 | + expect(isKeyPairSigner({ ...mySigner, signTransactions: 42 })).toBe(false); |
| 47 | + expect(isKeyPairSigner({ ...mySigner, keyPair: 42 })).toBe(false); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe('assertIsKeyPairSigner', () => { |
| 52 | + it('asserts that a given value is a KeyPairSigner', () => { |
| 53 | + const myAddress = address('Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy'); |
| 54 | + const mySigner = { |
| 55 | + address: myAddress, |
| 56 | + keyPair: getMockCryptoKeyPair(), |
| 57 | + signMessages: async () => [], |
| 58 | + signTransactions: async () => [], |
| 59 | + } satisfies KeyPairSigner<'Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy'>; |
| 60 | + |
| 61 | + const expectedMessage = 'The provided value does not implement the KeyPairSigner interface'; |
| 62 | + expect(() => assertIsKeyPairSigner(mySigner)).not.toThrow(); |
| 63 | + expect(() => assertIsKeyPairSigner({ address: myAddress })).toThrow(expectedMessage); |
| 64 | + expect(() => assertIsKeyPairSigner({ ...mySigner, signMessages: 42 })).toThrow(expectedMessage); |
| 65 | + expect(() => assertIsKeyPairSigner({ ...mySigner, signTransactions: 42 })).toThrow(expectedMessage); |
| 66 | + expect(() => assertIsKeyPairSigner({ ...mySigner, keyPair: 42 })).toThrow(expectedMessage); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe('createSignerFromKeyPair', () => { |
| 71 | + it('creates a KeyPairSigner from a given CryptoKeypair', async () => { |
| 72 | + expect.assertions(5); |
| 73 | + |
| 74 | + // Given a mock CryptoKeyPair returning a mock address. |
| 75 | + const myKeyPair = getMockCryptoKeyPair(); |
| 76 | + const myAddress = address('Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy'); |
| 77 | + jest.mocked(getAddressFromPublicKey).mockResolvedValueOnce(myAddress); |
| 78 | + |
| 79 | + // When we create a Signer from that CryptoKeyPair. |
| 80 | + const mySigner = await createSignerFromKeyPair(myKeyPair); |
| 81 | + mySigner satisfies KeyPairSigner; |
| 82 | + |
| 83 | + // Then the created signer kept track of the address and key pair. |
| 84 | + expect(jest.mocked(getAddressFromPublicKey)).toHaveBeenCalledTimes(1); |
| 85 | + expect(mySigner.address).toBe(myAddress); |
| 86 | + expect(mySigner.keyPair).toBe(myKeyPair); |
| 87 | + |
| 88 | + // And provided functions to sign messages and transactions. |
| 89 | + expect(typeof mySigner.signMessages).toBe('function'); |
| 90 | + expect(typeof mySigner.signTransactions).toBe('function'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('freezes the created signer', async () => { |
| 94 | + expect.assertions(1); |
| 95 | + const mySigner = await createSignerFromKeyPair(getMockCryptoKeyPair()); |
| 96 | + expect(mySigner).toBeFrozenObject(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('signs messages using the signBytes function', async () => { |
| 100 | + expect.assertions(7); |
| 101 | + |
| 102 | + // Given a KeyPairSigner created from a mock CryptoKeyPair. |
| 103 | + const myKeyPair = getMockCryptoKeyPair(); |
| 104 | + const myAddress = address('Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy'); |
| 105 | + jest.mocked(getAddressFromPublicKey).mockResolvedValueOnce(myAddress); |
| 106 | + const mySigner = await createSignerFromKeyPair(myKeyPair); |
| 107 | + |
| 108 | + // And given we mock the next two signatures of the signBytes function. |
| 109 | + const mockSignatures = [new Uint8Array([101, 101, 101]), new Uint8Array([201, 201, 201])] as SignatureBytes[]; |
| 110 | + jest.mocked(signBytes).mockResolvedValueOnce(mockSignatures[0]); |
| 111 | + jest.mocked(signBytes).mockResolvedValueOnce(mockSignatures[1]); |
| 112 | + |
| 113 | + // When we sign two messages using that signer. |
| 114 | + const messages = [ |
| 115 | + createSignableMessage(new Uint8Array([1, 1, 1])), |
| 116 | + createSignableMessage(new Uint8Array([2, 2, 2])), |
| 117 | + ]; |
| 118 | + const signatureDictionaries = await mySigner.signMessages(messages); |
| 119 | + |
| 120 | + // Then the signature directories contain the expected signatures. |
| 121 | + expect(signatureDictionaries[0]).toStrictEqual({ [myAddress]: mockSignatures[0] }); |
| 122 | + expect(signatureDictionaries[1]).toStrictEqual({ [myAddress]: mockSignatures[1] }); |
| 123 | + |
| 124 | + // And the signature directories are frozen. |
| 125 | + expect(signatureDictionaries[0]).toBeFrozenObject(); |
| 126 | + expect(signatureDictionaries[1]).toBeFrozenObject(); |
| 127 | + |
| 128 | + // And signBytes was called twice with the expected parameters. |
| 129 | + expect(jest.mocked(signBytes)).toHaveBeenCalledTimes(2); |
| 130 | + expect(jest.mocked(signBytes)).toHaveBeenNthCalledWith(1, myKeyPair.privateKey, messages[0].content); |
| 131 | + expect(jest.mocked(signBytes)).toHaveBeenNthCalledWith(2, myKeyPair.privateKey, messages[1].content); |
| 132 | + }); |
| 133 | + |
| 134 | + it('signs transactions using the signTransactions function', async () => { |
| 135 | + expect.assertions(7); |
| 136 | + |
| 137 | + // Given a KeyPairSigner created from a mock CryptoKeyPair. |
| 138 | + const myKeyPair = getMockCryptoKeyPair(); |
| 139 | + const myAddress = address('Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy'); |
| 140 | + jest.mocked(getAddressFromPublicKey).mockResolvedValueOnce(myAddress); |
| 141 | + const mySigner = await createSignerFromKeyPair(myKeyPair); |
| 142 | + |
| 143 | + // And given we have a couple of mock transactions to sign. |
| 144 | + const mockTransactions = [{} as CompilableTransaction, {} as CompilableTransaction]; |
| 145 | + |
| 146 | + // And given we mock the next two calls of the signTransactions function. |
| 147 | + const mockSignatures = [new Uint8Array([101, 101, 101]), new Uint8Array([201, 201, 201])] as SignatureBytes[]; |
| 148 | + jest.mocked(signTransaction).mockResolvedValueOnce({ |
| 149 | + ...mockTransactions[0], |
| 150 | + signatures: { [myAddress]: mockSignatures[0] }, |
| 151 | + }); |
| 152 | + jest.mocked(signTransaction).mockResolvedValueOnce({ |
| 153 | + ...mockTransactions[1], |
| 154 | + signatures: { [myAddress]: mockSignatures[1] }, |
| 155 | + }); |
| 156 | + |
| 157 | + // When we sign both transactions using that signer. |
| 158 | + const signatureDictionaries = await mySigner.signTransactions(mockTransactions); |
| 159 | + |
| 160 | + // Then the signature directories contain the expected signatures. |
| 161 | + expect(signatureDictionaries[0]).toStrictEqual({ [myAddress]: mockSignatures[0] }); |
| 162 | + expect(signatureDictionaries[1]).toStrictEqual({ [myAddress]: mockSignatures[1] }); |
| 163 | + |
| 164 | + // And the signature directories are frozen. |
| 165 | + expect(signatureDictionaries[0]).toBeFrozenObject(); |
| 166 | + expect(signatureDictionaries[1]).toBeFrozenObject(); |
| 167 | + |
| 168 | + // And signTransactions was called twice with the expected parameters. |
| 169 | + expect(jest.mocked(signTransaction)).toHaveBeenCalledTimes(2); |
| 170 | + expect(jest.mocked(signTransaction)).toHaveBeenNthCalledWith(1, [myKeyPair], mockTransactions[0]); |
| 171 | + expect(jest.mocked(signTransaction)).toHaveBeenNthCalledWith(2, [myKeyPair], mockTransactions[1]); |
| 172 | + }); |
| 173 | +}); |
| 174 | + |
| 175 | +describe('generateKeyPairSigner', () => { |
| 176 | + it('generates a new KeyPairSigner using the generateKeyPair function', async () => { |
| 177 | + expect.assertions(3); |
| 178 | + |
| 179 | + // Given we mock the return value of generateKeyPair. |
| 180 | + const mockKeypair = getMockCryptoKeyPair(); |
| 181 | + jest.mocked(generateKeyPair).mockResolvedValueOnce(mockKeypair); |
| 182 | + |
| 183 | + // And we mock the return value of getAddressFromPublicKey. |
| 184 | + const mockAddress = address('Gp7YgHcJciP4px5FdFnywUiMG4UcfMZV9UagSAZzDxdy'); |
| 185 | + jest.mocked(getAddressFromPublicKey).mockResolvedValueOnce(mockAddress); |
| 186 | + |
| 187 | + // When we generate a new KeyPairSigner from scratch. |
| 188 | + const mySigner = await generateKeyPairSigner(); |
| 189 | + mySigner satisfies KeyPairSigner; |
| 190 | + |
| 191 | + // Then the signer was created using the generated key pair and the mock address. |
| 192 | + expect(mySigner.keyPair).toBe(mockKeypair); |
| 193 | + expect(mySigner.address).toBe(mockAddress); |
| 194 | + |
| 195 | + // And generateKeyPair was called once. |
| 196 | + expect(jest.mocked(generateKeyPair)).toHaveBeenCalledTimes(1); |
| 197 | + }); |
| 198 | + |
| 199 | + it('freezes the generated signer', async () => { |
| 200 | + expect.assertions(1); |
| 201 | + |
| 202 | + // Given we mock the return value of generateKeyPair. |
| 203 | + const mockKeypair = getMockCryptoKeyPair(); |
| 204 | + jest.mocked(generateKeyPair).mockResolvedValueOnce(mockKeypair); |
| 205 | + |
| 206 | + // Then the generated signer is frozen. |
| 207 | + const mySigner = await generateKeyPairSigner(); |
| 208 | + expect(mySigner).toBeFrozenObject(); |
| 209 | + }); |
| 210 | +}); |
0 commit comments