Skip to content

Commit

Permalink
chore: refactor accept share logic
Browse files Browse the repository at this point in the history
Ticket: PX-3296
  • Loading branch information
ravneet-bitgo committed Apr 18, 2024
1 parent 68ca8d6 commit 47fc503
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 43 deletions.
1 change: 0 additions & 1 deletion modules/sdk-api/src/bitgoAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import * as utxolib from '@bitgo/utxo-lib';
import { bip32, ECPairInterface } from '@bitgo/utxo-lib';
import * as bitcoinMessage from 'bitcoinjs-message';
import { isBrowser, isWebWorker } from 'browser-or-node';
import * as bs58 from 'bs58';
import { createHmac } from 'crypto';
import debugLib from 'debug';
import * as _ from 'lodash';
Expand Down
2 changes: 1 addition & 1 deletion modules/sdk-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"dependencies": {
"@bitgo/bls-dkg": "^1.3.1",
"@bitgo/public-types": "2.1.0",
"@bitgo/public-types": "2.8.0",
"@bitgo/sdk-lib-mpc": "^9.2.0",
"@bitgo/sjcl": "^1.0.1",
"@bitgo/statics": "^48.6.0",
Expand Down
1 change: 1 addition & 0 deletions modules/sdk-core/src/bitgo/keychain/iKeychains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,5 @@ export interface IKeychains {
getKeysForSigning(params?: GetKeysForSigningOptions): Promise<Keychain[]>;
createMpc(params: CreateMpcOptions): Promise<KeychainsTriplet>;
createTssBitGoKeyFromOvcShares(ovcOutput: OvcToBitGoJSON): Promise<BitGoKeyFromOvcShares>;
createWalletKeychain(userPassword: string): Promise<Keychain>;
}
26 changes: 25 additions & 1 deletion modules/sdk-core/src/bitgo/keychain/keychains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import assert from 'assert';
import * as common from '../../common';
import { IBaseCoin, KeychainsTriplet, KeyPair } from '../baseCoin';
import { BitGoBase } from '../bitgoBase';
import { BlsUtils, RequestTracer, EDDSAUtils, ECDSAUtils, decodeOrElse } from '../utils';
import { BlsUtils, RequestTracer, EDDSAUtils, ECDSAUtils, decodeOrElse, generateRandomPassword } from '../utils';
import {
AddKeychainOptions,
ApiKeyShare,
Expand Down Expand Up @@ -427,4 +427,28 @@ export class Keychains implements IKeychains {
throw new Error(`Error producing the output: ${errors}`);
});
}

/**
* Create keychain for ofc wallet using the password
* @param userPassword
* @returns
*/
async createWalletKeychain(userPassword: string): Promise<Keychain> {
const keychains = this.baseCoin.keychains();
const newKeychain = keychains.create();
const originalPasscodeEncryptionCode = generateRandomPassword(5);

const encryptedPrv = this.bitgo.encrypt({
password: userPassword,
input: newKeychain.prv,
});

const walletKeychain = await keychains.add({
encryptedPrv,
originalPasscodeEncryptionCode,
pub: newKeychain.pub,
source: 'user',
});
return walletKeychain;
}
}
6 changes: 1 addition & 5 deletions modules/sdk-core/src/bitgo/wallet/iWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import { ILightning } from '../lightning';
import { SerializedNtilde } from '../../account-lib/mpc/tss/ecdsa/types';
import { IAddressBook } from '../address-book';
import { WalletUser } from '@bitgo/public-types';

export interface MaximumSpendableOptions {
minValue?: number | string;
Expand Down Expand Up @@ -575,11 +576,6 @@ export interface CrossChainUTXO {
export type WalletType = 'backing' | 'cold' | 'custodial' | 'custodialPaired' | 'hot' | 'trading';
export type SubWalletType = 'distributedCustody';

export type WalletUser = {
user: string;
permissions: string[];
};

export interface WalletData {
id: string;
approvalsRequired: number;
Expand Down
37 changes: 6 additions & 31 deletions modules/sdk-core/src/bitgo/wallet/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { IBaseCoin, KeychainsTriplet, SupplementGenerateWalletOptions } from '..
import { BitGoBase } from '../bitgoBase';
import { getSharedSecret } from '../ecdh';
import { AddKeychainOptions, Keychain } from '../keychain';
import { generateRandomPassword, promiseProps, RequestTracer } from '../utils';
import { promiseProps, RequestTracer } from '../utils';
import {
AcceptShareOptions,
AddWalletOptions,
Expand All @@ -28,8 +28,6 @@ import {
} from './iWallets';
import { Wallet } from './wallet';

const debug = require('debug')('bitgo:v2:wallets');

export class Wallets implements IWallets {
private readonly bitgo: BitGoBase;
private readonly baseCoin: IBaseCoin;
Expand Down Expand Up @@ -545,8 +543,8 @@ export class Wallets implements IWallets {
* @param walletId
* @param userPassword
*/
async reshareOfcAccountWithSpenders(walletId: string, userPassword: string): Promise<void> {
const wallet = await this.bitgo.coin('ofc').wallets().get({ id: walletId });
async reshareWalletWithSpenders(walletId: string, userPassword: string): Promise<void> {
const wallet = await this.get({ id: walletId });
if (!wallet?._wallet?.enterprise) {
throw new Error('Enterprise not found for the wallet');
}
Expand Down Expand Up @@ -579,29 +577,6 @@ export class Wallets implements IWallets {
}
}

/**
* Create keychain for ofc wallet using the password
* @param userPassword
* @returns
*/
async createKeychain(userPassword: string): Promise<Keychain> {
const keychains = this.baseCoin.keychains();
const newKeychain = keychains.create();
const originalPasscodeEncryptionCode = generateRandomPassword(5);

const encryptedPrv = this.bitgo.encrypt({
password: userPassword,
input: newKeychain.prv,
});

const walletKeychain = await keychains.add({
encryptedPrv,
originalPasscodeEncryptionCode,
pub: newKeychain.pub,
source: 'user',
});
return walletKeychain;
}
/**
* Accepts a wallet share, adding the wallet to the user's list
* Needs a user's password to decrypt the shared key
Expand All @@ -623,7 +598,7 @@ export class Wallets implements IWallets {
if (_.isUndefined(params.userPassword)) {
throw new Error('userPassword param must be provided to decrypt shared key');
}
const walletKeychain = await this.createKeychain(params.userPassword);
const walletKeychain = await this.baseCoin.keychains().createWalletKeychain(params.userPassword);
const response = await this.updateShare({
walletShareId: params.walletShareId,
state: 'accepted',
Expand All @@ -632,9 +607,9 @@ export class Wallets implements IWallets {
// If the wallet share was accepted successfully (changed=true), reshare the wallet with the spenders
if (response.changed && response.state === 'accepted') {
try {
await this.reshareOfcAccountWithSpenders(walletShare.wallet, params.userPassword);
await this.reshareWalletWithSpenders(walletShare.wallet, params.userPassword);
} catch (e) {
debug('failed to reshare wallet with spenders', e);
throw new Error('Failed to reshare wallet with spenders');
}
}
return response;
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1056,10 +1056,10 @@
"@scure/base" "1.1.5"
micro-eth-signer "0.7.2"

"@bitgo/public-types@2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@bitgo/public-types/-/public-types-2.1.0.tgz#b9ede95d490907f610cdc5e9ec4156e13645b852"
integrity sha512-EvjvArAExJd/tY8PPii3SE3lWOpAJ2+lJBtn3DVMmTROXfS7q9nLvBbAZHPZZ3Q/VkPEeAfJ7KZ2Fvbr/TgtQg==
"@bitgo/public-types@2.8.0":
version "2.8.0"
resolved "https://registry.yarnpkg.com/@bitgo/public-types/-/public-types-2.8.0.tgz#4ebad052056246fa721876fb424227db8ce750f7"
integrity sha512-zVMe7PDoneGdTT6XHwEnNy8pUPbggyPzSa+ojqxLBG7iIgLb3WxnUgCejAOuRtF5pgcr5tyBLVS3ceijudjG/Q==
dependencies:
"@api-ts/io-ts-http" "1.0.0"
fp-ts "2.16.2"
Expand Down

0 comments on commit 47fc503

Please sign in to comment.