|
| 1 | +/** |
| 2 | + * @prettier |
| 3 | + * |
| 4 | + * @experimental The safe client surface is experimental and may change (including breaking |
| 5 | + * changes) before the public release. |
| 6 | + */ |
| 7 | +import { FreezeSafeBody, SafeData, SafeShareData, SafeShareState } from '@bitgo/public-types'; |
| 8 | +import { BitGoBase } from '../bitgoBase'; |
| 9 | +import { decodeWithCodec } from '../utils/codecs'; |
| 10 | +import { postWithCodec } from '../utils/postWithCodec'; |
| 11 | +import { Wallet } from '../wallet'; |
| 12 | +import { |
| 13 | + AcceptSafeShareOptions, |
| 14 | + AddSafeMemberOptions, |
| 15 | + AddSafeWalletMemberOptions, |
| 16 | + CreateSafeWalletOptions, |
| 17 | + ISafe, |
| 18 | + WalletShareData, |
| 19 | +} from './iSafe'; |
| 20 | + |
| 21 | +/** |
| 22 | + * @experimental |
| 23 | + */ |
| 24 | +export class Safe implements ISafe { |
| 25 | + private readonly bitgo: BitGoBase; |
| 26 | + public readonly _safe: SafeData; |
| 27 | + |
| 28 | + constructor(bitgo: BitGoBase, safeData: SafeData) { |
| 29 | + this.bitgo = bitgo; |
| 30 | + this._safe = safeData; |
| 31 | + } |
| 32 | + |
| 33 | + id(): string { |
| 34 | + return this._safe.id; |
| 35 | + } |
| 36 | + |
| 37 | + enterpriseId(): string { |
| 38 | + return this._safe.enterpriseId; |
| 39 | + } |
| 40 | + |
| 41 | + label(): string { |
| 42 | + return this._safe.label; |
| 43 | + } |
| 44 | + |
| 45 | + status(): SafeData['status'] { |
| 46 | + return this._safe.status; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Enterprise-scoped v2 URL for this safe, e.g. /api/v2/enterprise/:eId/safes/:safeId |
| 51 | + * @param extra |
| 52 | + */ |
| 53 | + url(extra = ''): string { |
| 54 | + return this.bitgo.url(`/enterprise/${this.enterpriseId()}/safes/${this.id()}${extra}`, 2); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Mint a child wallet in this safe (server-side public derivation — no ceremony). |
| 59 | + * Body lands in WCN-1203. |
| 60 | + */ |
| 61 | + async createWallet(params: CreateSafeWalletOptions): Promise<Wallet> { |
| 62 | + throw new Error('Safe.createWallet is not yet implemented (WCN-1203)'); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Add a member to the whole safe (view/admin/spend). Spend opens a key share. |
| 67 | + * Body lands in WCN-1204. |
| 68 | + */ |
| 69 | + async addMember(params: AddSafeMemberOptions): Promise<SafeData> { |
| 70 | + throw new Error('Safe.addMember is not yet implemented (WCN-1204)'); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Share ONE safe wallet with a non-member via the existing wallet-share handshake (FR-13). |
| 75 | + * Body lands in WCN-1204. |
| 76 | + */ |
| 77 | + async addMemberToWallet(params: AddSafeWalletMemberOptions): Promise<WalletShareData> { |
| 78 | + throw new Error('Safe.addMemberToWallet is not yet implemented (WCN-1204)'); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * List the safe key shares visible to the caller. |
| 83 | + * Body lands in WCN-1204. |
| 84 | + */ |
| 85 | + async listShares(params: { state?: SafeShareState } = {}): Promise<SafeShareData[]> { |
| 86 | + throw new Error('Safe.listShares is not yet implemented (WCN-1204)'); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Accept a safe key share addressed to the caller. |
| 91 | + * Body lands in WCN-1204. |
| 92 | + */ |
| 93 | + async acceptShare(params: AcceptSafeShareOptions): Promise<SafeShareData> { |
| 94 | + throw new Error('Safe.acceptShare is not yet implemented (WCN-1204)'); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Freeze the safe — blocks withdrawals on all safe wallets. Safe stays 'active'. |
| 99 | + * @param params |
| 100 | + */ |
| 101 | + async freeze(params: FreezeSafeBody = {}): Promise<SafeData> { |
| 102 | + const response = await postWithCodec(this.bitgo, this.url('/freeze'), FreezeSafeBody, params).result(); |
| 103 | + return decodeWithCodec(SafeData, response, 'SafeData'); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Archive the safe. Requires every safe wallet to already be archived; also the abandonment |
| 108 | + * path for a stuck 'initializing' safe. |
| 109 | + */ |
| 110 | + async archive(): Promise<SafeData> { |
| 111 | + const response = await this.bitgo.post(this.url('/archive')).send().result(); |
| 112 | + return decodeWithCodec(SafeData, response, 'SafeData'); |
| 113 | + } |
| 114 | + |
| 115 | + toJSON(): SafeData { |
| 116 | + return this._safe; |
| 117 | + } |
| 118 | +} |
0 commit comments