Skip to content
Merged
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
24 changes: 24 additions & 0 deletions modules/sdk-core/test/unit/bitgo/safe/safe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,30 @@ describe('Safe', function () {
mintSend.notCalled.should.be.true();
});

it('rejects a tzec TSS wallet before any derivation-index call', async function () {
stubCoin('tzec', { getDefaultMultisigType: 'tss' });

await safe
.createWallet({ coin: 'tzec', label: 'zec desk', passphrase: 'pw', multisigType: 'tss' })
.should.be.rejectedWith("Coin 'tzec' is not supported for safe wallet minting");
mockBitGo.get.notCalled.should.be.true();
keychainsGet.notCalled.should.be.true();
mintSend.notCalled.should.be.true();
});

it('still mints a tzec onchain wallet from slot 1', async function () {
stubCoin('tzec');
derivationQuery.returns({
result: sinon.stub().resolves({ slot: 'secp256k1Multisig', coinType: 133, account: 0 }),
});

await safe.createWallet({ coin: 'tzec', label: 'zec desk', passphrase: 'pw' });

derivationQuery.calledOnceWithExactly({ slot: 'secp256k1Multisig', coin: 'tzec' }).should.be.true();
keychainsAdd.firstCall.args[0].derivedFromParentWithPath.should.equal("m/44'/133'/1'/0'");
mintSend.firstCall.args[0].should.containEql({ coin: 'tzec', multisigType: 'onchain' });
});

it('rejects a peeked derivation account for the wrong slot', async function () {
derivationQuery.returns({
result: sinon.stub().resolves({ slot: 'ecdsaMpc', coinType: 0, account: 0 }),
Expand Down
21 changes: 15 additions & 6 deletions modules/sdk-lib-safes/src/safeSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* The multisigType-vs-coin validation stays in sdk-core (it needs IBaseCoin); these
* functions take only the chain so this leaf never imports sdk-core.
*/
import { coins, CoinFeature, KeyCurve } from '@bitgo/statics';
import { BaseCoin, coins, CoinFeature, KeyCurve } from '@bitgo/statics';
import type { RootKeyType } from '@bitgo/public-types';

type OnchainSafeRootKeySlot = Extract<RootKeyType, 'secp256k1Multisig' | 'ed25519Multisig'>;
Expand All @@ -26,6 +26,14 @@ const CURVE_TO_SLOTS: Record<KeyCurve, { onchain?: OnchainSafeRootKeySlot; tss?:
[KeyCurve.Pallas]: {},
};

/**
* Coins that also support the Pallas curve (zec, tzec) are not minted under the `ecdsaMpc`
* slot, even though statics lists the `TSS` feature for them.
*/
function hasTssSlot(coin: Readonly<BaseCoin>): boolean {
return !coin.otherSupportedKeyCurves?.includes(KeyCurve.Pallas);
}

export function onchainSlotForCoin(chain: string): OnchainSafeRootKeySlot {
const slot = CURVE_TO_SLOTS[coins.get(chain).primaryKeyCurve].onchain;
if (!slot) {
Expand All @@ -35,7 +43,8 @@ export function onchainSlotForCoin(chain: string): OnchainSafeRootKeySlot {
}

export function tssSlotForCoin(chain: string): TssSafeRootKeySlot {
const slot = CURVE_TO_SLOTS[coins.get(chain).primaryKeyCurve].tss;
const coin = coins.get(chain);
const slot = hasTssSlot(coin) ? CURVE_TO_SLOTS[coin.primaryKeyCurve].tss : undefined;
if (!slot) {
throw new Error(`Coin '${chain}' is not supported for safe wallet minting`);
}
Expand All @@ -44,9 +53,9 @@ export function tssSlotForCoin(chain: string): TssSafeRootKeySlot {

/**
* Every safe root slot a coin can be minted under, in deterministic order (onchain first,
* then tss). Mirrors wallet-platform's mint truth: the onchain slot is included iff the coin
* carries {@link CoinFeature.MULTISIG}, the tss slot iff it carries {@link CoinFeature.TSS}.
* A coin that mints under neither (e.g. a Pallas coin) yields `[]`.
* then tss). The onchain slot is included iff the coin carries {@link CoinFeature.MULTISIG},
* the tss slot iff it carries {@link CoinFeature.TSS} and is not Pallas-capable (zec, tzec),
* so those are onchain-only. A coin that mints under neither yields `[]`.
*/
export function safeSlotsForCoin(chain: string): RootKeyType[] {
const coin = coins.get(chain);
Expand All @@ -55,7 +64,7 @@ export function safeSlotsForCoin(chain: string): RootKeyType[] {
if (onchain && coin.features.includes(CoinFeature.MULTISIG)) {
slots.push(onchain);
}
if (tss && coin.features.includes(CoinFeature.TSS)) {
if (tss && coin.features.includes(CoinFeature.TSS) && hasTssSlot(coin)) {
slots.push(tss);
}
return slots;
Expand Down
25 changes: 24 additions & 1 deletion modules/sdk-lib-safes/test/unit/safeSlot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'should';
import { coins, KeyCurve } from '@bitgo/statics';
import { onchainSlotForCoin, safeSlotsForCoin, tssSlotForCoin } from '../../src';

describe('safeSlot', function () {
Expand Down Expand Up @@ -29,6 +30,11 @@ describe('safeSlot', function () {
tssSlotForCoin('tsol').should.equal('eddsaMpc');
});

it('rejects Pallas-capable coins', function () {
(() => tssSlotForCoin('zec')).should.throw("Coin 'zec' is not supported for safe wallet minting");
(() => tssSlotForCoin('tzec')).should.throw("Coin 'tzec' is not supported for safe wallet minting");
});

it('rejects unknown chains', function () {
(() => tssSlotForCoin('definitely-not-a-coin')).should.throw();
});
Expand All @@ -51,7 +57,24 @@ describe('safeSlot', function () {
});

it('returns both slots in order for a coin with both features', function () {
safeSlotsForCoin('zec').should.deepEqual(['secp256k1Multisig', 'ecdsaMpc']);
safeSlotsForCoin('trx').should.deepEqual(['secp256k1Multisig', 'ecdsaMpc']);
safeSlotsForCoin('arbeth').should.deepEqual(['secp256k1Multisig', 'ecdsaMpc']);
});

it('omits the tss slot for Pallas-capable coins', function () {
safeSlotsForCoin('zec').should.deepEqual(['secp256k1Multisig']);
safeSlotsForCoin('tzec').should.deepEqual(['secp256k1Multisig']);
});

// Pins the Pallas set: a new Pallas coin needs a safe slot decision before it is added here.
it('statics carry Pallas only on zec and tzec', function () {
const pallasCoins: string[] = [];
coins.forEach((coin, name) => {
if (coin.primaryKeyCurve === KeyCurve.Pallas || coin.otherSupportedKeyCurves?.includes(KeyCurve.Pallas)) {
pallasCoins.push(name);
}
});
pallasCoins.sort().should.deepEqual(['tzec', 'zec']);
});

it('returns [] for a coin that mints under no safe slot', function () {
Expand Down
Loading