|
2 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this
|
3 | 3 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
4 | 4 | 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' |
6 | 13 | import { Assert } from '../assert'
|
7 |
| -import { Transaction } from '../primitives' |
| 14 | +import { makeFakeWitness } from '../devUtils' |
| 15 | +import { Note, RawTransaction, Transaction } from '../primitives' |
8 | 16 | import { Target } from '../primitives/target'
|
| 17 | +import { TransactionVersion } from '../primitives/transaction' |
9 | 18 | import {
|
10 | 19 | createNodeTest,
|
11 | 20 | useAccountFixture,
|
@@ -1368,4 +1377,62 @@ describe('Wallet', () => {
|
1368 | 1377 | .map((identity) => identity.toString('hex'))
|
1369 | 1378 | expect(identities.sort()).toEqual(storedIdentities.sort())
|
1370 | 1379 | })
|
| 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 | + }) |
1371 | 1438 | })
|
0 commit comments