@@ -24,13 +24,16 @@ import {
2424 BulkUpdateWalletShareOptionsRequest ,
2525 BulkUpdateWalletShareResponse ,
2626 GenerateBaseMpcWalletOptions ,
27+ GenerateGoAccountWalletOptions ,
28+ GenerateGoAccountWalletOptionsCodec ,
2729 GenerateLightningWalletOptions ,
2830 GenerateLightningWalletOptionsCodec ,
2931 GenerateMpcWalletOptions ,
3032 GenerateSMCMpcWalletOptions ,
3133 GenerateWalletOptions ,
3234 GetWalletByAddressOptions ,
3335 GetWalletOptions ,
36+ GoAccountWalletWithUserKeychain ,
3437 IWallets ,
3538 LightningWalletWithKeychains ,
3639 ListWalletOptions ,
@@ -47,7 +50,7 @@ import { createEvmKeyRingWallet, validateEvmKeyRingWalletParams } from '../evm/e
4750 * Check if a wallet is a WalletWithKeychains
4851 */
4952export function isWalletWithKeychains (
50- wallet : WalletWithKeychains | LightningWalletWithKeychains
53+ wallet : WalletWithKeychains | LightningWalletWithKeychains | GoAccountWalletWithUserKeychain
5154) : wallet is WalletWithKeychains {
5255 return wallet . responseType === 'WalletWithKeychains' ;
5356}
@@ -213,6 +216,57 @@ export class Wallets implements IWallets {
213216 } ;
214217 }
215218
219+ /**
220+ * Generate a Go Account wallet
221+ * @param params GenerateGoAccountWalletOptions
222+ * @returns Promise<GoAccountWalletWithUserKeychain>
223+ */
224+ private async generateGoAccountWallet (
225+ params : GenerateGoAccountWalletOptions
226+ ) : Promise < GoAccountWalletWithUserKeychain > {
227+ const reqId = new RequestTracer ( ) ;
228+ this . bitgo . setRequestTracer ( reqId ) ;
229+
230+ const { label, passphrase, enterprise, passcodeEncryptionCode } = params ;
231+
232+ const keychain = this . baseCoin . keychains ( ) . create ( ) ;
233+
234+ const keychainParams : AddKeychainOptions = {
235+ pub : keychain . pub ,
236+ encryptedPrv : this . bitgo . encrypt ( { password : passphrase , input : keychain . prv } ) ,
237+ originalPasscodeEncryptionCode : passcodeEncryptionCode ,
238+ keyType : 'independent' ,
239+ source : 'user' ,
240+ } ;
241+
242+ const userKeychain = await this . baseCoin . keychains ( ) . add ( keychainParams ) ;
243+
244+ const walletParams : SupplementGenerateWalletOptions = {
245+ label,
246+ m : 1 ,
247+ n : 1 ,
248+ type : 'trading' ,
249+ enterprise,
250+ keys : [ userKeychain . id ] ,
251+ } ;
252+
253+ const newWallet = await this . bitgo . post ( this . baseCoin . url ( '/wallet/add' ) ) . send ( walletParams ) . result ( ) ;
254+ const wallet = new Wallet ( this . bitgo , this . baseCoin , newWallet ) ;
255+
256+ const result : GoAccountWalletWithUserKeychain = {
257+ wallet,
258+ userKeychain,
259+ responseType : 'GoAccountWalletWithUserKeychain' ,
260+ } ;
261+
262+ // Add warning if the user keychain has an encrypted private key
263+ if ( ! _ . isUndefined ( userKeychain . encryptedPrv ) ) {
264+ result . warning = 'Be sure to backup the user keychain -- it is not stored anywhere else!' ;
265+ }
266+
267+ return result ;
268+ }
269+
216270 /**
217271 * Generate a new wallet
218272 * 1. Creates the user keychain locally on the client, and encrypts it with the provided passphrase
@@ -246,7 +300,7 @@ export class Wallets implements IWallets {
246300 */
247301 async generateWallet (
248302 params : GenerateWalletOptions = { }
249- ) : Promise < WalletWithKeychains | LightningWalletWithKeychains > {
303+ ) : Promise < WalletWithKeychains | LightningWalletWithKeychains | GoAccountWalletWithUserKeychain > {
250304 // Assign the default multiSig type value based on the coin
251305 if ( ! params . multisigType ) {
252306 params . multisigType = this . baseCoin . getDefaultMultisigType ( ) ;
@@ -270,6 +324,25 @@ export class Wallets implements IWallets {
270324 return walletData ;
271325 }
272326
327+ // Go Account wallet generation
328+ if ( this . baseCoin . getFamily ( ) === 'ofc' && params . type === 'trading' ) {
329+ const options = decodeOrElse (
330+ GenerateGoAccountWalletOptionsCodec . name ,
331+ GenerateGoAccountWalletOptionsCodec ,
332+ params ,
333+ ( errors ) => {
334+ throw new Error ( `error(s) parsing generate go account request params: ${ errors } ` ) ;
335+ }
336+ ) ;
337+
338+ const walletData = await this . generateGoAccountWallet ( options ) ;
339+ walletData . encryptedWalletPassphrase = this . bitgo . encrypt ( {
340+ input : options . passphrase ,
341+ password : options . passcodeEncryptionCode ,
342+ } ) ;
343+ return walletData ;
344+ }
345+
273346 common . validateParams ( params , [ 'label' ] , [ 'passphrase' , 'userKey' , 'backupXpub' ] ) ;
274347 if ( typeof params . label !== 'string' ) {
275348 throw new Error ( 'missing required string parameter label' ) ;
0 commit comments