Skip to content

Commit

Permalink
Merge pull request #3759 from BitGo/BG-0000-commonKeychain-util-clarity
Browse files Browse the repository at this point in the history
chore(sdk-core): add clarity to other commonKeychain util
  • Loading branch information
kxl4126 authored Jul 26, 2023
2 parents 1560ca1 + b8a21f3 commit bc4fd75
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
28 changes: 21 additions & 7 deletions modules/bitgo/test/v2/unit/internal/tssUtils/ecdsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1471,13 +1471,27 @@ describe('TSS Ecdsa Utils:', async function () {
const message = ECDSAUtils.EcdsaUtils.getMessageToSignFromChallenge(challenge);
message.should.equal(expectedMessageToSign);
});

it('publicKeyFromCommonKeychain returns correct public key', function () {
const commonKeychain =
'03f40c70545b519bb7bbc7195fd4b7d5bbfc873bfd38b18596e4b47a05b6a88d552e2e8319cb31e279b99dbe54115a983d35e86679af96d81b7478d1df368f76a8';
const expectedPubKeyResult = `f40c70545b519bb7bbc7195fd4b7d5bbfc873bfd38b18596e4b47a05b6a88d556a10d6ab8055dc0b3a9af9dc4e42f4f9773c590afcc298d017c1b1ce29a88041`;
const actualPubKey = ECDSAUtils.EcdsaUtils.publicKeyFromCommonKeychain(commonKeychain);
actualPubKey.should.equal(expectedPubKeyResult);
describe('validateCommonKeychainPublicKey', function () {
it('validateCommonKeychainPublicKey returns correct public key', function () {
const commonKeychain =
'03f40c70545b519bb7bbc7195fd4b7d5bbfc873bfd38b18596e4b47a05b6a88d552e2e8319cb31e279b99dbe54115a983d35e86679af96d81b7478d1df368f76a8';
const expectedPubKeyResult = `f40c70545b519bb7bbc7195fd4b7d5bbfc873bfd38b18596e4b47a05b6a88d556a10d6ab8055dc0b3a9af9dc4e42f4f9773c590afcc298d017c1b1ce29a88041`;
const actualPubKey = ECDSAUtils.EcdsaUtils.validateCommonKeychainPublicKey(commonKeychain);
actualPubKey.should.equal(expectedPubKeyResult);
});
it('validateCommonKeychainPublicKey throws correctly with invalid length', function () {
const commonKeychain = '03f40c70548';
should(() => ECDSAUtils.EcdsaUtils.validateCommonKeychainPublicKey(commonKeychain)).throwError(
'Invalid commonKeychain length, expected 130, got 11'
);
});
it('validateCommonKeychainPublicKey throws correctly with invalid commonKeychain', function () {
const commonKeychainWithInvalidCharacters =
'!@#$^0c70545b519bb7bbc7195fd4b7d5bfc873bfd38b18596e4b47a05b6a88d552e2e8319cb31e279b99dbe54115a983d35e86679af96d81b7478d1df368f76a8'; // 129 chars
should(() =>
ECDSAUtils.EcdsaUtils.validateCommonKeychainPublicKey(commonKeychainWithInvalidCharacters)
).throwError('Unknown point format');
});
});

// #region Nock helpers
Expand Down
8 changes: 7 additions & 1 deletion modules/sdk-core/src/bitgo/utils/tss/ecdsa/ecdsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,13 @@ export class EcdsaUtils extends baseTSSUtils<KeyShare> {
.result();
}

static publicKeyFromCommonKeychain(commonKeychain: string): string {
/**
* util function that checks that a commonKeychain is valid and can ultimately resolve to a valid public key
* @param commonKeychain - a user uploaded commonKeychain string
* @throws if the commonKeychain is invalid length or invalid format
*/

static validateCommonKeychainPublicKey(commonKeychain: string) {
const pub = EcdsaUtils.getPublicKeyFromCommonKeychain(commonKeychain);
const secp256k1 = new ec('secp256k1');
const key = secp256k1.keyFromPublic(pub, 'hex');
Expand Down

0 comments on commit bc4fd75

Please sign in to comment.