diff --git a/modules/sdk-api/src/bitgoAPI.ts b/modules/sdk-api/src/bitgoAPI.ts index 76b66c29df..898d150376 100644 --- a/modules/sdk-api/src/bitgoAPI.ts +++ b/modules/sdk-api/src/bitgoAPI.ts @@ -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'; diff --git a/modules/sdk-core/package.json b/modules/sdk-core/package.json index e301f9503f..38b8025bd4 100644 --- a/modules/sdk-core/package.json +++ b/modules/sdk-core/package.json @@ -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", diff --git a/modules/sdk-core/src/bitgo/keychain/iKeychains.ts b/modules/sdk-core/src/bitgo/keychain/iKeychains.ts index 77752ac30f..fef3664e2c 100644 --- a/modules/sdk-core/src/bitgo/keychain/iKeychains.ts +++ b/modules/sdk-core/src/bitgo/keychain/iKeychains.ts @@ -181,4 +181,5 @@ export interface IKeychains { getKeysForSigning(params?: GetKeysForSigningOptions): Promise; createMpc(params: CreateMpcOptions): Promise; createTssBitGoKeyFromOvcShares(ovcOutput: OvcToBitGoJSON): Promise; + createWalletKeychain(userPassword: string): Promise; } diff --git a/modules/sdk-core/src/bitgo/keychain/keychains.ts b/modules/sdk-core/src/bitgo/keychain/keychains.ts index bac469afe2..b4363c75c5 100644 --- a/modules/sdk-core/src/bitgo/keychain/keychains.ts +++ b/modules/sdk-core/src/bitgo/keychain/keychains.ts @@ -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, @@ -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 { + 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; + } } diff --git a/modules/sdk-core/src/bitgo/wallet/iWallet.ts b/modules/sdk-core/src/bitgo/wallet/iWallet.ts index b1abb342f7..eeabf63b09 100644 --- a/modules/sdk-core/src/bitgo/wallet/iWallet.ts +++ b/modules/sdk-core/src/bitgo/wallet/iWallet.ts @@ -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; @@ -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; diff --git a/modules/sdk-core/src/bitgo/wallet/wallets.ts b/modules/sdk-core/src/bitgo/wallet/wallets.ts index 2b713d5551..ac462fb8a7 100644 --- a/modules/sdk-core/src/bitgo/wallet/wallets.ts +++ b/modules/sdk-core/src/bitgo/wallet/wallets.ts @@ -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, @@ -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; @@ -545,8 +543,8 @@ export class Wallets implements IWallets { * @param walletId * @param userPassword */ - async reshareOfcAccountWithSpenders(walletId: string, userPassword: string): Promise { - const wallet = await this.bitgo.coin('ofc').wallets().get({ id: walletId }); + async reshareWalletWithSpenders(walletId: string, userPassword: string): Promise { + const wallet = await this.get({ id: walletId }); if (!wallet?._wallet?.enterprise) { throw new Error('Enterprise not found for the wallet'); } @@ -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 { - 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 @@ -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', @@ -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; diff --git a/yarn.lock b/yarn.lock index 628282354c..f1573a6c80 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"