@@ -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,59 @@ 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+ console . log ( 'generateGoAccountWallet' , params ) ;
232+
233+ const keychain = this . baseCoin . keychains ( ) . create ( ) ;
234+
235+ const keychainParams : AddKeychainOptions = {
236+ pub : keychain . pub ,
237+ encryptedPrv : this . bitgo . encrypt ( { password : passphrase , input : keychain . prv } ) ,
238+ originalPasscodeEncryptionCode : passcodeEncryptionCode ,
239+ keyType : 'independent' ,
240+ source : 'user' ,
241+ } ;
242+
243+ const userKeychain = await this . baseCoin . keychains ( ) . add ( keychainParams ) ;
244+
245+ const walletParams : SupplementGenerateWalletOptions = {
246+ label,
247+ m : 1 ,
248+ n : 1 ,
249+ type : 'trading' ,
250+ enterprise,
251+ keys : [ userKeychain . id ] ,
252+ } ;
253+ console . log ( 'walletParams' , walletParams ) ;
254+
255+ const newWallet = await this . bitgo . post ( this . baseCoin . url ( '/wallet/add' ) ) . send ( walletParams ) . result ( ) ;
256+ const wallet = new Wallet ( this . bitgo , this . baseCoin , newWallet ) ;
257+
258+ const result : GoAccountWalletWithUserKeychain = {
259+ wallet,
260+ userKeychain,
261+ responseType : 'GoAccountWalletWithUserKeychain' ,
262+ } ;
263+
264+ // Add warning if the user keychain has an encrypted private key
265+ if ( ! _ . isUndefined ( userKeychain . encryptedPrv ) ) {
266+ result . warning = 'Be sure to backup the user keychain -- it is not stored anywhere else!' ;
267+ }
268+
269+ return result ;
270+ }
271+
216272 /**
217273 * Generate a new wallet
218274 * 1. Creates the user keychain locally on the client, and encrypts it with the provided passphrase
@@ -246,7 +302,7 @@ export class Wallets implements IWallets {
246302 */
247303 async generateWallet (
248304 params : GenerateWalletOptions = { }
249- ) : Promise < WalletWithKeychains | LightningWalletWithKeychains > {
305+ ) : Promise < WalletWithKeychains | LightningWalletWithKeychains | GoAccountWalletWithUserKeychain > {
250306 // Assign the default multiSig type value based on the coin
251307 if ( ! params . multisigType ) {
252308 params . multisigType = this . baseCoin . getDefaultMultisigType ( ) ;
@@ -270,6 +326,26 @@ export class Wallets implements IWallets {
270326 return walletData ;
271327 }
272328
329+ // Go Account wallet generation
330+ if ( this . baseCoin . getFamily ( ) === 'ofc' && params . type === 'trading' ) {
331+ console . log ( 'generateWallet Go Account wallet' ) ;
332+ const options = decodeOrElse (
333+ GenerateGoAccountWalletOptionsCodec . name ,
334+ GenerateGoAccountWalletOptionsCodec ,
335+ params ,
336+ ( errors ) => {
337+ throw new Error ( `error(s) parsing generate go account request params: ${ errors } ` ) ;
338+ }
339+ ) ;
340+
341+ const walletData = await this . generateGoAccountWallet ( options ) ;
342+ walletData . encryptedWalletPassphrase = this . bitgo . encrypt ( {
343+ input : options . passphrase ,
344+ password : options . passcodeEncryptionCode ,
345+ } ) ;
346+ return walletData ;
347+ }
348+
273349 common . validateParams ( params , [ 'label' ] , [ 'passphrase' , 'userKey' , 'backupXpub' ] ) ;
274350 if ( typeof params . label !== 'string' ) {
275351 throw new Error ( 'missing required string parameter label' ) ;
0 commit comments