|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { Asset, multisig, Note as NativeNote, verifyTransactions } from '@ironfish/rust-nodejs' |
| 6 | +import { Note, RawTransaction } from './primitives' |
| 7 | +import { Transaction, TransactionVersion } from './primitives/transaction' |
| 8 | +import { makeFakeWitness } from './testUtilities' |
| 9 | + |
| 10 | +describe('multisig', () => { |
| 11 | + describe('dkg', () => { |
| 12 | + it('should create multisig accounts and sign transactions', () => { |
| 13 | + const participantSecrets = [ |
| 14 | + multisig.ParticipantSecret.random(), |
| 15 | + multisig.ParticipantSecret.random(), |
| 16 | + multisig.ParticipantSecret.random(), |
| 17 | + ] |
| 18 | + |
| 19 | + const secrets = participantSecrets.map((secret) => secret.serialize().toString('hex')) |
| 20 | + const identities = participantSecrets.map((secret) => |
| 21 | + secret.toIdentity().serialize().toString('hex'), |
| 22 | + ) |
| 23 | + |
| 24 | + const minSigners = 2 |
| 25 | + |
| 26 | + const round1Packages = secrets.map((_, index) => |
| 27 | + multisig.dkgRound1(identities[index], minSigners, identities), |
| 28 | + ) |
| 29 | + |
| 30 | + const round1PublicPackages = round1Packages.map( |
| 31 | + (packages) => packages.round1PublicPackage, |
| 32 | + ) |
| 33 | + |
| 34 | + const round2Packages = secrets.map((secret, index) => |
| 35 | + multisig.dkgRound2( |
| 36 | + secret, |
| 37 | + round1Packages[index].round1SecretPackage, |
| 38 | + round1PublicPackages, |
| 39 | + ), |
| 40 | + ) |
| 41 | + |
| 42 | + const round2PublicPackages = round2Packages.map( |
| 43 | + (packages) => packages.round2PublicPackage, |
| 44 | + ) |
| 45 | + |
| 46 | + const round3Packages = participantSecrets.map((participantSecret, index) => |
| 47 | + multisig.dkgRound3( |
| 48 | + participantSecret, |
| 49 | + round2Packages[index].round2SecretPackage, |
| 50 | + round1PublicPackages, |
| 51 | + round2PublicPackages, |
| 52 | + ), |
| 53 | + ) |
| 54 | + |
| 55 | + const publicAddress = round3Packages[0].publicAddress |
| 56 | + |
| 57 | + const raw = new RawTransaction(TransactionVersion.V1) |
| 58 | + |
| 59 | + const inNote = new NativeNote( |
| 60 | + publicAddress, |
| 61 | + 42n, |
| 62 | + Buffer.from(''), |
| 63 | + Asset.nativeId(), |
| 64 | + publicAddress, |
| 65 | + ) |
| 66 | + const outNote = new NativeNote( |
| 67 | + publicAddress, |
| 68 | + 40n, |
| 69 | + Buffer.from(''), |
| 70 | + Asset.nativeId(), |
| 71 | + publicAddress, |
| 72 | + ) |
| 73 | + const asset = new Asset(publicAddress, 'Testcoin', 'A really cool coin') |
| 74 | + const mintOutNote = new NativeNote( |
| 75 | + publicAddress, |
| 76 | + 5n, |
| 77 | + Buffer.from(''), |
| 78 | + asset.id(), |
| 79 | + publicAddress, |
| 80 | + ) |
| 81 | + |
| 82 | + const witness = makeFakeWitness(new Note(inNote.serialize())) |
| 83 | + |
| 84 | + raw.spends.push({ note: new Note(inNote.serialize()), witness }) |
| 85 | + raw.outputs.push({ note: new Note(outNote.serialize()) }) |
| 86 | + raw.outputs.push({ note: new Note(mintOutNote.serialize()) }) |
| 87 | + raw.mints.push({ |
| 88 | + creator: asset.creator().toString('hex'), |
| 89 | + name: asset.name().toString(), |
| 90 | + metadata: asset.metadata().toString(), |
| 91 | + value: mintOutNote.value(), |
| 92 | + }) |
| 93 | + raw.fee = 1n |
| 94 | + |
| 95 | + const proofAuthorizingKey = round3Packages[0].proofAuthorizingKey |
| 96 | + const viewKey = round3Packages[0].viewKey |
| 97 | + const outgoingViewKey = round3Packages[0].outgoingViewKey |
| 98 | + |
| 99 | + const unsignedTransaction = raw.build(proofAuthorizingKey, viewKey, outgoingViewKey) |
| 100 | + const transactionHash = unsignedTransaction.hash() |
| 101 | + |
| 102 | + const commitments = secrets.map((secret, index) => |
| 103 | + multisig.createSigningCommitment( |
| 104 | + secret, |
| 105 | + round3Packages[index].keyPackage, |
| 106 | + transactionHash, |
| 107 | + identities, |
| 108 | + ), |
| 109 | + ) |
| 110 | + |
| 111 | + // Simulates receiving raw commitments from Ledger |
| 112 | + // Ledger app generates raw commitments, not wrapped SigningCommitment |
| 113 | + const commitmentIdentities: string[] = [] |
| 114 | + const rawCommitments: string[] = [] |
| 115 | + for (const commitment of commitments) { |
| 116 | + const signingCommitment = new multisig.SigningCommitment(Buffer.from(commitment, 'hex')) |
| 117 | + commitmentIdentities.push(signingCommitment.identity().toString('hex')) |
| 118 | + rawCommitments.push(signingCommitment.rawCommitments().toString('hex')) |
| 119 | + } |
| 120 | + |
| 121 | + const signingPackage = unsignedTransaction.signingPackageFromRaw( |
| 122 | + commitmentIdentities, |
| 123 | + rawCommitments, |
| 124 | + ) |
| 125 | + |
| 126 | + // Ensure that we can extract deserialize and extract frost signing package |
| 127 | + // Ledger app needs frost signing package to generate signature shares |
| 128 | + const frostSigningPackage = new multisig.SigningPackage( |
| 129 | + Buffer.from(signingPackage, 'hex'), |
| 130 | + ).frostSigningPackage() |
| 131 | + expect(frostSigningPackage).not.toBeUndefined() |
| 132 | + |
| 133 | + const signatureShares = secrets.map((secret, index) => |
| 134 | + multisig.createSignatureShare(secret, round3Packages[index].keyPackage, signingPackage), |
| 135 | + ) |
| 136 | + |
| 137 | + // Ensure we can construct SignatureShare from parts |
| 138 | + // Ledger app returns raw frost signature shares |
| 139 | + for (const share of signatureShares) { |
| 140 | + const signatureShare = new multisig.SignatureShare(Buffer.from(share, 'hex')) |
| 141 | + const reconstructed = multisig.SignatureShare.fromFrost( |
| 142 | + signatureShare.frostSignatureShare(), |
| 143 | + signatureShare.identity(), |
| 144 | + ) |
| 145 | + expect(reconstructed.frostSignatureShare()).toEqual( |
| 146 | + signatureShare.frostSignatureShare(), |
| 147 | + ) |
| 148 | + expect(reconstructed.identity()).toEqual(signatureShare.identity()) |
| 149 | + } |
| 150 | + |
| 151 | + const serializedTransaction = multisig.aggregateSignatureShares( |
| 152 | + round3Packages[0].publicKeyPackage, |
| 153 | + signingPackage, |
| 154 | + signatureShares, |
| 155 | + ) |
| 156 | + const transaction = new Transaction(serializedTransaction) |
| 157 | + |
| 158 | + expect(verifyTransactions([serializedTransaction])).toBeTruthy() |
| 159 | + |
| 160 | + expect(transaction.unsignedHash().equals(transactionHash)).toBeTruthy() |
| 161 | + }) |
| 162 | + }) |
| 163 | +}) |
0 commit comments