Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"debug": "^3.1.0",
"dotenv": "^16.0.0",
"express": "4.21.2",
"io-ts-types": "^0.5.16",
"io-ts": "npm:@bitgo-forks/[email protected]",
"lodash": "^4.17.20",
"morgan": "^1.9.1",
Expand Down
8 changes: 4 additions & 4 deletions modules/express/src/clientRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,11 +646,11 @@ export async function handleV2OFCSignPayload(req: express.Request): Promise<{ pa
* handle new wallet creation
* @param req
*/
export async function handleV2GenerateWallet(req: express.Request) {
export async function handleV2GenerateWallet(req: ExpressApiRouteRequest<'express.wallet.generate', 'post'>) {
const bitgo = req.bitgo;
const coin = bitgo.coin(req.params.coin);
const coin = bitgo.coin(req.decoded.coin);
const result = await coin.wallets().generateWallet(req.body);
if (req.query.includeKeychains === 'false') {
if ((req.decoded.includeKeychains as any) === false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we casting to any here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because includeKeychains is type BooleanFromString which has output type string so statically it is typed as a string but during runtime it is actually a boolean. I think this is because of how we setup the decoder.

Also includeKeychains is a parameter in the request and httpRequest requires that params are of type string

return result.wallet.toJSON();
}
return { ...result, wallet: result.wallet.toJSON() };
Expand Down Expand Up @@ -1614,7 +1614,7 @@ export function setupAPIRoutes(app: express.Application, config: Config): void {
router.post('express.keychain.local', [prepareBitGo(config), typedPromiseWrapper(handleV2CreateLocalKeyChain)]);

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

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

Expand Down
4 changes: 4 additions & 0 deletions modules/express/src/typedRoutes/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { PostLightningInitWallet } from './v2/lightningInitWallet';
import { PostUnlockLightningWallet } from './v2/unlockWallet';
import { PostVerifyCoinAddress } from './v2/verifyAddress';
import { PostDeriveLocalKeyChain } from './v1/deriveLocalKeyChain';
import { PostGenerateWallet } from './v2/generateWallet';
import { PostCreateLocalKeyChain } from './v1/createLocalKeyChain';
import { PutConstructPendingApprovalTx } from './v1/constructPendingApprovalTx';
import { PutConsolidateUnspents } from './v1/consolidateUnspents';
Expand Down Expand Up @@ -69,6 +70,9 @@ export const ExpressApi = apiSpec({
'express.verifycoinaddress': {
post: PostVerifyCoinAddress,
},
'express.wallet.generate': {
post: PostGenerateWallet,
},
'express.calculateminerfeeinfo': {
post: PostCalculateMinerFeeInfo,
},
Expand Down
115 changes: 115 additions & 0 deletions modules/express/src/typedRoutes/api/v2/generateWallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import * as t from 'io-ts';
import { BooleanFromString } from 'io-ts-types';
import { httpRoute, httpRequest, optional } from '@api-ts/io-ts-http';
import { BitgoExpressError } from '../../schemas/error';
import { UserKeychainCodec, BackupKeychainCodec, BitgoKeychainCodec } from '../../schemas/keychain';
import { multisigType, walletType } from '../../schemas/wallet';

/**
* Request body for wallet generation.
*/
export const GenerateWalletBody = {
/** Wallet label */
label: t.string,
/** Enterprise id. This is required for Ethereum wallets since they can only be created as part of an enterprise */
enterprise: t.string,
/** If absent, BitGo uses the default wallet type for the asset */
multisigType: multisigType,
/** The type of wallet, defined by key management and signing protocols. 'hot' and 'cold' are both self-managed wallets. If absent, defaults to 'hot' */
type: walletType,
/** Passphrase to be used to encrypt the user key on the wallet */
passphrase: optional(t.string),
/** User provided public key */
userKey: optional(t.string),
/** Backup extended public key */
backupXpub: optional(t.string),
/** Optional key recovery service to provide and store the backup key */
backupXpubProvider: optional(t.literal('dai')),
/** Flag for disabling wallet transaction notifications */
disableTransactionNotifications: optional(t.boolean),
/** The passphrase used for decrypting the encrypted wallet passphrase during wallet recovery */
passcodeEncryptionCode: optional(t.string),
/** Seed that derives an extended user key or common keychain for a cold wallet */
coldDerivationSeed: optional(t.string),
/** Gas price to use when deploying an Ethereum wallet */
gasPrice: optional(t.number),
/** Flag for preventing KRS from sending email after creating backup key */
disableKRSEmail: optional(t.boolean),
/** (ETH only) Specify the wallet creation contract version used when creating a wallet contract */
walletVersion: optional(t.number),
/** True, if the wallet type is a distributed-custodial. If passed, you must also pass the 'enterprise' parameter */
isDistributedCustody: optional(t.boolean),
/** BitGo key ID for self-managed cold MPC wallets */
bitgoKeyId: optional(t.string),
/** Common keychain for self-managed cold MPC wallets */
commonKeychain: optional(t.string),
} as const;

export const GenerateWalletResponse200 = t.union([
t.UnknownRecord,
t.type({
wallet: t.UnknownRecord,
encryptedWalletPassphrase: optional(t.string),
userKeychain: optional(UserKeychainCodec),
backupKeychain: optional(BackupKeychainCodec),
bitgoKeychain: optional(BitgoKeychainCodec),
warning: optional(t.string),
}),
]);

/**
* Response body for wallet generation.
*/
export const GenerateWalletResponse = {
/** The newly created wallet */
200: GenerateWalletResponse200,
/** Bad request */
400: BitgoExpressError,
} as const;

/**
* Path parameters for wallet generation.
*/
export const GenerateWalletV2Params = {
/** Coin ticker / chain identifier */
coin: t.string,
};

/**
* Query parameters for wallet generation.
* @property includeKeychains - Include user, backup and bitgo keychains along with generated wallet
*/
export const GenerateWalletV2Query = {
/** Include user, backup and bitgo keychains along with generated wallet */
includeKeychains: optional(BooleanFromString),
};

/**
* Generate Wallet
*
* This API call creates a new wallet. Under the hood, the SDK (or BitGo Express) does the following:
*
* 1. Creates the user keychain locally on the machine, and encrypts it with the provided passphrase (skipped if userKey is provided).
* 2. Creates the backup keychain locally on the machine.
* 3. Uploads the encrypted user keychain and public backup keychain.
* 4. Creates the BitGo key (and the backup key if backupXpubProvider is set) on the service.
* 5. Creates the wallet on BitGo with the 3 public keys above.
*
* ⓘ 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.
*
* ⓘ 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.
*
* ⓘ This endpoint should be called through BitGo Express if used without the SDK, such as when using cURL.
*
* @operationId express.wallet.generate
*/
export const PostGenerateWallet = httpRoute({
path: '/api/v2/:coin/wallet/generate',
method: 'POST',
request: httpRequest({
params: GenerateWalletV2Params,
query: GenerateWalletV2Query,
body: GenerateWalletBody,
}),
response: GenerateWalletResponse,
});
41 changes: 41 additions & 0 deletions modules/express/src/typedRoutes/schemas/keychain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as t from 'io-ts';

// Base keychain fields
const BaseKeychainCodec = t.type({
id: t.string,
pub: t.string,
source: t.string,
});

// User keychain: can have encryptedPrv and prv
export const UserKeychainCodec = t.intersection([
BaseKeychainCodec,
t.partial({
ethAddress: t.string,
coinSpecific: t.UnknownRecord,
encryptedPrv: t.string,
prv: t.string,
}),
]);

// Backup keychain: can have prv
export const BackupKeychainCodec = t.intersection([
BaseKeychainCodec,
t.partial({
ethAddress: t.string,
coinSpecific: t.UnknownRecord,
prv: t.string,
}),
]);

// BitGo keychain: must have isBitGo
export const BitgoKeychainCodec = t.intersection([
BaseKeychainCodec,
t.type({
isBitGo: t.boolean,
}),
t.partial({
ethAddress: t.string,
coinSpecific: t.UnknownRecord,
}),
]);
5 changes: 5 additions & 0 deletions modules/express/src/typedRoutes/schemas/wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as t from 'io-ts';

export const multisigType = t.union([t.literal('onchain'), t.literal('tss'), t.literal('blsdkg')]);

export const walletType = t.union([t.literal('hot'), t.literal('cold'), t.literal('custodial')]);
Loading