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
39 changes: 30 additions & 9 deletions modules/sdk-coin-sui/src/lib/hashi/bitcoin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { BaseCoin } from '@bitgo/statics';
import { getHashiConfig } from './config';

export type HashiBitcoinNetwork = 'mainnet' | 'signet';
import { getHashiConfig, HashiBitcoinNetwork, HashiConfig } from './config';

export interface WitnessProgram {
version: number;
Expand Down Expand Up @@ -91,7 +89,7 @@ function assertSameObjectId(actual: unknown, expected: string, what: string): vo
* version 1 makes the object fail this check until the SDK is updated for the new package version.
*/
export function assertHashiObjectIdentity(hashiObject: HashiIdentityFields, coinConfig: Readonly<BaseCoin>): void {
const { hashiObject: pinned } = getHashiConfig(coinConfig);
const { hashiObject: pinned } = getHashiConfig(coinConfig.network.type);
assertSameObjectId(hashiObject?.id, pinned.objectId, 'id');
const enabledVersions = hashiObject?.versioning?.enabled_versions?.contents;
if (!Array.isArray(enabledVersions)) {
Expand Down Expand Up @@ -135,6 +133,26 @@ export function extractHbtcDepositKeys(hashiObject: HashiDepositKeyFields): Hbtc
};
}

function assertPinnedDepositKeys(keys: HbtcDepositKeys, config: Readonly<HashiConfig>): void {
const pinned = config.depositKeys;
if (!pinned?.mpcMasterCompressed || !pinned?.guardianBtcXOnly) {
throw new Error('Invalid Hashi config: pinned deposit keys are missing');
}
const toHex = (bytes: Uint8Array): string => Buffer.from(bytes).toString('hex');
if (toHex(keys.mpcMasterCompressed) !== pinned.mpcMasterCompressed.toLowerCase()) {
throw new Error(
'Hashi committee key does not match the pinned value; the committee may have rotated. ' +
'Do not derive a deposit address until the pin is updated and security-reviewed'
);
}
if (toHex(keys.guardianBtcXOnly) !== pinned.guardianBtcXOnly.toLowerCase()) {
throw new Error(
'Hashi guardian key does not match the pinned value. ' +
'Do not derive a deposit address until the pin is updated and security-reviewed'
);
}
}

export function deriveHbtcDepositAddress(
suiAddress: string,
keys: HbtcDepositKeys,
Expand All @@ -155,24 +173,27 @@ export function deriveHbtcDepositAddress(
* The Sui address must be the full 32-byte form (0x followed by 64 hex characters). The committee and guardian
* keys are read from the supplied on-chain Hashi object.
* The object's id must match the deployment pinned in the SDK config and package version 1 must be enabled.
* The keys are taken from the supplied object as is, so it must be fetched from a trusted node.
* The keys are verified against the values pinned per network in the SDK config. A mismatch is a hard error
* that stays in place until the pin is updated and reviewed.
*/
export function getHbtcDepositAddress(
suiAddress: string,
hashiObject: HashiObjectFields,
coinConfig: Readonly<BaseCoin>
): string {
const { bitcoinNetwork } = getHashiConfig(coinConfig);
const config = getHashiConfig(coinConfig.network.type);
assertHashiObjectIdentity(hashiObject, coinConfig);
return deriveHbtcDepositAddress(suiAddress, extractHbtcDepositKeys(hashiObject), bitcoinNetwork);
const keys = extractHbtcDepositKeys(hashiObject);
assertPinnedDepositKeys(keys, config);
return deriveHbtcDepositAddress(suiAddress, keys, config.bitcoinNetwork);
}

/**
* Decodes a Bitcoin redeem destination, accepting only P2WPKH (v0, 20 bytes) and P2TR (v1, 32 bytes) on the
* configured network. Throws for P2WSH, legacy, wrong-network and wrong-checksum encodings.
*/
export function hbtcRedeemAddressToWitnessProgram(address: string, coinConfig: Readonly<BaseCoin>): WitnessProgram {
const { bitcoinNetwork } = getHashiConfig(coinConfig);
const { bitcoinNetwork } = getHashiConfig(coinConfig.network.type);
return loadMystenHashi().bitcoinAddressToWitnessProgram(address, bitcoinNetwork);
}

Expand All @@ -181,6 +202,6 @@ export function hbtcRedeemAddressToWitnessProgram(address: string, coinConfig: R
* bech32m for 32 bytes (P2TR). Inverse of `hbtcRedeemAddressToWitnessProgram`.
*/
export function witnessProgramToHbtcRedeemAddress(program: Uint8Array, coinConfig: Readonly<BaseCoin>): string {
const { bitcoinNetwork } = getHashiConfig(coinConfig);
const { bitcoinNetwork } = getHashiConfig(coinConfig.network.type);
return loadMystenHashi().witnessProgramToAddress(program, bitcoinNetwork);
}
109 changes: 86 additions & 23 deletions modules/sdk-coin-sui/src/lib/hashi/config.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,126 @@
import { BaseCoin, coins, NetworkType, SuiCoin } from '@bitgo/statics';
import { coins, NetworkType, SuiCoin } from '@bitgo/statics';
import { SUI_CLOCK_OBJECT_ID } from '../mystenlab/framework';

export type HashiBitcoinNetwork = 'mainnet' | 'signet';

export interface HashiSharedObjectRef {
objectId: string;
initialSharedVersion: number;
mutable: boolean;
}

export interface HashiDepositKeysPin {
/** SEC1 compressed secp256k1 committee key, lowercase hex without 0x */
mpcMasterCompressed: string;
/** x-only guardian key, lowercase hex without 0x */
guardianBtcXOnly: string;
}

export interface HashiConfig {
hashiObject: HashiSharedObjectRef;
originalPackageId: string;
hbtcCoinType: string;
hbtcTokenName: string;
clockObject: HashiSharedObjectRef;
bitcoinNetwork: 'signet';
bitcoinNetwork: HashiBitcoinNetwork;
depositKeys: HashiDepositKeysPin;
}

const HBTC_TESTNET_TOKEN_NAME = 'tsui:hbtc';
const HBTC_MAINNET_TOKEN_NAME = 'sui:hbtc';

type HbtcTokenName = typeof HBTC_TESTNET_TOKEN_NAME | typeof HBTC_MAINNET_TOKEN_NAME;

function getHbtcTestnetToken(): SuiCoin {
const token = coins.get(HBTC_TESTNET_TOKEN_NAME);
const CLOCK_OBJECT: Readonly<HashiSharedObjectRef> = Object.freeze({
objectId: SUI_CLOCK_OBJECT_ID,
initialSharedVersion: 1,
mutable: false,
});

function getHbtcToken(name: HbtcTokenName): SuiCoin {
const token = coins.get(name);
if (!(token instanceof SuiCoin)) {
throw new Error(`${HBTC_TESTNET_TOKEN_NAME} is not a Sui token in statics`);
throw new Error(`${name} is not a Sui token in statics`);
}
return token;
}

function buildConfig(
tokenName: HbtcTokenName,
hashiObject: HashiSharedObjectRef,
bitcoinNetwork: HashiBitcoinNetwork,
depositKeys: HashiDepositKeysPin
): Readonly<HashiConfig> {
const token = getHbtcToken(tokenName);
return Object.freeze({
hashiObject: Object.freeze(hashiObject),
originalPackageId: token.packageId,
hbtcCoinType: `${token.packageId}::${token.module}::${token.symbol}`,
hbtcTokenName: token.name,
clockObject: CLOCK_OBJECT,
bitcoinNetwork,
depositKeys: Object.freeze(depositKeys),
});
}

let testnetConfig: Readonly<HashiConfig> | undefined;
let mainnetConfig: Readonly<HashiConfig> | undefined;

/** Looked up on first use so that loading the SDK does not depend on the statics version carrying `tsui:hbtc`. */
export function getHashiTestnetConfig(): Readonly<HashiConfig> {
if (!testnetConfig) {
const token = getHbtcTestnetToken();
testnetConfig = Object.freeze({
hashiObject: Object.freeze({
testnetConfig = buildConfig(
HBTC_TESTNET_TOKEN_NAME,
{
objectId: '0x342e908043db6090558ed7766df95bfc3609881b2eac7edb5343dd5bb3ddd1bb',
initialSharedVersion: 1,
mutable: true,
}),
originalPackageId: token.packageId,
hbtcCoinType: `${token.packageId}::${token.module}::${token.symbol}`,
hbtcTokenName: token.name,
clockObject: Object.freeze({
objectId: '0x0000000000000000000000000000000000000000000000000000000000000006',
initialSharedVersion: 1,
mutable: false,
}),
bitcoinNetwork: 'signet',
});
},
'signet',
{
mpcMasterCompressed: '02f8d7d783e41fa1d3008f1fa33499fb07171732b8f413ce4431154318efac7bcb',
guardianBtcXOnly: '7a708f475562163946faaa1626e65c8907ee1007a1916e7415940fdc33fe7cd3',
}
);
}
return testnetConfig;
}

/** Looked up on first use so that loading the SDK does not depend on the statics version carrying `sui:hbtc`. */
export function getHashiMainnetConfig(): Readonly<HashiConfig> {
if (!mainnetConfig) {
mainnetConfig = buildConfig(
HBTC_MAINNET_TOKEN_NAME,
{
objectId: '0x8d110642d9b5133bd661cda4873edf135ab6b91ff6918601015880874b950e5d',
initialSharedVersion: 920727622,
mutable: true,
},
'mainnet',
{
mpcMasterCompressed: '0296f87c7ed4ee4b0ada70e6c66df253fffdbb62e17bc5466306d0e34e6cb71f83',
guardianBtcXOnly: '5c478afca04c09b5c7cfd12afee3ca43d6dba0d5767576779ceedef6e34d4d4a',
}
);
}
return mainnetConfig;
}

export function getHashiTestnetOriginalPackageId(): string {
return getHashiTestnetConfig().originalPackageId;
}

export function getHashiConfig(coinConfig: Readonly<BaseCoin>): Readonly<HashiConfig> {
if (coinConfig.network.type !== NetworkType.TESTNET) {
throw new Error('Hashi on Sui is only supported on testnet by the SDK');
export function getHashiMainnetOriginalPackageId(): string {
return getHashiMainnetConfig().originalPackageId;
}

export function getHashiConfig(networkType: NetworkType): Readonly<HashiConfig> {
switch (networkType) {
case NetworkType.MAINNET:
return getHashiMainnetConfig();
case NetworkType.TESTNET:
return getHashiTestnetConfig();
default:
throw new Error(`The SDK does not support Hashi on network type ${String(networkType)}`);
}
return getHashiTestnetConfig();
}
Comment thread
arvind-bg marked this conversation as resolved.
Resolved
8 changes: 7 additions & 1 deletion modules/sdk-coin-sui/src/lib/hashi/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export { hbtcRedeemAddressToWitnessProgram, getHbtcDepositAddress, witnessProgramToHbtcRedeemAddress } from './bitcoin';
export type { HashiDepositKeyFields, HashiIdentityFields, HashiObjectFields, WitnessProgram } from './bitcoin';
export { getHashiConfig, getHashiTestnetOriginalPackageId, getHashiTestnetConfig } from './config';
export {
getHashiConfig,
getHashiMainnetConfig,
getHashiMainnetOriginalPackageId,
getHashiTestnetConfig,
getHashiTestnetOriginalPackageId,
} from './config';
export type { HashiConfig, HashiSharedObjectRef } from './config';
4 changes: 2 additions & 2 deletions modules/sdk-coin-sui/src/lib/hashiBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class HashiBuilder extends TransactionBuilder<HashiProgrammableTransactio
*/
hashiObject(fields: HashiIdentityFields): this {
assertHashiObjectIdentity(fields, this._coinConfig);
this._callPackageId = normalizeSuiAddress(getHashiConfig(this._coinConfig).originalPackageId);
this._callPackageId = normalizeSuiAddress(getHashiConfig(this._coinConfig.network.type).originalPackageId);
return this;
}

Expand Down Expand Up @@ -284,7 +284,7 @@ export class HashiBuilder extends TransactionBuilder<HashiProgrammableTransactio
protected buildSuiTransaction(): SuiTransaction<HashiProgrammableTransaction> {
this.validateTransactionFields();

const config = getHashiConfig(this._coinConfig);
const config = getHashiConfig(this._coinConfig.network.type);
const packageId = this._callPackageId as string;

let tx: HashiProgrammableTransaction;
Expand Down
4 changes: 2 additions & 2 deletions modules/sdk-coin-sui/src/lib/hashiTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function assertCanonical(
coinConfig: Readonly<CoinConfig>
): void {
const operation = parseHashiOperation(suiTransaction.tx);
const config = getHashiConfig(coinConfig);
const config = getHashiConfig(coinConfig.network.type);
const packageId = normalizeSuiAddress(config.originalPackageId);
if (operation.callPackageId !== packageId) {
throw invalid(`calls package ${operation.callPackageId}, expected ${config.originalPackageId}`);
Expand Down Expand Up @@ -253,7 +253,7 @@ export class HashiTransaction extends Transaction<HashiProgrammableTransaction>
return;
}
const operation = this.operation;
const coin = getHashiConfig(this._coinConfig).hbtcTokenName;
const coin = getHashiConfig(this._coinConfig.network.type).hbtcTokenName;
this._outputs = this.recipients(operation).map((recipient) => ({
address: recipient.address,
value: String(recipient.amount),
Expand Down
8 changes: 5 additions & 3 deletions modules/sdk-coin-sui/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@bitgo/sdk-core';
import BigNumber from 'bignumber.js';
import { SUI_ADDRESS_LENGTH } from './constants';
import { getHashiTestnetOriginalPackageId } from './hashi/config';
import { getHashiMainnetOriginalPackageId, getHashiTestnetOriginalPackageId } from './hashi/config';
import { isPureArg } from './mystenlab/types/sui-bcs';
import { BCS, fromB64 } from '@mysten/bcs';
import {
Expand Down Expand Up @@ -299,9 +299,11 @@ export class Utils implements BaseUtils {
* other than the canonical one when it is rebuilt from raw.
*/
getHashiTransactionType(commands: TransactionCommandType[]): SuiTransactionType | undefined {
const hashiPackage = normalizeSuiAddress(getHashiTestnetOriginalPackageId());
const hashiPackages = [getHashiTestnetOriginalPackageId(), getHashiMainnetOriginalPackageId()].map((packageId) =>
normalizeSuiAddress(packageId)
);
const targets = commands.flatMap((command) =>
command.kind === 'MoveCall' && normalizeSuiAddress(command.target.split('::')[0]) === hashiPackage
command.kind === 'MoveCall' && hashiPackages.includes(normalizeSuiAddress(command.target.split('::')[0]))
? [command.target]
: []
);
Expand Down
22 changes: 22 additions & 0 deletions modules/sdk-coin-sui/test/resources/hashiObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,25 @@ export const testnetHashiObjectFields: HashiObjectFields = {
},
},
};

// Relevant fields of the mainnet Hashi object 0x8d110642...0e5d, read on 2026-12-05.
export const mainnetHashiObjectFields: HashiObjectFields = {
id: '0x8d110642d9b5133bd661cda4873edf135ab6b91ff6918601015880874b950e5d',
versioning: {
enabled_versions: { contents: ['1'] },
},
committee_set: {
mpc_public_key: 'gx+3bE7j0AZjRsV74WK7/f9T8m3G5nDaCkvu1H58+JYA',
},
config: {
config: {
contents: [
{ key: 'paused', value: { '@variant': 'Bool', pos0: false } },
{
key: 'guardian_btc_public_key',
value: { '@variant': 'Bytes', pos0: 'XEeK/KBMCbXHz9Eq/uPKQ9bboNV2dXZ3nO7e9uNNTUo=' },
},
],
},
},
};
Loading
Loading