|
| 1 | +import { Msg } from '@xdefi-tech/chains-core'; |
| 2 | +import { Transaction, PublicKey } from '@solana/web3.js'; |
| 3 | +import fetch from 'cross-fetch'; |
| 4 | + |
| 5 | +import { SolanaProvider } from '../chain.provider'; |
| 6 | +import { IndexerDataSource } from '../datasource'; |
| 7 | +import { SOLANA_MANIFEST } from '../manifests'; |
| 8 | +import { ChainMsg, MsgBody } from '../msg'; |
| 9 | + |
| 10 | +import LatticeSigner from './lattice.signer'; |
| 11 | + |
| 12 | +globalThis.fetch = fetch; |
| 13 | + |
| 14 | +/** |
| 15 | + * Update this config to match your Lattice device |
| 16 | + * |
| 17 | + * TODO: Update this to use environment variables |
| 18 | + */ |
| 19 | +const CONFIG = { |
| 20 | + deviceId: 'sc6J1U', |
| 21 | + password: 'asdfasdf', |
| 22 | + name: 'SDK Test2', |
| 23 | +}; |
| 24 | + |
| 25 | +jest.mock('@solana/web3.js', () => { |
| 26 | + const original = jest.requireActual('@solana/web3.js'); |
| 27 | + return { |
| 28 | + ...original, |
| 29 | + Transaction: { |
| 30 | + ...original.Transaction, |
| 31 | + prototype: { |
| 32 | + ...original.Transaction.prototype, |
| 33 | + serialize: jest |
| 34 | + .fn() |
| 35 | + .mockReturnValue(Buffer.from('serialized_transaction')), |
| 36 | + }, |
| 37 | + }, |
| 38 | + }; |
| 39 | +}); |
| 40 | + |
| 41 | +describe('lattice.signer', () => { |
| 42 | + let signer: LatticeSigner; |
| 43 | + let derivationPath: string; |
| 44 | + let provider: SolanaProvider; |
| 45 | + let txInput: MsgBody; |
| 46 | + let message: Msg; |
| 47 | + |
| 48 | + beforeAll(async () => { |
| 49 | + //@ts-ignore |
| 50 | + signer = await LatticeSigner.create(CONFIG); |
| 51 | + if (!signer.isPaired) { |
| 52 | + throw new Error('Failed to pair with Lattice device'); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + beforeEach(async () => { |
| 57 | + provider = new SolanaProvider(new IndexerDataSource(SOLANA_MANIFEST)); |
| 58 | + derivationPath = "m/44'/501'/0'/0/0"; |
| 59 | + |
| 60 | + txInput = { |
| 61 | + from: '7HZYYfdqQgDgNduLA5gh8y4A5Mr3rCLVWeXBF4Vg9qZZ', |
| 62 | + to: 'GrDMoeqMLFjeXQ24H56S1RLgT4R76jsuWCd6SvXyGPQ5', |
| 63 | + amount: 0.000001, |
| 64 | + gasPrice: 5000, |
| 65 | + priorityFeeAmount: 10000, |
| 66 | + }; |
| 67 | + |
| 68 | + message = provider.createMsg(txInput); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should get an address from the lattice device', async () => { |
| 72 | + const address = await signer.getAddress(derivationPath); |
| 73 | + expect(address).toBe(txInput.from); |
| 74 | + }, 100000); |
| 75 | + |
| 76 | + it('should sign a transaction using a lattice device', async () => { |
| 77 | + await signer.sign(message as ChainMsg, derivationPath); |
| 78 | + expect(message.signedTransaction).toBeTruthy(); |
| 79 | + }, 100000); |
| 80 | + |
| 81 | + it('should return false when verifying an invalid address', async () => { |
| 82 | + expect(signer.verifyAddress('invalid_address')).toBe(false); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should validate a correct address', async () => { |
| 86 | + expect(signer.verifyAddress(txInput.from)).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should fail if private key is requested', async () => { |
| 90 | + await expect(signer.getPrivateKey(derivationPath)).rejects.toThrow(); |
| 91 | + }); |
| 92 | +}); |
0 commit comments