Skip to content

Commit 18241f0

Browse files
Merge pull request #9318 from BitGo/WCN-1539
feat: set the initialize safe codec properly
2 parents c7c5671 + 64d8b5d commit 18241f0

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

modules/sdk-core/src/bitgo/safe/iSafes.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* @experimental The safe client surface is experimental and may change (including breaking
55
* changes) before the public release.
66
*/
7+
import type { InitializeSafeResponse } from '@bitgo/public-types';
78
import { FinalizeSafeOptions, InitializeSafeOptions } from './iSafe';
89
import { Safe } from './safe';
910

@@ -62,10 +63,12 @@ export interface ISafes {
6263
*/
6364
generateSafe(params: CreateSafeOptions): Promise<Safe>;
6465
/**
65-
* Phase 1 — initialize a safe (metadata only, no key material).
66+
* Phase 1 — initialize a safe (metadata only, no key material). The server response is just
67+
* `{ id, status }` (no `label`/`enterpriseId`/`creator`/`users`/`createdAt` yet), so this
68+
* returns that raw shape rather than a full `Safe`.
6669
* @experimental
6770
*/
68-
initializeSafe(params: InitializeSafeOptions): Promise<Safe>;
71+
initializeSafe(params: InitializeSafeOptions): Promise<InitializeSafeResponse>;
6972
/**
7073
* Phase 2 — run the 4 root key ceremonies tagged with `safeId`; returns the 12 minted key ids.
7174
* @experimental

modules/sdk-core/src/bitgo/safe/safes.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
* changes) before the public release.
66
*/
77
import * as t from 'io-ts';
8-
import { FinalizeSafeBody, InitializeSafeBody, RootKeyTriplet, RootKeyType, SafeData } from '@bitgo/public-types';
8+
import {
9+
FinalizeSafeBody,
10+
InitializeSafeBody,
11+
InitializeSafeResponse,
12+
RootKeyTriplet,
13+
RootKeyType,
14+
SafeData,
15+
} from '@bitgo/public-types';
916
import { Environments } from '../../common';
1017
import { IBaseCoin } from '../baseCoin';
1118
import { BitGoBase } from '../bitgoBase';
@@ -87,19 +94,19 @@ export class Safes implements ISafes {
8794
*/
8895
async generateSafe(params: CreateSafeOptions): Promise<Safe> {
8996
const safe = await this.initializeSafe({ label: params.label });
90-
const rootKeys = await this.createSafeKeys({ ...params, safeId: safe.id() });
91-
return await this.finalizeSafe(safe.id(), rootKeys);
97+
const rootKeys = await this.createSafeKeys({ ...params, safeId: safe.id });
98+
return await this.finalizeSafe(safe.id, rootKeys);
9299
}
93100

94101
/**
95102
* Phase 1 — initialize a safe (metadata only, no key material).
96103
* POST /api/v2/enterprise/:eId/safes { label }
104+
* Response is just `{ id, status }` — the safe has no label/roster/etc. yet.
97105
* @experimental
98106
*/
99-
async initializeSafe(params: InitializeSafeOptions): Promise<Safe> {
107+
async initializeSafe(params: InitializeSafeOptions): Promise<InitializeSafeResponse> {
100108
const response = await postWithCodec(this.bitgo, this.url(), InitializeSafeBody, params).result();
101-
const safeData = decodeWithCodec(SafeData, response, 'SafeData');
102-
return new Safe(this.bitgo, safeData);
109+
return decodeWithCodec(InitializeSafeResponse, response, 'InitializeSafeResponse');
103110
}
104111

105112
/**

modules/sdk-core/test/unit/bitgo/safe/safes.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ describe('Safes', function () {
2828
});
2929

3030
describe('initializeSafe', function () {
31-
it('POSTs to the safes collection URL and returns a Safe', async function () {
32-
const send = sinon.stub().returns({ result: sinon.stub().resolves(safeDataWire) });
31+
it('POSTs to the safes collection URL and returns the initializing id/status', async function () {
32+
const initializeResponseWire = { id: 'test-safe-id', status: 'initializing' };
33+
const send = sinon.stub().returns({ result: sinon.stub().resolves(initializeResponseWire) });
3334
mockBitGo.post.returns({ send });
3435

3536
const result = await safes.initializeSafe({ label: 'my safe' });
3637

37-
result.should.be.instanceof(Safe);
38-
result.id().should.equal('test-safe-id');
39-
result.enterpriseId().should.equal('test-enterprise-id');
38+
result.id.should.equal('test-safe-id');
39+
result.status.should.equal('initializing');
4040
sinon.assert.calledWith(mockBitGo.post, '/enterprise/test-enterprise-id/safes');
4141
sinon.assert.calledWith(send, { label: 'my safe' });
4242
});
@@ -237,7 +237,7 @@ describe('Safes', function () {
237237

238238
describe('generateSafe', function () {
239239
it('chains initialize → createSafeKeys → finalize, threading the safeId', async function () {
240-
const initializing = new Safe(mockBitGo, { ...safeDataWire, status: 'initializing' } as any);
240+
const initializing = { id: 'test-safe-id', status: 'initializing' as const };
241241
const rootKeys = { rootKeys: { hot: {} } } as any;
242242
const initStub = sinon.stub(safes, 'initializeSafe').resolves(initializing);
243243
const keysStub = sinon.stub(safes, 'createSafeKeys').resolves(rootKeys);

0 commit comments

Comments
 (0)