|
| 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 { CreateAddressFormat } from '../../schemas/address'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Path parameters for locally deriving a wallet address |
| 8 | + */ |
| 9 | +export const DeriveAddressParams = { |
| 10 | + /** Blockchain identifier (e.g., 'btc', 'eth', 'tbtc', 'teth', 'sol') */ |
| 11 | + coin: t.string, |
| 12 | +} as const; |
| 13 | + |
| 14 | +/** |
| 15 | + * A keychain entry for local derivation. Public key material only — no private keys. |
| 16 | + * Modelled as a union so a keychain must carry at least one of `pub` / `commonKeychain`: |
| 17 | + * - `pub` (xpub) for BIP32 multisig coins (UTXO, legacy EVM) |
| 18 | + * - `commonKeychain` for TSS/MPC coins (SOL, EVM MPC) — identical across keychains |
| 19 | + * |
| 20 | + * (A keychain may legitimately carry both; TSS keychains commonly do.) |
| 21 | + */ |
| 22 | +export const DeriveAddressKeychainCodec = t.union([t.type({ pub: t.string }), t.type({ commonKeychain: t.string })]); |
| 23 | + |
| 24 | +/** |
| 25 | + * Request body for locally deriving a wallet receive address |
| 26 | + */ |
| 27 | +export const DeriveAddressBody = { |
| 28 | + /** |
| 29 | + * Keychains for derivation (public key material only). |
| 30 | + * BIP32 multisig: the user/backup/bitgo xpub triple via `pub`. |
| 31 | + * TSS/MPC: the `commonKeychain`. |
| 32 | + */ |
| 33 | + keychains: t.array(DeriveAddressKeychainCodec), |
| 34 | + /** Derivation index for the address (caller-supplied; the endpoint is stateless) */ |
| 35 | + index: t.number, |
| 36 | + /** Derivation chain code: UTXO script-type / external(0) vs internal(1) selector */ |
| 37 | + chain: optional(t.number), |
| 38 | + /** Address format override (e.g. 'p2sh', 'p2wsh' for UTXO; 'cashaddr' / 'base58') */ |
| 39 | + format: optional(CreateAddressFormat), |
| 40 | + /** Wallet version, to disambiguate derivation strategy (e.g. EVM forwarder vs MPC) */ |
| 41 | + walletVersion: optional(t.number), |
| 42 | + /** |
| 43 | + * Seed from the user keychain's derivedFromParentWithSeed field (SMC TSS wallets); |
| 44 | + * makes the derivation path `{prefix}/{index}` instead of `m/{index}`. |
| 45 | + */ |
| 46 | + derivedFromParentWithSeed: optional(t.string), |
| 47 | +} as const; |
| 48 | + |
| 49 | +/** |
| 50 | + * Response for locally deriving a wallet address |
| 51 | + */ |
| 52 | +export const DeriveAddressResponse = { |
| 53 | + /** The derived address and related derivation info */ |
| 54 | + 200: t.intersection([ |
| 55 | + t.type({ |
| 56 | + /** The derived address */ |
| 57 | + address: t.string, |
| 58 | + /** The derivation index used */ |
| 59 | + index: t.number, |
| 60 | + }), |
| 61 | + t.partial({ |
| 62 | + /** The derivation chain code used */ |
| 63 | + chain: t.number, |
| 64 | + /** Coin-specific address data (e.g. redeemScript/witnessScript for UTXO) */ |
| 65 | + coinSpecific: t.UnknownRecord, |
| 66 | + /** The HD derivation path actually used */ |
| 67 | + derivationPath: t.string, |
| 68 | + }), |
| 69 | + ]), |
| 70 | + /** Invalid request parameters or derivation failed */ |
| 71 | + 400: BitgoExpressError, |
| 72 | +} as const; |
| 73 | + |
| 74 | +/** |
| 75 | + * Locally derive and return a wallet receive address from a derivation path. |
| 76 | + * |
| 77 | + * Unlike `iswalletaddress` (which checks a candidate address), this *produces* the address |
| 78 | + * offline from public key material only — the xpub triple for BIP32 multisig coins, or the |
| 79 | + * commonKeychain for TSS/MPC coins. No private keys, no wallet lookup, and no network access: |
| 80 | + * the handler operates purely on the request body and can run in an air-gapped Express. |
| 81 | + * |
| 82 | + * Pairs with `iswalletaddress` for a derive→verify round-trip: derive the address here, then |
| 83 | + * verify it against the same keychains to independently confirm correctness. |
| 84 | + * |
| 85 | + * @operationId express.v2.address.derive |
| 86 | + * @tag Express |
| 87 | + */ |
| 88 | +export const PostDeriveAddress = httpRoute({ |
| 89 | + path: '/api/v2/{coin}/address/derive', |
| 90 | + method: 'POST', |
| 91 | + request: httpRequest({ |
| 92 | + params: DeriveAddressParams, |
| 93 | + body: DeriveAddressBody, |
| 94 | + }), |
| 95 | + response: DeriveAddressResponse, |
| 96 | +}); |
0 commit comments