|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as pgp from 'openpgp'; |
| 3 | +import { EddsaMPSDsg, MPSComms, MPSUtil } from '@bitgo/sdk-lib-mpc'; |
| 4 | +import { |
| 5 | + EddsaMPCv2SignatureShareRound1Input, |
| 6 | + EddsaMPCv2SignatureShareRound1Output, |
| 7 | + EddsaMPCv2SignatureShareRound2Input, |
| 8 | + EddsaMPCv2SignatureShareRound2Output, |
| 9 | + EddsaMPCv2SignatureShareRound3Input, |
| 10 | +} from '@bitgo/public-types'; |
| 11 | +import { SignatureShareRecord, SignatureShareType } from '../../../../../../src'; |
| 12 | +import { |
| 13 | + getEddsaSignatureShareRound1, |
| 14 | + getEddsaSignatureShareRound2, |
| 15 | + getEddsaSignatureShareRound3, |
| 16 | + verifyBitGoEddsaMessageRound1, |
| 17 | + verifyBitGoEddsaMessageRound2, |
| 18 | +} from '../../../../../../src/bitgo/tss/eddsa/eddsaMPCv2'; |
| 19 | +import { decodeWithCodec } from '../../../../../../src/bitgo/utils/codecs'; |
| 20 | +import { generateGPGKeyPair } from '../../../../../../src/bitgo/utils/opengpgUtils'; |
| 21 | +import { MPCv2PartiesEnum } from '../../../../../../src/bitgo/utils/tss/ecdsa/typesMPCv2'; |
| 22 | + |
| 23 | +describe('EdDSA MPS DSG helper functions', async () => { |
| 24 | + let userKeyShare: Buffer; |
| 25 | + let bitgoKeyShare: Buffer; |
| 26 | + let userGpgPrivKey: pgp.PrivateKey; |
| 27 | + let bitgoGpgPrivKey: pgp.PrivateKey; |
| 28 | + let bitgoGpgPubKey: pgp.Key; |
| 29 | + |
| 30 | + const signableHex = 'deadbeef'; |
| 31 | + const derivationPath = 'm/0'; |
| 32 | + |
| 33 | + before('generate EdDSA DKG key shares', async () => { |
| 34 | + const userGpgKeyPair = await generateGPGKeyPair('ed25519'); |
| 35 | + const bitgoGpgKeyPair = await generateGPGKeyPair('ed25519'); |
| 36 | + |
| 37 | + userGpgPrivKey = await pgp.readPrivateKey({ armoredKey: userGpgKeyPair.privateKey }); |
| 38 | + bitgoGpgPrivKey = await pgp.readPrivateKey({ armoredKey: bitgoGpgKeyPair.privateKey }); |
| 39 | + bitgoGpgPubKey = await pgp.readKey({ armoredKey: bitgoGpgKeyPair.publicKey }); |
| 40 | + |
| 41 | + const [userDkg, , bitgoDkg] = await MPSUtil.generateEdDsaDKGKeyShares(); |
| 42 | + userKeyShare = userDkg.getKeyShare(); |
| 43 | + bitgoKeyShare = bitgoDkg.getKeyShare(); |
| 44 | + }); |
| 45 | + |
| 46 | + // ── Round 1 ───────────────────────────────────────────────────────────────── |
| 47 | + |
| 48 | + it('getEddsaSignatureShareRound1 should build a valid round-1 share', async () => { |
| 49 | + const messageBuffer = Buffer.from(signableHex, 'hex'); |
| 50 | + const userDsg = new EddsaMPSDsg.DSG(MPCv2PartiesEnum.USER); |
| 51 | + userDsg.initDsg(userKeyShare, messageBuffer, derivationPath, MPCv2PartiesEnum.BITGO); |
| 52 | + const userMsg1 = userDsg.getFirstMessage(); |
| 53 | + |
| 54 | + const share: SignatureShareRecord = await getEddsaSignatureShareRound1(userMsg1, userGpgPrivKey); |
| 55 | + |
| 56 | + assert.strictEqual(share.from, SignatureShareType.USER); |
| 57 | + assert.strictEqual(share.to, SignatureShareType.BITGO); |
| 58 | + |
| 59 | + const parsed = decodeWithCodec( |
| 60 | + EddsaMPCv2SignatureShareRound1Input, |
| 61 | + JSON.parse(share.share), |
| 62 | + 'EddsaMPCv2SignatureShareRound1Input' |
| 63 | + ); |
| 64 | + assert.strictEqual(parsed.type, 'round1Input'); |
| 65 | + assert.ok(parsed.data.msg1.message, 'msg1.message should be set'); |
| 66 | + assert.ok(parsed.data.msg1.signature, 'msg1.signature should be set'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('verifyBitGoEddsaMessageRound1 should verify a valid BitGo round-1 message', async () => { |
| 70 | + const messageBuffer = Buffer.from(signableHex, 'hex'); |
| 71 | + const bitgoDsg = new EddsaMPSDsg.DSG(MPCv2PartiesEnum.BITGO); |
| 72 | + bitgoDsg.initDsg(bitgoKeyShare, messageBuffer, derivationPath, MPCv2PartiesEnum.USER); |
| 73 | + const bitgoMsg1 = bitgoDsg.getFirstMessage(); |
| 74 | + |
| 75 | + const bitgoSignedMsg1 = await MPSComms.detachSignMpsMessage(Buffer.from(bitgoMsg1.payload), bitgoGpgPrivKey); |
| 76 | + const round1Output: EddsaMPCv2SignatureShareRound1Output = { |
| 77 | + type: 'round1Output', |
| 78 | + data: { msg1: bitgoSignedMsg1 }, |
| 79 | + }; |
| 80 | + |
| 81 | + const result = await verifyBitGoEddsaMessageRound1(round1Output, bitgoGpgPubKey); |
| 82 | + |
| 83 | + assert.strictEqual(result.from, MPCv2PartiesEnum.BITGO); |
| 84 | + assert.ok(result.payload.length > 0, 'payload should be non-empty'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('verifyBitGoEddsaMessageRound1 should throw on a tampered message', async () => { |
| 88 | + const round1Output: EddsaMPCv2SignatureShareRound1Output = { |
| 89 | + type: 'round1Output', |
| 90 | + data: { |
| 91 | + msg1: { |
| 92 | + message: Buffer.from('tampered').toString('base64'), |
| 93 | + signature: '-----BEGIN PGP SIGNATURE-----\n\nINVALID\n-----END PGP SIGNATURE-----\n', |
| 94 | + }, |
| 95 | + }, |
| 96 | + }; |
| 97 | + |
| 98 | + await assert.rejects( |
| 99 | + verifyBitGoEddsaMessageRound1(round1Output, bitgoGpgPubKey), |
| 100 | + 'should throw on invalid signature' |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + // ── Round 2 ───────────────────────────────────────────────────────────────── |
| 105 | + |
| 106 | + it('getEddsaSignatureShareRound2 should build a valid round-2 share', async () => { |
| 107 | + const messageBuffer = Buffer.from(signableHex, 'hex'); |
| 108 | + const userDsg = new EddsaMPSDsg.DSG(MPCv2PartiesEnum.USER); |
| 109 | + userDsg.initDsg(userKeyShare, messageBuffer, derivationPath, MPCv2PartiesEnum.BITGO); |
| 110 | + const userMsg1 = userDsg.getFirstMessage(); |
| 111 | + |
| 112 | + const bitgoDsg = new EddsaMPSDsg.DSG(MPCv2PartiesEnum.BITGO); |
| 113 | + bitgoDsg.initDsg(bitgoKeyShare, messageBuffer, derivationPath, MPCv2PartiesEnum.USER); |
| 114 | + const bitgoMsg1 = bitgoDsg.getFirstMessage(); |
| 115 | + |
| 116 | + const bitgoSignedMsg1 = await MPSComms.detachSignMpsMessage(Buffer.from(bitgoMsg1.payload), bitgoGpgPrivKey); |
| 117 | + const bitgoDeserializedMsg1 = await verifyBitGoEddsaMessageRound1( |
| 118 | + { type: 'round1Output', data: { msg1: bitgoSignedMsg1 } }, |
| 119 | + bitgoGpgPubKey |
| 120 | + ); |
| 121 | + const [userMsg2] = userDsg.handleIncomingMessages([userMsg1, bitgoDeserializedMsg1]); |
| 122 | + |
| 123 | + const share: SignatureShareRecord = await getEddsaSignatureShareRound2(userMsg2, userGpgPrivKey); |
| 124 | + |
| 125 | + assert.strictEqual(share.from, SignatureShareType.USER); |
| 126 | + assert.strictEqual(share.to, SignatureShareType.BITGO); |
| 127 | + |
| 128 | + const parsed = decodeWithCodec( |
| 129 | + EddsaMPCv2SignatureShareRound2Input, |
| 130 | + JSON.parse(share.share), |
| 131 | + 'EddsaMPCv2SignatureShareRound2Input' |
| 132 | + ); |
| 133 | + assert.strictEqual(parsed.type, 'round2Input'); |
| 134 | + assert.ok(parsed.data.msg2.message, 'msg2.message should be set'); |
| 135 | + assert.ok(parsed.data.msg2.signature, 'msg2.signature should be set'); |
| 136 | + }); |
| 137 | + |
| 138 | + it('verifyBitGoEddsaMessageRound2 should verify a valid BitGo round-2 message', async () => { |
| 139 | + const messageBuffer = Buffer.from(signableHex, 'hex'); |
| 140 | + const userDsg = new EddsaMPSDsg.DSG(MPCv2PartiesEnum.USER); |
| 141 | + userDsg.initDsg(userKeyShare, messageBuffer, derivationPath, MPCv2PartiesEnum.BITGO); |
| 142 | + const userMsg1 = userDsg.getFirstMessage(); |
| 143 | + |
| 144 | + const bitgoDsg = new EddsaMPSDsg.DSG(MPCv2PartiesEnum.BITGO); |
| 145 | + bitgoDsg.initDsg(bitgoKeyShare, messageBuffer, derivationPath, MPCv2PartiesEnum.USER); |
| 146 | + const bitgoMsg1 = bitgoDsg.getFirstMessage(); |
| 147 | + |
| 148 | + const bitgoSignedMsg1 = await MPSComms.detachSignMpsMessage(Buffer.from(bitgoMsg1.payload), bitgoGpgPrivKey); |
| 149 | + const [bitgoMsg2] = bitgoDsg.handleIncomingMessages([bitgoMsg1, userMsg1]); |
| 150 | + const bitgoSignedMsg2 = await MPSComms.detachSignMpsMessage(Buffer.from(bitgoMsg2.payload), bitgoGpgPrivKey); |
| 151 | + |
| 152 | + const round2Output: EddsaMPCv2SignatureShareRound2Output = { |
| 153 | + type: 'round2Output', |
| 154 | + data: { msg2: bitgoSignedMsg2 }, |
| 155 | + }; |
| 156 | + |
| 157 | + void bitgoSignedMsg1; |
| 158 | + const result = await verifyBitGoEddsaMessageRound2(round2Output, bitgoGpgPubKey); |
| 159 | + |
| 160 | + assert.strictEqual(result.from, MPCv2PartiesEnum.BITGO); |
| 161 | + assert.ok(result.payload.length > 0, 'payload should be non-empty'); |
| 162 | + }); |
| 163 | + |
| 164 | + // ── Round 3 ───────────────────────────────────────────────────────────────── |
| 165 | + |
| 166 | + it('getEddsaSignatureShareRound3 should build a valid round-3 share', async () => { |
| 167 | + const messageBuffer = Buffer.from(signableHex, 'hex'); |
| 168 | + const userDsg = new EddsaMPSDsg.DSG(MPCv2PartiesEnum.USER); |
| 169 | + userDsg.initDsg(userKeyShare, messageBuffer, derivationPath, MPCv2PartiesEnum.BITGO); |
| 170 | + const userMsg1 = userDsg.getFirstMessage(); |
| 171 | + |
| 172 | + const bitgoDsg = new EddsaMPSDsg.DSG(MPCv2PartiesEnum.BITGO); |
| 173 | + bitgoDsg.initDsg(bitgoKeyShare, messageBuffer, derivationPath, MPCv2PartiesEnum.USER); |
| 174 | + const bitgoMsg1 = bitgoDsg.getFirstMessage(); |
| 175 | + |
| 176 | + // Advance to round 2 |
| 177 | + const bitgoSignedMsg1 = await MPSComms.detachSignMpsMessage(Buffer.from(bitgoMsg1.payload), bitgoGpgPrivKey); |
| 178 | + const bitgoDeserializedMsg1 = await verifyBitGoEddsaMessageRound1( |
| 179 | + { type: 'round1Output', data: { msg1: bitgoSignedMsg1 } }, |
| 180 | + bitgoGpgPubKey |
| 181 | + ); |
| 182 | + const [userMsg2] = userDsg.handleIncomingMessages([userMsg1, bitgoDeserializedMsg1]); |
| 183 | + |
| 184 | + const [bitgoMsg2] = bitgoDsg.handleIncomingMessages([bitgoMsg1, userMsg1]); |
| 185 | + const bitgoSignedMsg2 = await MPSComms.detachSignMpsMessage(Buffer.from(bitgoMsg2.payload), bitgoGpgPrivKey); |
| 186 | + const bitgoDeserializedMsg2 = await verifyBitGoEddsaMessageRound2( |
| 187 | + { type: 'round2Output', data: { msg2: bitgoSignedMsg2 } }, |
| 188 | + bitgoGpgPubKey |
| 189 | + ); |
| 190 | + const [userMsg3] = userDsg.handleIncomingMessages([userMsg2, bitgoDeserializedMsg2]); |
| 191 | + |
| 192 | + const share: SignatureShareRecord = await getEddsaSignatureShareRound3(userMsg3, userGpgPrivKey); |
| 193 | + |
| 194 | + assert.strictEqual(share.from, SignatureShareType.USER); |
| 195 | + assert.strictEqual(share.to, SignatureShareType.BITGO); |
| 196 | + |
| 197 | + const parsed = decodeWithCodec( |
| 198 | + EddsaMPCv2SignatureShareRound3Input, |
| 199 | + JSON.parse(share.share), |
| 200 | + 'EddsaMPCv2SignatureShareRound3Input' |
| 201 | + ); |
| 202 | + assert.strictEqual(parsed.type, 'round3Input'); |
| 203 | + assert.ok(parsed.data.msg3.message, 'msg3.message should be set'); |
| 204 | + assert.ok(parsed.data.msg3.signature, 'msg3.signature should be set'); |
| 205 | + }); |
| 206 | +}); |
0 commit comments