@atomiqlabs/chain-starknet 8.2.3 → 8.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/starknet/StarknetInitializer.js +1 -1
- package/dist/starknet/chain/StarknetChainInterface.d.ts +4 -2
- package/dist/starknet/chain/StarknetChainInterface.js +14 -1
- package/dist/starknet/chain/modules/StarknetTransactions.d.ts +1 -1
- package/dist/starknet/swaps/StarknetSwapData.d.ts +4 -0
- package/dist/starknet/swaps/StarknetSwapData.js +6 -0
- package/dist/utils/Utils.d.ts +1 -0
- package/dist/utils/Utils.js +19 -1
- package/package.json +2 -2
- package/src/starknet/StarknetInitializer.ts +1 -1
- package/src/starknet/chain/StarknetChainInterface.ts +22 -3
- package/src/starknet/chain/modules/StarknetTransactions.ts +1 -1
- package/src/starknet/swaps/StarknetSwapData.ts +8 -1
- package/src/utils/Utils.ts +19 -0
|
@@ -69,7 +69,7 @@ function initializeStarknet(options, bitcoinRpc, network) {
|
|
|
69
69
|
const Fees = options.fees ?? new StarknetFees_1.StarknetFees(provider);
|
|
70
70
|
const chainId = options.chainId ??
|
|
71
71
|
(network === base_1.BitcoinNetwork.MAINNET ? starknet_1.constants.StarknetChainId.SN_MAIN : starknet_1.constants.StarknetChainId.SN_SEPOLIA);
|
|
72
|
-
const chainInterface = new StarknetChainInterface_1.StarknetChainInterface(chainId, provider, wsChannel, Fees, options.starknetConfig);
|
|
72
|
+
const chainInterface = new StarknetChainInterface_1.StarknetChainInterface(chainId, provider, wsChannel, Fees, options.starknetConfig, network);
|
|
73
73
|
const btcRelay = new StarknetBtcRelay_1.StarknetBtcRelay(chainInterface, bitcoinRpc, network, options.btcRelayContract, options.btcRelayContractDeploymentHeight);
|
|
74
74
|
const swapContract = new StarknetSwapContract_1.StarknetSwapContract(chainInterface, btcRelay, options.swapContract, options.handlerContracts, options.swapContractDeploymentHeight);
|
|
75
75
|
const spvVaultContract = new StarknetSpvVaultContract_1.StarknetSpvVaultContract(chainInterface, btcRelay, bitcoinRpc, options.spvVaultContract, options.spvVaultContractDeploymentHeight);
|
|
@@ -6,7 +6,7 @@ import { StarknetEvents } from "./modules/StarknetEvents";
|
|
|
6
6
|
import { StarknetSignatures } from "./modules/StarknetSignatures";
|
|
7
7
|
import { StarknetAccounts } from "./modules/StarknetAccounts";
|
|
8
8
|
import { StarknetBlocks } from "./modules/StarknetBlocks";
|
|
9
|
-
import { ChainInterface, TransactionConfirmationOptions } from "@atomiqlabs/base";
|
|
9
|
+
import { BitcoinNetwork, ChainInterface, TransactionConfirmationOptions } from "@atomiqlabs/base";
|
|
10
10
|
import { StarknetSigner } from "../wallet/StarknetSigner";
|
|
11
11
|
/**
|
|
12
12
|
* Configuration options for Starknet chain interface
|
|
@@ -64,7 +64,8 @@ export declare class StarknetChainInterface implements ChainInterface<StarknetTx
|
|
|
64
64
|
readonly Accounts: StarknetAccounts;
|
|
65
65
|
readonly Blocks: StarknetBlocks;
|
|
66
66
|
readonly config: StarknetConfig;
|
|
67
|
-
|
|
67
|
+
private readonly bitcoinNetwork?;
|
|
68
|
+
constructor(chainId: constants.StarknetChainId, provider: Provider, wsChannel?: WebSocketChannel, feeEstimator?: StarknetFees, options?: StarknetConfig, bitcoinNetwork?: BitcoinNetwork);
|
|
68
69
|
/**
|
|
69
70
|
* @inheritDoc
|
|
70
71
|
*/
|
|
@@ -164,4 +165,5 @@ export declare class StarknetChainInterface implements ChainInterface<StarknetTx
|
|
|
164
165
|
* @inheritDoc
|
|
165
166
|
*/
|
|
166
167
|
wrapSigner(signer: Account): Promise<StarknetSigner>;
|
|
168
|
+
verifyNetwork(bitcoinNetwork: BitcoinNetwork): Promise<void>;
|
|
167
169
|
}
|
|
@@ -11,6 +11,7 @@ const StarknetEvents_1 = require("./modules/StarknetEvents");
|
|
|
11
11
|
const StarknetSignatures_1 = require("./modules/StarknetSignatures");
|
|
12
12
|
const StarknetAccounts_1 = require("./modules/StarknetAccounts");
|
|
13
13
|
const StarknetBlocks_1 = require("./modules/StarknetBlocks");
|
|
14
|
+
const base_1 = require("@atomiqlabs/base");
|
|
14
15
|
const StarknetSigner_1 = require("../wallet/StarknetSigner");
|
|
15
16
|
const buffer_1 = require("buffer");
|
|
16
17
|
const StarknetKeypairWallet_1 = require("../wallet/accounts/StarknetKeypairWallet");
|
|
@@ -21,7 +22,7 @@ const StarknetBrowserSigner_1 = require("../wallet/StarknetBrowserSigner");
|
|
|
21
22
|
* @category Chain Interface
|
|
22
23
|
*/
|
|
23
24
|
class StarknetChainInterface {
|
|
24
|
-
constructor(chainId, provider, wsChannel, feeEstimator = new StarknetFees_1.StarknetFees(provider), options) {
|
|
25
|
+
constructor(chainId, provider, wsChannel, feeEstimator = new StarknetFees_1.StarknetFees(provider), options, bitcoinNetwork) {
|
|
25
26
|
var _a, _b, _c, _d;
|
|
26
27
|
this.chainId = "STARKNET";
|
|
27
28
|
this.starknetChainId = chainId;
|
|
@@ -39,6 +40,7 @@ class StarknetChainInterface {
|
|
|
39
40
|
this.Events = new StarknetEvents_1.StarknetEvents(this);
|
|
40
41
|
this.Accounts = new StarknetAccounts_1.StarknetAccounts(this);
|
|
41
42
|
this.Blocks = new StarknetBlocks_1.StarknetBlocks(this);
|
|
43
|
+
this.bitcoinNetwork = bitcoinNetwork;
|
|
42
44
|
}
|
|
43
45
|
/**
|
|
44
46
|
* @inheritDoc
|
|
@@ -201,5 +203,16 @@ class StarknetChainInterface {
|
|
|
201
203
|
return Promise.resolve(new StarknetSigner_1.StarknetSigner(signer));
|
|
202
204
|
}
|
|
203
205
|
}
|
|
206
|
+
async verifyNetwork(bitcoinNetwork) {
|
|
207
|
+
if (this.bitcoinNetwork != null && bitcoinNetwork !== this.bitcoinNetwork)
|
|
208
|
+
throw new Error(`Network mismatch, the chain interface was not setup for ${base_1.BitcoinNetwork[bitcoinNetwork]}, chain interface network: ${base_1.BitcoinNetwork[this.bitcoinNetwork]}`);
|
|
209
|
+
const chainId = await this.provider.getChainId();
|
|
210
|
+
if (chainId !== starknet_1.constants.StarknetChainId.SN_MAIN && chainId !== starknet_1.constants.StarknetChainId.SN_SEPOLIA) {
|
|
211
|
+
starknet_1.logger.warn(`verifyNetwork(): Using non-standard chainId ${chainId}, skipping network verfication!`);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (this.starknetChainId !== chainId)
|
|
215
|
+
throw new Error(`Network mismatch, the underlying RPC provider isn't using the correct chainId, expected: ${this.starknetChainId}, provider returned: ${chainId}`);
|
|
216
|
+
}
|
|
204
217
|
}
|
|
205
218
|
exports.StarknetChainInterface = StarknetChainInterface;
|
|
@@ -52,7 +52,7 @@ export type StarknetTx = StarknetTxInvoke | StarknetTxDeployAccount;
|
|
|
52
52
|
* Represents a signed Starknet transactions, which can either be an "INVOKE" or "DEPLOY_ACCOUNT" type, use the
|
|
53
53
|
* {@link isStarknetTxInvoke} & {@link isStarknetTxDeployAccount} to narrow down the type.
|
|
54
54
|
*
|
|
55
|
-
* @
|
|
55
|
+
* @remarks For Starknet this is just an alias for {@link StarknetTx}
|
|
56
56
|
*
|
|
57
57
|
* @category Chain Interface
|
|
58
58
|
*/
|
|
@@ -469,6 +469,12 @@ class StarknetSwapData extends base_1.SwapData {
|
|
|
469
469
|
hasSuccessAction() {
|
|
470
470
|
return this.successAction != null;
|
|
471
471
|
}
|
|
472
|
+
/**
|
|
473
|
+
* @inheritDoc
|
|
474
|
+
*/
|
|
475
|
+
getEscrowStruct() {
|
|
476
|
+
return (0, Utils_1.replaceBigInts)(this.toEscrowStruct());
|
|
477
|
+
}
|
|
472
478
|
}
|
|
473
479
|
exports.StarknetSwapData = StarknetSwapData;
|
|
474
480
|
base_1.SwapData.deserializers["strk"] = StarknetSwapData;
|
package/dist/utils/Utils.d.ts
CHANGED
package/dist/utils/Utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.deserializeResourceBounds = exports.serializeResourceBounds = exports.deserializeSignature = exports.serializeSignature = exports.bigIntMax = exports.findLastIndex = exports.poseidonHashRange = exports.bufferToByteArray = exports.bufferToBytes31Span = exports.bytes31SpanToBuffer = exports.toBigInt = exports.bigNumberishToBuffer = exports.u32ReverseEndianness = exports.bufferToU32Array = exports.u32ArrayToBuffer = exports.calculateHash = exports.toHex = exports.tryWithRetries = exports.getLogger = exports.onceAsync = exports.timeoutPromise = exports.isUint256 = void 0;
|
|
3
|
+
exports.replaceBigInts = exports.deserializeResourceBounds = exports.serializeResourceBounds = exports.deserializeSignature = exports.serializeSignature = exports.bigIntMax = exports.findLastIndex = exports.poseidonHashRange = exports.bufferToByteArray = exports.bufferToBytes31Span = exports.bytes31SpanToBuffer = exports.toBigInt = exports.bigNumberishToBuffer = exports.u32ReverseEndianness = exports.bufferToU32Array = exports.u32ArrayToBuffer = exports.calculateHash = exports.toHex = exports.tryWithRetries = exports.getLogger = exports.onceAsync = exports.timeoutPromise = exports.isUint256 = void 0;
|
|
4
4
|
const starknet_1 = require("starknet");
|
|
5
5
|
const buffer_1 = require("buffer");
|
|
6
6
|
function isUint256(val) {
|
|
@@ -302,3 +302,21 @@ function deserializeResourceBounds(resourceBounds) {
|
|
|
302
302
|
};
|
|
303
303
|
}
|
|
304
304
|
exports.deserializeResourceBounds = deserializeResourceBounds;
|
|
305
|
+
function replaceBigInts(obj) {
|
|
306
|
+
const replace = (value) => {
|
|
307
|
+
if (typeof (value) === "bigint")
|
|
308
|
+
return "0x" + value.toString(16);
|
|
309
|
+
if (value == null || typeof (value) !== "object")
|
|
310
|
+
return value;
|
|
311
|
+
if (Array.isArray(value)) {
|
|
312
|
+
return value.map(replace);
|
|
313
|
+
}
|
|
314
|
+
const mapped = {};
|
|
315
|
+
for (const key of Object.keys(value)) {
|
|
316
|
+
mapped[key] = replace(value[key]);
|
|
317
|
+
}
|
|
318
|
+
return mapped;
|
|
319
|
+
};
|
|
320
|
+
return replace(obj);
|
|
321
|
+
}
|
|
322
|
+
exports.replaceBigInts = replaceBigInts;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atomiqlabs/chain-starknet",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.4.0",
|
|
4
4
|
"description": "Starknet specific base implementation",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"url": "git+https://github.com/atomiqlabs/atomiq-chain-starknet.git"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@atomiqlabs/base": "^13.
|
|
34
|
+
"@atomiqlabs/base": "^13.4.0",
|
|
35
35
|
"@noble/hashes": "^1.7.1",
|
|
36
36
|
"@scure/btc-signer": "^1.6.0",
|
|
37
37
|
"abi-wan-kanabi": "2.2.4",
|
|
@@ -152,7 +152,7 @@ export function initializeStarknet(
|
|
|
152
152
|
const chainId = options.chainId ??
|
|
153
153
|
(network===BitcoinNetwork.MAINNET ? constants.StarknetChainId.SN_MAIN : constants.StarknetChainId.SN_SEPOLIA);
|
|
154
154
|
|
|
155
|
-
const chainInterface = new StarknetChainInterface(chainId, provider, wsChannel, Fees, options.starknetConfig);
|
|
155
|
+
const chainInterface = new StarknetChainInterface(chainId, provider, wsChannel, Fees, options.starknetConfig, network);
|
|
156
156
|
|
|
157
157
|
const btcRelay = new StarknetBtcRelay(
|
|
158
158
|
chainInterface, bitcoinRpc, network, options.btcRelayContract, options.btcRelayContractDeploymentHeight
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {Provider, constants, stark, ec, Account, provider, wallet, WebSocketChannel} from "starknet";
|
|
1
|
+
import {Provider, constants, stark, ec, Account, provider, wallet, WebSocketChannel, logger} from "starknet";
|
|
2
2
|
import {getLogger, toHex} from "../../utils/Utils";
|
|
3
3
|
import {SignedStarknetTx, StarknetTransactions, StarknetTx} from "./modules/StarknetTransactions";
|
|
4
4
|
import {StarknetFees} from "./modules/StarknetFees";
|
|
@@ -8,7 +8,7 @@ import {StarknetEvents} from "./modules/StarknetEvents";
|
|
|
8
8
|
import {StarknetSignatures} from "./modules/StarknetSignatures";
|
|
9
9
|
import {StarknetAccounts} from "./modules/StarknetAccounts";
|
|
10
10
|
import {StarknetBlocks} from "./modules/StarknetBlocks";
|
|
11
|
-
import {ChainInterface, TransactionConfirmationOptions} from "@atomiqlabs/base";
|
|
11
|
+
import {BitcoinNetwork, ChainInterface, TransactionConfirmationOptions} from "@atomiqlabs/base";
|
|
12
12
|
import {StarknetSigner} from "../wallet/StarknetSigner";
|
|
13
13
|
import {Buffer} from "buffer";
|
|
14
14
|
import {StarknetKeypairWallet} from "../wallet/accounts/StarknetKeypairWallet";
|
|
@@ -76,12 +76,15 @@ export class StarknetChainInterface implements ChainInterface<StarknetTx, Signed
|
|
|
76
76
|
|
|
77
77
|
public readonly config: StarknetConfig;
|
|
78
78
|
|
|
79
|
+
private readonly bitcoinNetwork?: BitcoinNetwork;
|
|
80
|
+
|
|
79
81
|
constructor(
|
|
80
82
|
chainId: constants.StarknetChainId,
|
|
81
83
|
provider: Provider,
|
|
82
84
|
wsChannel?: WebSocketChannel,
|
|
83
85
|
feeEstimator: StarknetFees = new StarknetFees(provider),
|
|
84
|
-
options?: StarknetConfig
|
|
86
|
+
options?: StarknetConfig,
|
|
87
|
+
bitcoinNetwork?: BitcoinNetwork
|
|
85
88
|
) {
|
|
86
89
|
this.starknetChainId = chainId;
|
|
87
90
|
this.provider = provider;
|
|
@@ -100,6 +103,8 @@ export class StarknetChainInterface implements ChainInterface<StarknetTx, Signed
|
|
|
100
103
|
this.Events = new StarknetEvents(this);
|
|
101
104
|
this.Accounts = new StarknetAccounts(this);
|
|
102
105
|
this.Blocks = new StarknetBlocks(this);
|
|
106
|
+
|
|
107
|
+
this.bitcoinNetwork = bitcoinNetwork;
|
|
103
108
|
}
|
|
104
109
|
|
|
105
110
|
/**
|
|
@@ -305,4 +310,18 @@ export class StarknetChainInterface implements ChainInterface<StarknetTx, Signed
|
|
|
305
310
|
}
|
|
306
311
|
}
|
|
307
312
|
|
|
313
|
+
async verifyNetwork(bitcoinNetwork: BitcoinNetwork): Promise<void> {
|
|
314
|
+
if(this.bitcoinNetwork!=null && bitcoinNetwork!==this.bitcoinNetwork)
|
|
315
|
+
throw new Error(`Network mismatch, the chain interface was not setup for ${BitcoinNetwork[bitcoinNetwork]}, chain interface network: ${BitcoinNetwork[this.bitcoinNetwork]}`);
|
|
316
|
+
|
|
317
|
+
const chainId = await this.provider.getChainId();
|
|
318
|
+
if(chainId!==constants.StarknetChainId.SN_MAIN && chainId!==constants.StarknetChainId.SN_SEPOLIA) {
|
|
319
|
+
logger.warn(`verifyNetwork(): Using non-standard chainId ${chainId}, skipping network verfication!`);
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if(this.starknetChainId!==chainId)
|
|
324
|
+
throw new Error(`Network mismatch, the underlying RPC provider isn't using the correct chainId, expected: ${this.starknetChainId}, provider returned: ${chainId}`);
|
|
325
|
+
}
|
|
326
|
+
|
|
308
327
|
}
|
|
@@ -97,7 +97,7 @@ export type StarknetTx = StarknetTxInvoke | StarknetTxDeployAccount;
|
|
|
97
97
|
* Represents a signed Starknet transactions, which can either be an "INVOKE" or "DEPLOY_ACCOUNT" type, use the
|
|
98
98
|
* {@link isStarknetTxInvoke} & {@link isStarknetTxDeployAccount} to narrow down the type.
|
|
99
99
|
*
|
|
100
|
-
* @
|
|
100
|
+
* @remarks For Starknet this is just an alias for {@link StarknetTx}
|
|
101
101
|
*
|
|
102
102
|
* @category Chain Interface
|
|
103
103
|
*/
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {SwapData, ChainSwapType} from "@atomiqlabs/base";
|
|
2
2
|
import {TimelockRefundHandler} from "./handlers/refund/TimelockRefundHandler";
|
|
3
3
|
import {BigNumberish, cairo, CairoOption, CairoOptionVariant, hash} from "starknet";
|
|
4
|
-
import {Serialized, toBigInt, toHex} from "../../utils/Utils";
|
|
4
|
+
import {replaceBigInts, Serialized, toBigInt, toHex} from "../../utils/Utils";
|
|
5
5
|
import {
|
|
6
6
|
StringToPrimitiveType
|
|
7
7
|
} from "abi-wan-kanabi/dist/kanabi";
|
|
@@ -582,6 +582,13 @@ export class StarknetSwapData extends SwapData {
|
|
|
582
582
|
return this.successAction != null;
|
|
583
583
|
}
|
|
584
584
|
|
|
585
|
+
/**
|
|
586
|
+
* @inheritDoc
|
|
587
|
+
*/
|
|
588
|
+
getEscrowStruct(): any {
|
|
589
|
+
return replaceBigInts(this.toEscrowStruct());
|
|
590
|
+
}
|
|
591
|
+
|
|
585
592
|
}
|
|
586
593
|
|
|
587
594
|
SwapData.deserializers["strk"] = StarknetSwapData;
|
package/src/utils/Utils.ts
CHANGED
|
@@ -341,3 +341,22 @@ export function deserializeResourceBounds(resourceBounds: ResourceBounds): Resou
|
|
|
341
341
|
}
|
|
342
342
|
};
|
|
343
343
|
}
|
|
344
|
+
|
|
345
|
+
export function replaceBigInts<T>(obj: T): ReplaceBigInt<T> {
|
|
346
|
+
const replace = (value: any): any => {
|
|
347
|
+
if(typeof(value)==="bigint") return "0x"+value.toString(16);
|
|
348
|
+
if(value==null || typeof(value)!=="object") return value;
|
|
349
|
+
|
|
350
|
+
if(Array.isArray(value)) {
|
|
351
|
+
return value.map(replace);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const mapped: any = {};
|
|
355
|
+
for(const key of Object.keys(value)) {
|
|
356
|
+
mapped[key] = replace(value[key]);
|
|
357
|
+
}
|
|
358
|
+
return mapped;
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
return replace(obj);
|
|
362
|
+
}
|