|
| 1 | +import * as openpgp from 'openpgp'; |
| 2 | +import { RedPallasMPSComms, RedPallasMPSTypes } from '@bitgo/sdk-lib-mpc'; |
| 3 | +import { |
| 4 | + RedpallasMPCv2SignatureShareRound1Input, |
| 5 | + RedpallasMPCv2SignatureShareRound1Output, |
| 6 | + RedpallasMPCv2SignatureShareRound2Input, |
| 7 | + RedpallasMPCv2SignatureShareRound2Output, |
| 8 | + RedpallasMPCv2SignatureShareRound3Input, |
| 9 | + RedpallasMPCv2SignatureShareRound3Output, |
| 10 | +} from '@bitgo/public-types'; |
| 11 | +import { SignatureShareRecord, SignatureShareType } from '../../utils/tss/baseTypes'; |
| 12 | +import { MPCv2PartiesEnum } from '../../utils/tss/ecdsa/typesMPCv2'; |
| 13 | + |
| 14 | +type SignerPartyId = MPCv2PartiesEnum.USER | MPCv2PartiesEnum.BACKUP; |
| 15 | + |
| 16 | +function partyIdToSignatureShareType(partyId: MPCv2PartiesEnum): SignatureShareType { |
| 17 | + switch (partyId) { |
| 18 | + case MPCv2PartiesEnum.USER: |
| 19 | + return SignatureShareType.USER; |
| 20 | + case MPCv2PartiesEnum.BACKUP: |
| 21 | + return SignatureShareType.BACKUP; |
| 22 | + case MPCv2PartiesEnum.BITGO: |
| 23 | + return SignatureShareType.BITGO; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * RedPallas MPS DSG signature-share helpers. |
| 29 | + * |
| 30 | + * Groundwork for a future custodial/cold (SMC/OVC) DSG signing flow - not yet wired up to any |
| 31 | + * caller in this SDK. Mirrors `../eddsa/eddsaMPCv2.ts` (same 3-round shape, same PGP-signed- |
| 32 | + * message envelope), but kept as an independent copy - built on `RedPallasMPSComms` - so |
| 33 | + * RedPallas MPS never depends on the EdDSA MPS module, and vice versa. |
| 34 | + */ |
| 35 | + |
| 36 | +/** |
| 37 | + * Builds the round-1 signature share record. |
| 38 | + * |
| 39 | + * PGP-signs the WASM round-0 broadcast message with the signer's ephemeral key and |
| 40 | + * wraps it into a SignatureShareRecord ready for `sendSignatureShareV2`. |
| 41 | + */ |
| 42 | +export async function getSignatureShareRoundOne( |
| 43 | + userMsg1: RedPallasMPSTypes.DeserializedMessage, |
| 44 | + userGpgPrivKey: openpgp.PrivateKey, |
| 45 | + partyId: SignerPartyId = MPCv2PartiesEnum.USER, |
| 46 | + otherSignerPartyId: MPCv2PartiesEnum = MPCv2PartiesEnum.BITGO |
| 47 | +): Promise<SignatureShareRecord> { |
| 48 | + const signedMsg1 = await RedPallasMPSComms.detachSignMpsMessage(Buffer.from(userMsg1.payload), userGpgPrivKey); |
| 49 | + const share: RedpallasMPCv2SignatureShareRound1Input = { |
| 50 | + type: 'round1Input', |
| 51 | + data: { msg1: signedMsg1 }, |
| 52 | + }; |
| 53 | + return { |
| 54 | + from: partyIdToSignatureShareType(partyId), |
| 55 | + to: partyIdToSignatureShareType(otherSignerPartyId), |
| 56 | + share: JSON.stringify(share), |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Verifies the peer's round-1 PGP signature and returns the raw deserialized |
| 62 | + * message ready for `RedPallasDSG.handleIncomingMessages`. |
| 63 | + */ |
| 64 | +export async function verifyPeerMessageRoundOne( |
| 65 | + parsedRound1Output: RedpallasMPCv2SignatureShareRound1Output, |
| 66 | + peerGpgKey: openpgp.Key, |
| 67 | + peerPartyId: MPCv2PartiesEnum = MPCv2PartiesEnum.BITGO |
| 68 | +): Promise<RedPallasMPSTypes.DeserializedMessage> { |
| 69 | + const rawBytes = await RedPallasMPSComms.verifyMpsMessage(parsedRound1Output.data.msg1, peerGpgKey); |
| 70 | + return { |
| 71 | + from: peerPartyId, |
| 72 | + payload: new Uint8Array(rawBytes), |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Builds the round-2 signature share record. |
| 78 | + */ |
| 79 | +export async function getSignatureShareRoundTwo( |
| 80 | + userMsg2: RedPallasMPSTypes.DeserializedMessage, |
| 81 | + userGpgPrivKey: openpgp.PrivateKey, |
| 82 | + partyId: SignerPartyId = MPCv2PartiesEnum.USER, |
| 83 | + otherSignerPartyId: MPCv2PartiesEnum = MPCv2PartiesEnum.BITGO |
| 84 | +): Promise<SignatureShareRecord> { |
| 85 | + const signedMsg2 = await RedPallasMPSComms.detachSignMpsMessage(Buffer.from(userMsg2.payload), userGpgPrivKey); |
| 86 | + const share: RedpallasMPCv2SignatureShareRound2Input = { |
| 87 | + type: 'round2Input', |
| 88 | + data: { msg2: signedMsg2 }, |
| 89 | + }; |
| 90 | + return { |
| 91 | + from: partyIdToSignatureShareType(partyId), |
| 92 | + to: partyIdToSignatureShareType(otherSignerPartyId), |
| 93 | + share: JSON.stringify(share), |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Verifies the peer's round-2 PGP signature and returns the raw deserialized |
| 99 | + * message ready for `RedPallasDSG.handleIncomingMessages`. |
| 100 | + */ |
| 101 | +export async function verifyPeerMessageRoundTwo( |
| 102 | + parsedRound2Output: RedpallasMPCv2SignatureShareRound2Output, |
| 103 | + peerGpgKey: openpgp.Key, |
| 104 | + peerPartyId: MPCv2PartiesEnum = MPCv2PartiesEnum.BITGO |
| 105 | +): Promise<RedPallasMPSTypes.DeserializedMessage> { |
| 106 | + const rawBytes = await RedPallasMPSComms.verifyMpsMessage(parsedRound2Output.data.msg2, peerGpgKey); |
| 107 | + return { |
| 108 | + from: peerPartyId, |
| 109 | + payload: new Uint8Array(rawBytes), |
| 110 | + }; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Verifies the peer's round-3 PGP signature and returns the raw deserialized |
| 115 | + * message ready for `RedPallasDSG.handleIncomingMessages`. |
| 116 | + */ |
| 117 | +export async function verifyPeerMessageRoundThree( |
| 118 | + parsedRound3Output: RedpallasMPCv2SignatureShareRound3Output, |
| 119 | + peerGpgKey: openpgp.Key, |
| 120 | + peerPartyId: MPCv2PartiesEnum = MPCv2PartiesEnum.BITGO |
| 121 | +): Promise<RedPallasMPSTypes.DeserializedMessage> { |
| 122 | + const rawBytes = await RedPallasMPSComms.verifyMpsMessage(parsedRound3Output.data.msg3, peerGpgKey); |
| 123 | + return { from: peerPartyId, payload: new Uint8Array(rawBytes) }; |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Builds the round-3 signature share record (final signer message). |
| 128 | + * |
| 129 | + * There is no corresponding `verifyBitGoMessageRoundThree` because Wallet Platform |
| 130 | + * finalises the signing server-side after receiving round 3; the client obtains the |
| 131 | + * signed transaction via `sendTxRequest`. |
| 132 | + */ |
| 133 | +export async function getSignatureShareRoundThree( |
| 134 | + userMsg3: RedPallasMPSTypes.DeserializedMessage, |
| 135 | + userGpgPrivKey: openpgp.PrivateKey, |
| 136 | + partyId: SignerPartyId = MPCv2PartiesEnum.USER, |
| 137 | + otherSignerPartyId: MPCv2PartiesEnum = MPCv2PartiesEnum.BITGO |
| 138 | +): Promise<SignatureShareRecord> { |
| 139 | + const signedMsg3 = await RedPallasMPSComms.detachSignMpsMessage(Buffer.from(userMsg3.payload), userGpgPrivKey); |
| 140 | + const share: RedpallasMPCv2SignatureShareRound3Input = { |
| 141 | + type: 'round3Input', |
| 142 | + data: { msg3: signedMsg3 }, |
| 143 | + }; |
| 144 | + return { |
| 145 | + from: partyIdToSignatureShareType(partyId), |
| 146 | + to: partyIdToSignatureShareType(otherSignerPartyId), |
| 147 | + share: JSON.stringify(share), |
| 148 | + }; |
| 149 | +} |
0 commit comments