-
Notifications
You must be signed in to change notification settings - Fork 304
feat: add eddsa MPCv2 SMC utils #8845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b2b1adc
feat: add eddsa MPCv2 SMC utils
kisslove-dewangan b16dc14
chore: bump public-types to v6.21.0
kisslove-dewangan c74ea84
fix: remove walletGpgPubKeySigs in eddsa v2 round1 response
kisslove-dewangan a7fd39f
test: add eddsa MPCv2 SMC util test cases
kisslove-dewangan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
modules/sdk-core/src/bitgo/utils/tss/eddsa/SMC/utils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import assert from 'assert'; | ||
| import { | ||
| EddsaBitgoToOVC1Round1Response, | ||
| EddsaBitgoToOVC1Round2Response, | ||
| EddsaKeyCreationMPCv2StateEnum, | ||
| EddsaMPCv2KeyGenRound1Response, | ||
| EddsaMPCv2KeyGenRound2Response, | ||
| EddsaOVC1ToBitgoRound1Payload, | ||
| EddsaOVC2ToBitgoRound2Payload, | ||
| OVCIndexEnum, | ||
| } from '@bitgo/public-types'; | ||
| import { IBaseCoin } from '../../../../baseCoin'; | ||
| import { BitGoBase } from '../../../../bitgoBase'; | ||
| import { decodeOrElse, Keychain } from '../../../..'; | ||
| import { EddsaMPCv2Utils } from '../eddsaMPCv2'; | ||
| import { EddsaMPCv2KeyGenSendFn, KeyGenSenderForEnterprise } from '../eddsaMPCv2KeyGenSender'; | ||
|
|
||
| export class MPCv2SMCUtils { | ||
| private MPCv2Utils: EddsaMPCv2Utils; | ||
|
|
||
| constructor(private bitgo: BitGoBase, private baseCoin: IBaseCoin) { | ||
| this.MPCv2Utils = new EddsaMPCv2Utils(bitgo, baseCoin); | ||
| } | ||
|
|
||
| public async keyGenRound1( | ||
| enterprise: string, | ||
| payload: EddsaOVC1ToBitgoRound1Payload | ||
| ): Promise<EddsaBitgoToOVC1Round1Response> { | ||
| return this.keyGenRound1BySender(KeyGenSenderForEnterprise(this.bitgo, enterprise), payload); | ||
| } | ||
|
|
||
| public async keyGenRound2( | ||
| enterprise: string, | ||
| payload: EddsaOVC2ToBitgoRound2Payload | ||
| ): Promise<EddsaBitgoToOVC1Round2Response> { | ||
| return this.keyGenRound2BySender(KeyGenSenderForEnterprise(this.bitgo, enterprise), payload); | ||
| } | ||
|
|
||
| public async keyGenRound1BySender( | ||
| senderFn: EddsaMPCv2KeyGenSendFn<EddsaMPCv2KeyGenRound1Response>, | ||
| payload: EddsaOVC1ToBitgoRound1Payload | ||
| ): Promise<EddsaBitgoToOVC1Round1Response> { | ||
| assert( | ||
| payload.state === EddsaKeyCreationMPCv2StateEnum.WaitingForBitgoRound1Data, | ||
| `Invalid state for round 1, expected: ${EddsaKeyCreationMPCv2StateEnum.WaitingForBitgoRound1Data}, got: ${payload.state}` | ||
| ); | ||
| decodeOrElse(EddsaOVC1ToBitgoRound1Payload.name, EddsaOVC1ToBitgoRound1Payload, payload, (errors) => { | ||
| throw new Error(`error(s) parsing payload: ${errors}`); | ||
| }); | ||
|
|
||
| const ovc1 = payload.ovc[OVCIndexEnum.ONE]; | ||
| const ovc2 = payload.ovc[OVCIndexEnum.TWO]; | ||
| const result = await this.MPCv2Utils.sendKeyGenerationRound1BySender(senderFn, { | ||
| userGpgPublicKey: ovc1.gpgPubKey, | ||
| backupGpgPublicKey: ovc2.gpgPubKey, | ||
| userMsg1: ovc1.ovcMsg1, | ||
| backupMsg1: ovc2.ovcMsg1, | ||
| }); | ||
|
|
||
| const response = { | ||
| state: EddsaKeyCreationMPCv2StateEnum.WaitingForOVC1Round2Data, | ||
| tssVersion: payload.tssVersion, | ||
| walletType: payload.walletType, | ||
| coin: payload.coin, | ||
| ovc: payload.ovc, | ||
| platform: { | ||
| sessionId: result.sessionId, | ||
| bitgoMsg1: result.bitgoMsg1, | ||
| }, | ||
| }; | ||
|
|
||
| return decodeOrElse(EddsaBitgoToOVC1Round1Response.name, EddsaBitgoToOVC1Round1Response, response, (errors) => { | ||
| throw new Error(`error(s) parsing response: ${errors}`); | ||
| }); | ||
| } | ||
|
|
||
| public async keyGenRound2BySender( | ||
| senderFn: EddsaMPCv2KeyGenSendFn<EddsaMPCv2KeyGenRound2Response>, | ||
| payload: EddsaOVC2ToBitgoRound2Payload | ||
| ): Promise<EddsaBitgoToOVC1Round2Response> { | ||
| assert( | ||
| payload.state === EddsaKeyCreationMPCv2StateEnum.WaitingForBitgoRound2Data, | ||
| `Invalid state for round 2, expected: ${EddsaKeyCreationMPCv2StateEnum.WaitingForBitgoRound2Data}, got: ${payload.state}` | ||
| ); | ||
| decodeOrElse(EddsaOVC2ToBitgoRound2Payload.name, EddsaOVC2ToBitgoRound2Payload, payload, (errors) => { | ||
| throw new Error(`error(s) parsing payload: ${errors}`); | ||
| }); | ||
|
|
||
| const ovc1 = payload.ovc[OVCIndexEnum.ONE]; | ||
| const ovc2 = payload.ovc[OVCIndexEnum.TWO]; | ||
| const sessionId = payload.platform.sessionId; | ||
| const result = await this.MPCv2Utils.sendKeyGenerationRound2BySender(senderFn, { | ||
| sessionId, | ||
| userMsg2: ovc1.ovcMsg2, | ||
| backupMsg2: ovc2.ovcMsg2, | ||
| }); | ||
|
|
||
| assert.equal(sessionId, result.sessionId, 'Round 1 and round 2 session IDs do not match'); | ||
|
kisslove-dewangan marked this conversation as resolved.
|
||
|
|
||
| const keychains = this.baseCoin.keychains(); | ||
| const bitgoKeychain = await keychains.add({ | ||
| source: 'bitgo', | ||
| keyType: 'tss', | ||
| commonKeychain: result.commonPublicKeychain, | ||
| isMPCv2: true, | ||
| }); | ||
|
|
||
| const response = { | ||
| state: EddsaKeyCreationMPCv2StateEnum.WaitingForOVC1GenerateKey, | ||
| bitGoKeyId: bitgoKeychain.id, | ||
| tssVersion: payload.tssVersion, | ||
| walletType: payload.walletType, | ||
| coin: payload.coin, | ||
| ovc: payload.ovc, | ||
| platform: { | ||
| // sessionId carried over from payload.platform; safe because the assert above | ||
| // guarantees it equals result.sessionId. | ||
| ...payload.platform, | ||
| commonPublicKeychain: result.commonPublicKeychain, | ||
| bitgoMsg2: result.bitgoMsg2, | ||
| }, | ||
| }; | ||
|
|
||
| return decodeOrElse(EddsaBitgoToOVC1Round2Response.name, EddsaBitgoToOVC1Round2Response, response, (errors) => { | ||
| throw new Error(`error(s) parsing response: ${errors}`); | ||
| }); | ||
| } | ||
|
|
||
| public async uploadClientKeys( | ||
| bitgoKeyId: string, | ||
| userCommonKeychain: string, | ||
| backupCommonKeychain: string | ||
| ): Promise<{ userKeychain: Keychain; backupKeychain: Keychain; bitgoKeychain: Keychain }> { | ||
| assert( | ||
| userCommonKeychain === backupCommonKeychain, | ||
| 'Common keychain mismatch between the user and backup keychains' | ||
| ); | ||
|
|
||
| const keychains = this.baseCoin.keychains(); | ||
| const bitgoKeychain = await keychains.get({ id: bitgoKeyId }); | ||
| assert(bitgoKeychain, 'Keychain not found'); | ||
| assert(bitgoKeychain.source === 'bitgo', 'The keychain is not a BitGo keychain'); | ||
| assert(bitgoKeychain.type === 'tss', 'BitGo keychain is not a TSS keychain'); | ||
| assert(bitgoKeychain.commonKeychain, 'BitGo keychain does not have a common keychain'); | ||
| assert(bitgoKeychain.commonKeychain === userCommonKeychain, 'Common keychain mismatch between the OVCs and BitGo'); | ||
|
|
||
| const userKeychainPromise = keychains.add({ | ||
| source: 'user', | ||
| keyType: 'tss', | ||
| commonKeychain: userCommonKeychain, | ||
| isMPCv2: true, | ||
| }); | ||
| const backupKeychainPromise = keychains.add({ | ||
| source: 'backup', | ||
| keyType: 'tss', | ||
| commonKeychain: backupCommonKeychain, | ||
| isMPCv2: true, | ||
| }); | ||
|
|
||
| const [userKeychain, backupKeychain] = await Promise.all([userKeychainPromise, backupKeychainPromise]); | ||
| return { userKeychain, backupKeychain, bitgoKeychain }; | ||
| } | ||
| } | ||
|
kisslove-dewangan marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.