Skip to content
Draft
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
5 changes: 5 additions & 0 deletions modules/express/src/clientRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { logger } from '@bitgo/logger';
import {
CommitmentShareRecord,
CreateNetworkConnectionParams,
CreateSafeOptions,
CustomCommitmentGeneratingFunction,
CustomGShareGeneratingFunction,
CustomKShareGeneratingFunction,
Expand Down Expand Up @@ -837,6 +838,9 @@ export async function handleV2GenerateSafe(
label: req.decoded.label,
passphrase: req.decoded.passphrase,
...(req.decoded.multisigRoots === undefined ? {} : { multisigRoots: req.decoded.multisigRoots }),
...(req.decoded.backupKeyProvider === undefined
? {}
: { backupKeyProvider: req.decoded.backupKeyProvider as CreateSafeOptions['backupKeyProvider'] }),
});
return safe.toJSON();
}
Expand Down Expand Up @@ -869,6 +873,7 @@ export async function handleV2GenerateSafeKeys(req: ExpressApiRouteRequest<'expr
safeId: req.decoded.safeId,
enabledRootSlots: req.decoded.enabledRootSlots,
multisigRoots: req.decoded.multisigRoots,
backupKeyProvider: req.decoded.backupKeyProvider as CreateSafeOptions['backupKeyProvider'],
externalBackupSlots: req.decoded.externalBackupSlots,
});
// The root triplets are client-side keycard material (encrypted private keys); the express
Expand Down
24 changes: 24 additions & 0 deletions modules/express/src/typedRoutes/api/v2/generateSafe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as t from 'io-ts';
import { httpRoute, httpRequest, optional } from '@api-ts/io-ts-http';
import { MultisigRootKeyType } from '@bitgo/public-types';
import { BitgoExpressError } from '../../schemas/error';

/**
Expand All @@ -21,6 +22,24 @@ export const SafeMultisigRoots = t.partial({
ed25519Multisig: SafeMultisigRootKeyOptions,
});

/**
* KRS-held backup roots: the provider mints and custodies the backup root for the named multisig
* slots; the SDK never sees its private key. Mutually exclusive with `multisigRoots` — rejected in
* `sdk-core` before the safe is created. `slots` defaults to both multisig slots.
*/
export const SafeBackupKeyProvider = t.intersection([
t.type({
/** Registered KRS provider name, e.g. `coincover` */
provider: t.string,
}),
// `optional()` inside `t.type` yields a required key typed `| undefined`, which a JSON body
// cannot carry; `t.partial` makes `slots` genuinely absent-able.
t.partial({
/** Multisig slots whose backup root the provider holds; defaults to both multisig slots */
slots: t.array(MultisigRootKeyType),
}),
]);

/**
* Path parameters for one-shot Safe generation.
*/
Expand All @@ -45,6 +64,11 @@ export const GenerateSafeBody = {
* initialize, so it also needs the external backup root feature enabled for the enterprise.
*/
multisigRoots: optional(SafeMultisigRoots),
/**
* KRS-held backup roots. A provider is used instead of `multisigRoots`, never with it; `slots`
* defaults to both multisig slots.
*/
backupKeyProvider: optional(SafeBackupKeyProvider),
} as const;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as t from 'io-ts';
import { httpRoute, httpRequest, optional } from '@api-ts/io-ts-http';
import { MultisigRootKeyType, RootKeyType } from '@bitgo/public-types';
import { BitgoExpressError } from '../../schemas/error';
import { SafeMultisigRoots } from './generateSafe';
import { SafeBackupKeyProvider, SafeMultisigRoots } from './generateSafe';

/**
* Path parameters for Phase-2-only Safe key ceremonies.
Expand All @@ -26,6 +26,8 @@ export const GenerateSafeKeysBody = {
enabledRootSlots: optional(t.array(RootKeyType)),
/** Bring-your-own backup key per multisig slot. Must match `externalBackupSlots` exactly. */
multisigRoots: optional(SafeMultisigRoots),
/** KRS-held backup roots for the external slots. Mutually exclusive with `multisigRoots`. */
backupKeyProvider: optional(SafeBackupKeyProvider),
/** The safe's external backup slots as returned by initialize. Omit when every backup root is local. */
externalBackupSlots: optional(t.array(MultisigRootKeyType)),
} as const;
Expand Down
65 changes: 65 additions & 0 deletions modules/express/test/unit/typedRoutes/generateSafe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,55 @@ describe('Generate Safe Typed Routes Tests', function () {
generateSafeStub.firstCall.args[0].should.deepEqual({ label, passphrase, multisigRoots });
});

it('should forward backupKeyProvider (KRS-held backup root) to the SDK', async function () {
const label = 'Test Safe';
const passphrase = 'mySecurePassphrase123';
const backupKeyProvider = { provider: 'coincover', slots: ['secp256k1Multisig'] };
const generateSafeStub = sinon
.stub(Safes.prototype, 'generateSafe')
.resolves({ safe: stubSafe({ toJSON: () => mockSafeJson(label) }), rootKeychains: {} });

const res = await agent
.post(`/api/v2/enterprise/${ENTERPRISE_ID}/safes/generate`)
.send({ label, passphrase, backupKeyProvider });

res.status.should.equal(200);
generateSafeStub.should.have.been.calledOnce();
generateSafeStub.firstCall.args[0].should.deepEqual({ label, passphrase, backupKeyProvider });
});

it('should return 400 when backupKeyProvider.slots contains an MPC slot', async function () {
const res = await agent.post(`/api/v2/enterprise/${ENTERPRISE_ID}/safes/generate`).send({
label: 'Test Safe',
passphrase: 'password',
backupKeyProvider: { provider: 'coincover', slots: ['ecdsaMpc'] },
});

res.status.should.equal(400);
res.body.should.have.property('error');
});

it('should propagate the backupKeyProvider + multisigRoots mutual-exclusion rejection', async function () {
sinon
.stub(Safes.prototype, 'generateSafe')
.rejects(
new Error(
'backupKeyProvider and multisigRoots are mutually exclusive: the backup roots are either KRS-held or caller-supplied, not both'
)
);

const res = await agent.post(`/api/v2/enterprise/${ENTERPRISE_ID}/safes/generate`).send({
label: 'Test Safe',
passphrase: 'password',
multisigRoots: { secp256k1Multisig: { backupXpub: 'xpub-backup' } },
backupKeyProvider: { provider: 'coincover' },
});

res.status.should.equal(500);
res.body.should.have.property('error');
res.body.error.should.match(/mutually exclusive/);
});

it('should return 400 when a multisigRoots backupXpub is not a string', async function () {
const res = await agent.post(`/api/v2/enterprise/${ENTERPRISE_ID}/safes/generate`).send({
label: 'Test Safe',
Expand Down Expand Up @@ -312,6 +361,22 @@ describe('Generate Safe Typed Routes Tests', function () {
createSafeKeysStub.firstCall.args[0].externalBackupSlots?.should.deepEqual(['secp256k1Multisig']);
});

it('should forward backupKeyProvider to the SDK', async function () {
const backupKeyProvider = { provider: 'coincover', slots: ['secp256k1Multisig'] };
const createSafeKeysStub = sinon.stub(Safes.prototype, 'createSafeKeys').resolves(rootKeys);

const res = await agent.post(`/api/v2/enterprise/${ENTERPRISE_ID}/safes/${SAFE_ID}/keys/generate`).send({
passphrase: 'password',
backupKeyProvider,
externalBackupSlots: ['secp256k1Multisig'],
});

res.status.should.equal(200);
createSafeKeysStub.should.have.been.calledOnce();
createSafeKeysStub.firstCall.args[0].should.have.property('backupKeyProvider');
createSafeKeysStub.firstCall.args[0].backupKeyProvider?.should.deepEqual(backupKeyProvider);
});

it('should return 400 when externalBackupSlots contains an MPC slot', async function () {
const res = await agent.post(`/api/v2/enterprise/${ENTERPRISE_ID}/safes/${SAFE_ID}/keys/generate`).send({
passphrase: 'password',
Expand Down
5 changes: 5 additions & 0 deletions modules/sdk-core/src/bitgo/safe/iSafe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export interface InitializeSafeOptions {
* Omit when every backup root is local.
*/
externalBackupSlots?: MultisigRootKeyType[];
/**
* KRS provider holding the declared slots' backup roots, e.g. `coincover`. Omit when the
* external backup roots are caller-supplied (BYOK) or there are none.
*/
backupKeyProvider?: string;
}

/** @experimental */
Expand Down
21 changes: 21 additions & 0 deletions modules/sdk-core/src/bitgo/safe/iSafes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ export interface CreateSafeOptions {
* through it. Supply a caller-generated composite pub to control the chain code yourself.
*/
multisigRoots?: SafeMultisigRootKeys;
/**
* Backup root held by a key recovery service (KRS). Mutually exclusive with `multisigRoots`:
* a safe's backup roots are either generated locally (neither option), caller-supplied BYOK
* (`multisigRoots`), or KRS-held (`backupKeyProvider`) — never a mix.
*
* `slots` names the multisig slots whose backup root the provider holds and defaults to both
* multisig slots; the server rejects a slot the safe does not enable. `provider` must be a
* registered KRS provider (`krsProviders`).
*/
backupKeyProvider?: BackupKeyProviderOptions;
}

/**
* Options for a KRS-held multisig backup root.
* @experimental
*/
export interface BackupKeyProviderOptions {
/** Registered KRS provider name, e.g. `coincover`. */
provider: 'coincover';
/** Multisig slots whose backup root the provider holds; defaults to both multisig slots. */
slots?: MultisigRootKeyType[];
}

/**
Expand Down
Loading
Loading