Skip to content

Commit ce7e4c6

Browse files
authored
adds a single signer transaction signing test (#5683)
adds a test case to wallet slow tests to serve as an example for how to build a test transaction and sign it using the JavaScript SDK useful for integrations that require testing transaction signing (e.g., Ledger integration) without running a network and/or building a chain
1 parent 17a0d65 commit ce7e4c6

File tree

1 file changed

+69
-2
lines changed

1 file changed

+69
-2
lines changed

ironfish/src/wallet/wallet.test.slow.ts

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44
import type { SpiedFunction } from 'jest-mock'
5-
import { Asset, ASSET_ID_LENGTH, generateKey, multisig } from '@ironfish/rust-nodejs'
5+
import {
6+
Asset,
7+
ASSET_ID_LENGTH,
8+
generateKey,
9+
multisig,
10+
Note as NativeNote,
11+
verifyTransactions,
12+
} from '@ironfish/rust-nodejs'
613
import { Assert } from '../assert'
7-
import { Transaction } from '../primitives'
14+
import { makeFakeWitness } from '../devUtils'
15+
import { Note, RawTransaction, Transaction } from '../primitives'
816
import { Target } from '../primitives/target'
17+
import { TransactionVersion } from '../primitives/transaction'
918
import {
1019
createNodeTest,
1120
useAccountFixture,
@@ -1368,4 +1377,62 @@ describe('Wallet', () => {
13681377
.map((identity) => identity.toString('hex'))
13691378
expect(identities.sort()).toEqual(storedIdentities.sort())
13701379
})
1380+
1381+
it('build and signs transactions', () => {
1382+
// Generate random key
1383+
const { outgoingViewKey, proofAuthorizingKey, publicAddress, spendingKey, viewKey } =
1384+
generateKey()
1385+
1386+
const inNote = new NativeNote(
1387+
publicAddress,
1388+
42n,
1389+
Buffer.from(''),
1390+
Asset.nativeId(),
1391+
publicAddress,
1392+
)
1393+
const outNote = new NativeNote(
1394+
publicAddress,
1395+
40n,
1396+
Buffer.from(''),
1397+
Asset.nativeId(),
1398+
publicAddress,
1399+
)
1400+
const asset = new Asset(publicAddress, 'Testcoin', 'A really cool coin')
1401+
const mintOutNote = new NativeNote(
1402+
publicAddress,
1403+
5n,
1404+
Buffer.from(''),
1405+
asset.id(),
1406+
publicAddress,
1407+
)
1408+
1409+
// Construct fake note witness for input note to spend
1410+
const witness = makeFakeWitness(new Note(inNote.serialize()))
1411+
1412+
// Construct raw transaction
1413+
const raw = new RawTransaction(TransactionVersion.V1)
1414+
raw.spends.push({ note: new Note(inNote.serialize()), witness })
1415+
raw.outputs.push({ note: new Note(outNote.serialize()) })
1416+
raw.outputs.push({ note: new Note(mintOutNote.serialize()) })
1417+
raw.mints.push({
1418+
creator: asset.creator().toString('hex'),
1419+
name: asset.name().toString(),
1420+
metadata: asset.metadata().toString(),
1421+
value: mintOutNote.value(),
1422+
})
1423+
raw.fee = 1n
1424+
1425+
// Build transaction and construct proofs to generate unsigned transaction
1426+
const unsignedTransaction = raw.build(proofAuthorizingKey, viewKey, outgoingViewKey)
1427+
1428+
// Sign unsigned transaction
1429+
const serializedTransaction = unsignedTransaction.sign(spendingKey)
1430+
const signedTransaction = new Transaction(serializedTransaction)
1431+
1432+
// Check that hash is equal to hash of unsigned transaction
1433+
expect(signedTransaction.unsignedHash()).toEqualBuffer(unsignedTransaction.hash())
1434+
1435+
// Check that signed transaction verifies
1436+
expect(verifyTransactions([serializedTransaction])).toBeTruthy()
1437+
})
13711438
})

0 commit comments

Comments
 (0)