Skip to content

Commit fea1c02

Browse files
authored
Merge pull request #9843 from BitGo/cshld-1849-tstxsignet
feat(sdk-coin-stx): register tstxsignet staking-testnet coin
2 parents 0e55b17 + d3b64b6 commit fea1c02

20 files changed

Lines changed: 262 additions & 11 deletions

File tree

‎modules/account-lib/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ const coinBuilderMap = {
249249
tcspr: Cspr.TransactionBuilderFactory,
250250
stx: Stx.TransactionBuilderFactory,
251251
tstx: Stx.TransactionBuilderFactory,
252+
tstxsignet: Stx.TransactionBuilderFactory,
252253
algo: Algo.TransactionBuilderFactory,
253254
talgo: Algo.TransactionBuilderFactory,
254255
sol: Sol.TransactionBuilderFactory,

‎modules/bitgo/src/v2/coinFactory.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ import {
207207
Tsol,
208208
Tsoneium,
209209
Tstx,
210+
Tstxsignet,
210211
Tstt,
211212
Tsui,
212213
Tsusd,
@@ -390,6 +391,7 @@ export function registerCoinConstructors(coinFactory: CoinFactory, coinMap: Coin
390391
coinFactory.register('tsoneium', Tsoneium.createInstance);
391392
coinFactory.register('tstarknet', Starknet.createInstance);
392393
coinFactory.register('tstx', Tstx.createInstance);
394+
coinFactory.register('tstxsignet', Tstxsignet.createInstance);
393395
coinFactory.register('tstt', Tstt.createInstance);
394396
coinFactory.register('tsui', Tsui.createInstance);
395397
coinFactory.register('tsusd', Tsusd.createInstance);
@@ -972,6 +974,8 @@ export function getCoinConstructor(coinName: string): CoinConstructor | undefine
972974
return Starknet.createInstance;
973975
case 'tstx':
974976
return Tstx.createInstance;
977+
case 'tstxsignet':
978+
return Tstxsignet.createInstance;
975979
case 'tstt':
976980
return Tstt.createInstance;
977981
case 'tsui':
@@ -1184,6 +1188,7 @@ export function getTokenConstructor(
11841188
}
11851189
case 'stx':
11861190
case 'tstx':
1191+
case 'tstxsignet':
11871192
return Sip10Token.createTokenConstructor(tokenConfig as Sip10TokenConfig);
11881193
case 'near':
11891194
case 'tnear':

‎modules/bitgo/src/v2/coins/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import { Tstt } from '@bitgo/sdk-coin-stt';
6161
import { Sgb, Tsgb } from '@bitgo/sdk-coin-sgb';
6262
import { Sol, Tsol } from '@bitgo/sdk-coin-sol';
6363
import { Starknet } from '@bitgo/sdk-coin-starknet';
64-
import { Stx, Tstx, Sip10Token } from '@bitgo/sdk-coin-stx';
64+
import { Stx, Tstx, Tstxsignet, Sip10Token } from '@bitgo/sdk-coin-stx';
6565
import { Sui, Tsui, SuiToken } from '@bitgo/sdk-coin-sui';
6666
import { Tao, Ttao, TaoToken } from '@bitgo/sdk-coin-tao';
6767
import { Tempo, Ttempo, Tip20Token } from '@bitgo/sdk-coin-tempo';
@@ -138,7 +138,7 @@ export { Sol, Tsol };
138138
export { Soneium, Tsoneium, SoneiumToken };
139139
export { Starknet };
140140
export { Tstt };
141-
export { Stx, Tstx, Sip10Token };
141+
export { Stx, Tstx, Tstxsignet, Sip10Token };
142142
export { Sui, Tsui, SuiToken };
143143
export { Tao, Ttao, TaoToken };
144144
export { Tempo, Ttempo, Tip20Token };

‎modules/sdk-coin-stx/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * as StxLib from './lib';
22
export * from './stx';
33
export * from './tstx';
4+
export * from './tstxsignet';
45
export * from './types';
56
export * from './register';
67
export * from './sip10Token';

‎modules/sdk-coin-stx/src/lib/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { AddressVersion, AddressHashMode } from '@stacks/transactions';
22
export * from './keyPair';
3+
export * from './network';
34
export * from './transaction';
45
export * from './transactionBuilderFactory';
56
export * from './sbtcWithdrawBuilder';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { BaseCoin as CoinConfig, NetworkType, StacksNetwork as StaticsStacksNetwork } from '@bitgo/statics';
2+
import { StacksMainnet, StacksNetwork, StacksTestnet } from '@stacks/network';
3+
4+
/**
5+
* Build the `@stacks/network` object a transaction is serialized against.
6+
*
7+
* The stock Stacks mainnet/testnet networks hardcode their chain ID and transaction version. A
8+
* statics network that describes a different Stacks deployment (for example the Stacks
9+
* Foundation `staking-testnet`, chain ID 1280, on a private Bitcoin signet) declares
10+
* `chainId` / `transactionVersion`; those are applied on top of the stock network so the
11+
* serialized transaction carries the chain ID the node expects.
12+
*
13+
* When statics omits both fields the returned network is exactly the stock one, so existing
14+
* `stx` / `tstx` transactions stay byte-identical.
15+
*/
16+
export function toStacksNetwork(coinConfig: Readonly<CoinConfig>): StacksNetwork {
17+
const staticsNetwork = coinConfig.network as StaticsStacksNetwork;
18+
const network = staticsNetwork.type === NetworkType.MAINNET ? new StacksMainnet() : new StacksTestnet();
19+
20+
if (staticsNetwork.chainId !== undefined) {
21+
network.chainId = staticsNetwork.chainId;
22+
}
23+
if (staticsNetwork.transactionVersion !== undefined) {
24+
network.version = staticsNetwork.transactionVersion;
25+
}
26+
27+
return network;
28+
}

‎modules/sdk-coin-stx/src/lib/transactionBuilder.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import BigNumber from 'bignumber.js';
22
import BigNum from 'bn.js';
3-
import { BaseCoin as CoinConfig, NetworkType } from '@bitgo/statics';
3+
import { BaseCoin as CoinConfig } from '@bitgo/statics';
44
import {
55
AuthType,
66
BufferReader,
@@ -13,7 +13,7 @@ import {
1313
StacksMessageType,
1414
PubKeyEncoding,
1515
} from '@stacks/transactions';
16-
import { StacksNetwork, StacksTestnet, StacksMainnet } from '@stacks/network';
16+
import { StacksNetwork } from '@stacks/network';
1717
import {
1818
BaseAddress,
1919
BaseFee,
@@ -31,6 +31,7 @@ import { KeyPair } from './keyPair';
3131
import { SignatureData } from './iface';
3232
import { isValidAddress, removeHexPrefix, isValidMemo, isValidPublicKey } from './utils';
3333
import { ANCHOR_MODE, DEFAULT_MULTISIG_SIG_NUMBER } from './constants';
34+
import { toStacksNetwork } from './network';
3435

3536
export abstract class TransactionBuilder extends BaseTransactionBuilder {
3637
private _transaction: Transaction;
@@ -51,7 +52,7 @@ export abstract class TransactionBuilder extends BaseTransactionBuilder {
5152
this._fromPubKeys = [];
5253
this._signatures = [];
5354
this._numberSignatures = DEFAULT_MULTISIG_SIG_NUMBER;
54-
this._network = _coinConfig.network.type === NetworkType.MAINNET ? new StacksMainnet() : new StacksTestnet();
55+
this._network = toStacksNetwork(_coinConfig);
5556
this._transaction = new Transaction(_coinConfig);
5657
}
5758

‎modules/sdk-coin-stx/src/register.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { BitGoBase } from '@bitgo/sdk-core';
22
import { Stx } from './stx';
33
import { Tstx } from './tstx';
4+
import { Tstxsignet } from './tstxsignet';
45
import { Sip10Token } from './sip10Token';
56

67
export const register = (sdk: BitGoBase): void => {
78
sdk.register('stx', Stx.createInstance);
89
sdk.register('tstx', Tstx.createInstance);
10+
sdk.register('tstxsignet', Tstxsignet.createInstance);
911
Sip10Token.createTokenConstructors().forEach(({ name, coinConstructor }) => {
1012
sdk.register(name, coinConstructor);
1113
});

‎modules/sdk-coin-stx/src/sip10Token.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import _ from 'lodash';
22
import BigNumber from 'bignumber.js';
33

44
import { BitGoBase, CoinConstructor, NamedCoinConstructor, VerifyTransactionOptions } from '@bitgo/sdk-core';
5-
import { BaseCoin as StaticsBaseCoin, coins, NetworkType, Sip10TokenConfig, tokens } from '@bitgo/statics';
5+
import { BaseCoin as StaticsBaseCoin, coins, Sip10TokenConfig, tokens } from '@bitgo/statics';
66

77
import { Stx } from './stx';
88
import { TransactionBuilderFactory } from './lib';
@@ -13,7 +13,9 @@ export class Sip10Token extends Stx {
1313
public readonly tokenConfig: Sip10TokenConfig;
1414

1515
constructor(bitgo: BitGoBase, tokenConfig: Sip10TokenConfig) {
16-
const staticsCoin = tokenConfig.network === NetworkType.MAINNET ? coins.get('stx') : coins.get('tstx');
16+
// tokenConfig.coin is derived by statics from the token's network, so it stays correct for
17+
// deployments beyond the stock mainnet/testnet pair (e.g. tstxsignet, chain ID 1280).
18+
const staticsCoin = coins.get(tokenConfig.coin);
1719
super(bitgo, staticsCoin);
1820
this.tokenConfig = tokenConfig;
1921
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Testnet Stacks Signet
3+
*
4+
* @format
5+
*/
6+
import { BaseCoin, BitGoBase, Environments } from '@bitgo/sdk-core';
7+
import { BaseCoin as StaticsBaseCoin } from '@bitgo/statics';
8+
9+
import { Stx } from './stx';
10+
11+
export class Tstxsignet extends Stx {
12+
protected readonly _staticsCoin: Readonly<StaticsBaseCoin>;
13+
14+
constructor(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>) {
15+
super(bitgo, staticsCoin);
16+
17+
if (!staticsCoin) {
18+
throw new Error('missing required constructor parameter staticsCoin');
19+
}
20+
21+
this._staticsCoin = staticsCoin;
22+
}
23+
24+
static createInstance(bitgo: BitGoBase, staticsCoin?: Readonly<StaticsBaseCoin>): BaseCoin {
25+
return new Tstxsignet(bitgo, staticsCoin);
26+
}
27+
28+
/**
29+
* The Stacks staking-testnet RPC. It is a distinct chain (chain ID 1280) that only exists on
30+
* testnet-derived environments; environments without it configured (e.g. prod) cannot talk to
31+
* this network.
32+
*/
33+
override getPublicNodeUrl(): string {
34+
const nodeUrl = Environments[this.bitgo.getEnv()].stxSignetNodeUrl;
35+
if (!nodeUrl) {
36+
throw new Error(`no stx signet node url configured for env ${this.bitgo.getEnv()}`);
37+
}
38+
return nodeUrl;
39+
}
40+
}

0 commit comments

Comments
 (0)