Skip to content

Commit 16a0cd0

Browse files
committed
refactor: migrated generateWallet to typed routes
added keychain codecs to a general codec file, as I assume that these codecs could be reused (but not sure if they will). refactored the UT that covered this generateWallet to ensure that encoding and decoding works e2e. TICKET: WP-5415
1 parent 487e2b7 commit 16a0cd0

File tree

5 files changed

+333
-37
lines changed

5 files changed

+333
-37
lines changed

modules/express/src/clientRoutes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ export async function handleV2OFCSignPayload(req: express.Request): Promise<{ pa
645645
* handle new wallet creation
646646
* @param req
647647
*/
648-
export async function handleV2GenerateWallet(req: express.Request) {
648+
export async function handleV2GenerateWallet(req: ExpressApiRouteRequest<'express.wallet.generate', 'post'>) {
649649
const bitgo = req.bitgo;
650650
const coin = bitgo.coin(req.params.coin);
651651
const result = await coin.wallets().generateWallet(req.body);
@@ -1617,7 +1617,7 @@ export function setupAPIRoutes(app: express.Application, config: Config): void {
16171617
);
16181618

16191619
// generate wallet
1620-
app.post('/api/v2/:coin/wallet/generate', parseBody, prepareBitGo(config), promiseWrapper(handleV2GenerateWallet));
1620+
router.post('express.wallet.generate', [prepareBitGo(config), typedPromiseWrapper(handleV2GenerateWallet)]);
16211621

16221622
app.put('/express/api/v2/:coin/wallet/:id', parseBody, prepareBitGo(config), promiseWrapper(handleWalletUpdate));
16231623

modules/express/src/typedRoutes/api/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { PostSimpleCreate } from './v1/simpleCreate';
1313
import { PutPendingApproval } from './v1/pendingApproval';
1414
import { PostSignTransaction } from './v1/signTransaction';
1515
import { PostVerifyCoinAddress } from './v2/verifyAddress';
16+
import { PostGenerateWallet } from './v2/generate';
1617

1718
export const ExpressApi = apiSpec({
1819
'express.ping': {
@@ -48,6 +49,9 @@ export const ExpressApi = apiSpec({
4849
'express.verifycoinaddress': {
4950
post: PostVerifyCoinAddress,
5051
},
52+
'express.wallet.generate': {
53+
post: PostGenerateWallet,
54+
},
5155
});
5256

5357
export type ExpressApi = typeof ExpressApi;
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import * as t from 'io-ts';
2+
import { httpRoute, httpRequest, optional } from '@api-ts/io-ts-http';
3+
import { BitgoExpressError } from '../../schemas/error';
4+
import { UserKeychainCodec, BackupKeychainCodec, BitgoKeychainCodec } from '../../../wallet/codec';
5+
6+
/**
7+
* Request body for wallet generation.
8+
*/
9+
export const GenerateWalletBody = {
10+
/** Wallet label */
11+
label: t.string,
12+
/** Enterprise id. This is required for Ethereum wallets since they can only be created as part of an enterprise */
13+
enterprise: t.string,
14+
/** If absent, BitGo uses the default wallet type for the asset */
15+
multisigType: optional(t.union([t.literal('onchain'), t.literal('tss'), t.literal('blsdkg')])),
16+
/** The type of wallet, defined by key management and signing protocols. 'hot' and 'cold' are both self-managed wallets. If absent, defaults to 'hot' */
17+
type: optional(t.union([t.literal('hot'), t.literal('cold'), t.literal('custodial')])),
18+
/** Passphrase to be used to encrypt the user key on the wallet */
19+
passphrase: optional(t.string),
20+
/** User provided public key */
21+
userKey: optional(t.string),
22+
/** Backup extended public key */
23+
backupXpub: optional(t.string),
24+
/** Optional key recovery service to provide and store the backup key */
25+
backupXpubProvider: optional(t.literal('dai')),
26+
/** Flag for disabling wallet transaction notifications */
27+
disableTransactionNotifications: optional(t.boolean),
28+
/** The passphrase used for decrypting the encrypted wallet passphrase during wallet recovery */
29+
passcodeEncryptionCode: optional(t.string),
30+
/** Seed that derives an extended user key or common keychain for a cold wallet */
31+
coldDerivationSeed: optional(t.string),
32+
/** Gas price to use when deploying an Ethereum wallet */
33+
gasPrice: optional(t.number),
34+
/** Flag for preventing KRS from sending email after creating backup key */
35+
disableKRSEmail: optional(t.boolean),
36+
/** (ETH only) Specify the wallet creation contract version used when creating a wallet contract */
37+
walletVersion: optional(t.number),
38+
/** True, if the wallet type is a distributed-custodial. If passed, you must also pass the 'enterprise' parameter */
39+
isDistributedCustody: optional(t.boolean),
40+
/** BitGo key ID for self-managed cold MPC wallets */
41+
bitgoKeyId: optional(t.string),
42+
/** Common keychain for self-managed cold MPC wallets */
43+
commonKeychain: optional(t.string),
44+
} as const;
45+
46+
export const GenerateWalletResponse200 = t.union([
47+
t.UnknownRecord,
48+
t.type({
49+
wallet: t.UnknownRecord,
50+
encryptedWalletPassphrase: optional(t.string),
51+
userKeychain: optional(UserKeychainCodec),
52+
backupKeychain: optional(BackupKeychainCodec),
53+
bitgoKeychain: optional(BitgoKeychainCodec),
54+
warning: optional(t.string),
55+
}),
56+
]);
57+
58+
/**
59+
* Response body for wallet generation.
60+
*/
61+
export const GenerateWalletResponse = {
62+
/** The newly created wallet */
63+
200: GenerateWalletResponse200,
64+
/** Bad request */
65+
400: BitgoExpressError,
66+
} as const;
67+
68+
/**
69+
* Path parameters for wallet generation.
70+
*/
71+
export const GenerateWalletV2Params = {
72+
/** Coin ticker / chain identifier */
73+
coin: t.string,
74+
};
75+
76+
/**
77+
* Query parameters for wallet generation.
78+
* @property includeKeychains - Include user, backup and bitgo keychains along with generated wallet
79+
*/
80+
export const GenerateWalletV2Query = {
81+
/** Include user, backup and bitgo keychains along with generated wallet */
82+
includeKeychains: optional(t.string),
83+
};
84+
85+
/**
86+
* Generate Wallet
87+
*
88+
* This API call creates a new wallet. Under the hood, the SDK (or BitGo Express) does the following:
89+
*
90+
* 1. Creates the user keychain locally on the machine, and encrypts it with the provided passphrase (skipped if userKey is provided).
91+
* 2. Creates the backup keychain locally on the machine.
92+
* 3. Uploads the encrypted user keychain and public backup keychain.
93+
* 4. Creates the BitGo key (and the backup key if backupXpubProvider is set) on the service.
94+
* 5. Creates the wallet on BitGo with the 3 public keys above.
95+
*
96+
* ⓘ Ethereum wallets can only be created under an enterprise. Pass in the id of the enterprise to associate the wallet with. Your enterprise id can be seen by clicking on the "Manage Organization" link on the enterprise dropdown. Each enterprise has a fee address which will be used to pay for transaction fees on all Ethereum wallets in that enterprise. The fee address is displayed in the dashboard of the website, please fund it before creating a wallet.
97+
*
98+
* ⓘ You cannot generate a wallet by passing in a subtoken as the coin. Subtokens share wallets with their parent coin and it is not possible to create a wallet specific to one token.
99+
*
100+
* ⓘ This endpoint should be called through BitGo Express if used without the SDK, such as when using cURL.
101+
*
102+
* @operationId express.wallet.generate
103+
*/
104+
export const PostGenerateWallet = httpRoute({
105+
path: '/api/v2/:coin/wallet/generate',
106+
method: 'POST',
107+
request: httpRequest({
108+
params: GenerateWalletV2Params,
109+
query: GenerateWalletV2Query,
110+
body: GenerateWalletBody,
111+
}),
112+
response: GenerateWalletResponse,
113+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as t from 'io-ts';
2+
3+
// Base keychain fields
4+
const BaseKeychainCodec = t.type({
5+
id: t.string,
6+
pub: t.string,
7+
source: t.string,
8+
});
9+
10+
// User keychain: can have encryptedPrv and prv
11+
export const UserKeychainCodec = t.intersection([
12+
BaseKeychainCodec,
13+
t.partial({
14+
ethAddress: t.string,
15+
coinSpecific: t.UnknownRecord,
16+
encryptedPrv: t.string,
17+
prv: t.string,
18+
}),
19+
]);
20+
21+
// Backup keychain: can have prv
22+
export const BackupKeychainCodec = t.intersection([
23+
BaseKeychainCodec,
24+
t.partial({
25+
ethAddress: t.string,
26+
coinSpecific: t.UnknownRecord,
27+
prv: t.string,
28+
}),
29+
]);
30+
31+
// BitGo keychain: must have isBitGo
32+
export const BitgoKeychainCodec = t.intersection([
33+
BaseKeychainCodec,
34+
t.type({
35+
isBitGo: t.boolean,
36+
}),
37+
t.partial({
38+
ethAddress: t.string,
39+
coinSpecific: t.UnknownRecord,
40+
}),
41+
]);

0 commit comments

Comments
 (0)