starknet 4.13.2 → 4.15.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/CHANGELOG.md +39 -0
- package/CODE_OF_CONDUCT.md +128 -0
- package/README.md +2 -2
- package/__mocks__/naming_compiled.json +53283 -0
- package/__mocks__/starknetId_compiled.json +44703 -0
- package/__tests__/account.test.ts +100 -15
- package/__tests__/contract.test.ts +70 -57
- package/__tests__/defaultProvider.test.ts +14 -13
- package/__tests__/fixtures.ts +27 -26
- package/__tests__/rpcProvider.test.ts +19 -16
- package/__tests__/sequencerProvider.test.ts +47 -55
- package/__tests__/utils/starknetId.test.ts +53 -0
- package/__tests__/utils/utils.test.ts +10 -0
- package/dist/index.d.ts +116 -30
- package/dist/index.global.js +660 -463
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +247 -51
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +247 -51
- package/dist/index.mjs.map +1 -1
- package/index.d.ts +116 -30
- package/index.global.js +660 -463
- package/index.global.js.map +1 -1
- package/index.js +247 -51
- package/index.js.map +1 -1
- package/index.mjs +247 -51
- package/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/account/default.ts +81 -8
- package/src/account/interface.ts +71 -5
- package/src/contract/contractFactory.ts +20 -13
- package/src/contract/default.ts +11 -2
- package/src/provider/default.ts +26 -11
- package/src/provider/interface.ts +9 -3
- package/src/provider/rpc.ts +15 -11
- package/src/provider/sequencer.ts +32 -20
- package/src/provider/utils.ts +19 -11
- package/src/types/account.ts +21 -1
- package/src/types/lib.ts +5 -2
- package/src/types/provider.ts +0 -5
- package/src/utils/events.ts +32 -0
- package/src/utils/number.ts +6 -0
- package/src/utils/starknetId.ts +116 -0
- package/www/docs/API/account.md +176 -2
- package/www/docs/API/contractFactory.md +7 -11
- package/www/docs/API/provider.md +5 -9
- package/www/docs/API/utils.md +8 -0
- package/www/guides/account.md +89 -38
- package/www/guides/erc20.md +115 -59
- package/www/guides/intro.md +11 -4
package/index.js
CHANGED
|
@@ -62,7 +62,7 @@ __export(src_exports, {
|
|
|
62
62
|
module.exports = __toCommonJS(src_exports);
|
|
63
63
|
|
|
64
64
|
// src/contract/default.ts
|
|
65
|
-
var
|
|
65
|
+
var import_bn3 = __toESM(require("bn.js"));
|
|
66
66
|
var import_minimalistic_assert4 = __toESM(require("minimalistic-assert"));
|
|
67
67
|
|
|
68
68
|
// src/utils/fetchPonyfill.ts
|
|
@@ -119,6 +119,7 @@ __export(number_exports, {
|
|
|
119
119
|
assertInRange: () => assertInRange,
|
|
120
120
|
bigNumberishArrayToDecimalStringArray: () => bigNumberishArrayToDecimalStringArray,
|
|
121
121
|
bigNumberishArrayToHexadecimalStringArray: () => bigNumberishArrayToHexadecimalStringArray,
|
|
122
|
+
cleanHex: () => cleanHex,
|
|
122
123
|
getDecimalString: () => getDecimalString,
|
|
123
124
|
getHexString: () => getHexString,
|
|
124
125
|
getHexStringArray: () => getHexStringArray,
|
|
@@ -221,6 +222,7 @@ function toFelt(num) {
|
|
|
221
222
|
}
|
|
222
223
|
return toBN(num).toString();
|
|
223
224
|
}
|
|
225
|
+
var cleanHex = (hex) => hex.toLowerCase().replace(/^(0x)0+/, "$1");
|
|
224
226
|
function assertInRange(input, lowerBound, upperBound, inputName = "") {
|
|
225
227
|
const messageSuffix = inputName === "" ? "invalid length" : `invalid ${inputName} length`;
|
|
226
228
|
const inputBn = toBN(input);
|
|
@@ -2652,6 +2654,8 @@ var RPCResponseParser = class {
|
|
|
2652
2654
|
};
|
|
2653
2655
|
|
|
2654
2656
|
// src/provider/utils.ts
|
|
2657
|
+
var import_bn2 = require("bn.js");
|
|
2658
|
+
var validBlockTags = ["latest", "pending"];
|
|
2655
2659
|
var Block = class {
|
|
2656
2660
|
constructor(_identifier) {
|
|
2657
2661
|
this.hash = null;
|
|
@@ -2659,17 +2663,21 @@ var Block = class {
|
|
|
2659
2663
|
this.tag = null;
|
|
2660
2664
|
this.valueOf = () => this.number;
|
|
2661
2665
|
this.toString = () => this.hash;
|
|
2662
|
-
this.setIdentifier = function(__identifier) {
|
|
2663
|
-
if (typeof __identifier === "string" && isHex(__identifier)) {
|
|
2664
|
-
this.hash = __identifier;
|
|
2665
|
-
} else if (typeof __identifier === "number") {
|
|
2666
|
-
this.number = __identifier;
|
|
2667
|
-
} else {
|
|
2668
|
-
this.tag = __identifier;
|
|
2669
|
-
}
|
|
2670
|
-
};
|
|
2671
2666
|
this.setIdentifier(_identifier);
|
|
2672
2667
|
}
|
|
2668
|
+
setIdentifier(__identifier) {
|
|
2669
|
+
if (typeof __identifier === "string" && isHex(__identifier)) {
|
|
2670
|
+
this.hash = __identifier;
|
|
2671
|
+
} else if (import_bn2.BN.isBN(__identifier)) {
|
|
2672
|
+
this.hash = toHex(__identifier);
|
|
2673
|
+
} else if (typeof __identifier === "number") {
|
|
2674
|
+
this.number = __identifier;
|
|
2675
|
+
} else if (typeof __identifier === "string" && validBlockTags.includes(__identifier)) {
|
|
2676
|
+
this.tag = __identifier;
|
|
2677
|
+
} else {
|
|
2678
|
+
this.tag = "pending";
|
|
2679
|
+
}
|
|
2680
|
+
}
|
|
2673
2681
|
get queryIdentifier() {
|
|
2674
2682
|
if (this.number !== null) {
|
|
2675
2683
|
return `blockNumber=${this.number}`;
|
|
@@ -2756,7 +2764,7 @@ var RpcProvider = class {
|
|
|
2756
2764
|
contract_address: contractAddress
|
|
2757
2765
|
});
|
|
2758
2766
|
}
|
|
2759
|
-
async
|
|
2767
|
+
async getNonceForAddress(contractAddress, blockIdentifier = "pending") {
|
|
2760
2768
|
const block_id = new Block(blockIdentifier).identifier;
|
|
2761
2769
|
return this.fetchEndpoint("starknet_getNonce", {
|
|
2762
2770
|
contract_address: contractAddress,
|
|
@@ -2941,25 +2949,25 @@ var RpcProvider = class {
|
|
|
2941
2949
|
async traceBlockTransactions(blockHash) {
|
|
2942
2950
|
return this.fetchEndpoint("starknet_traceBlockTransactions", { block_hash: blockHash });
|
|
2943
2951
|
}
|
|
2944
|
-
async waitForTransaction(txHash, retryInterval = 8e3) {
|
|
2952
|
+
async waitForTransaction(txHash, retryInterval = 8e3, successStates = ["ACCEPTED_ON_L1", "ACCEPTED_ON_L2", "PENDING"]) {
|
|
2953
|
+
const errorStates = ["REJECTED", "NOT_RECEIVED"];
|
|
2945
2954
|
let { retries } = this;
|
|
2946
2955
|
let onchain = false;
|
|
2956
|
+
let txReceipt = {};
|
|
2947
2957
|
while (!onchain) {
|
|
2948
|
-
const successStates = ["ACCEPTED_ON_L1", "ACCEPTED_ON_L2", "PENDING"];
|
|
2949
|
-
const errorStates = ["REJECTED", "NOT_RECEIVED"];
|
|
2950
2958
|
await wait(retryInterval);
|
|
2951
2959
|
try {
|
|
2952
|
-
|
|
2953
|
-
if (!("status" in
|
|
2960
|
+
txReceipt = await this.getTransactionReceipt(txHash);
|
|
2961
|
+
if (!("status" in txReceipt)) {
|
|
2954
2962
|
const error = new Error("pending transaction");
|
|
2955
2963
|
throw error;
|
|
2956
2964
|
}
|
|
2957
|
-
if (
|
|
2965
|
+
if (txReceipt.status && successStates.includes(txReceipt.status)) {
|
|
2958
2966
|
onchain = true;
|
|
2959
|
-
} else if (
|
|
2960
|
-
const message =
|
|
2967
|
+
} else if (txReceipt.status && errorStates.includes(txReceipt.status)) {
|
|
2968
|
+
const message = txReceipt.status;
|
|
2961
2969
|
const error = new Error(message);
|
|
2962
|
-
error.response =
|
|
2970
|
+
error.response = txReceipt;
|
|
2963
2971
|
throw error;
|
|
2964
2972
|
}
|
|
2965
2973
|
} catch (error) {
|
|
@@ -2973,6 +2981,7 @@ var RpcProvider = class {
|
|
|
2973
2981
|
retries -= 1;
|
|
2974
2982
|
}
|
|
2975
2983
|
await wait(retryInterval);
|
|
2984
|
+
return txReceipt;
|
|
2976
2985
|
}
|
|
2977
2986
|
async getTransactionCount(blockIdentifier = "pending") {
|
|
2978
2987
|
const block_id = new Block(blockIdentifier).identifier;
|
|
@@ -3131,8 +3140,11 @@ var HttpError = class extends import_ts_custom_error.CustomError {
|
|
|
3131
3140
|
function isEmptyQueryObject(obj) {
|
|
3132
3141
|
return obj === void 0 || Object.keys(obj).length === 0 || Object.keys(obj).length === 1 && Object.entries(obj).every(([k, v]) => k === "blockIdentifier" && v === null);
|
|
3133
3142
|
}
|
|
3143
|
+
var defaultOptions = {
|
|
3144
|
+
network: "goerli-alpha-2"
|
|
3145
|
+
};
|
|
3134
3146
|
var SequencerProvider = class {
|
|
3135
|
-
constructor(optionsOrProvider =
|
|
3147
|
+
constructor(optionsOrProvider = defaultOptions) {
|
|
3136
3148
|
this.responseParser = new SequencerAPIResponseParser();
|
|
3137
3149
|
if ("network" in optionsOrProvider) {
|
|
3138
3150
|
this.baseUrl = SequencerProvider.getNetworkFromName(optionsOrProvider.network);
|
|
@@ -3169,6 +3181,9 @@ var SequencerProvider = class {
|
|
|
3169
3181
|
if (url.host.includes("mainnet.starknet.io")) {
|
|
3170
3182
|
return "0x534e5f4d41494e" /* MAINNET */;
|
|
3171
3183
|
}
|
|
3184
|
+
if (url.host.includes("alpha4-2.starknet.io")) {
|
|
3185
|
+
return "0x534e5f474f45524c4932" /* TESTNET2 */;
|
|
3186
|
+
}
|
|
3172
3187
|
} catch {
|
|
3173
3188
|
console.error(`Could not parse baseUrl: ${baseUrl}`);
|
|
3174
3189
|
}
|
|
@@ -3271,7 +3286,7 @@ var SequencerProvider = class {
|
|
|
3271
3286
|
this.responseParser.parseGetBlockResponse
|
|
3272
3287
|
);
|
|
3273
3288
|
}
|
|
3274
|
-
async
|
|
3289
|
+
async getNonceForAddress(contractAddress, blockIdentifier = "pending") {
|
|
3275
3290
|
return this.fetchEndpoint("get_nonce", { contractAddress, blockIdentifier });
|
|
3276
3291
|
}
|
|
3277
3292
|
async getStorageAt(contractAddress, key, blockIdentifier = "pending") {
|
|
@@ -3401,13 +3416,13 @@ var SequencerProvider = class {
|
|
|
3401
3416
|
async getCode(contractAddress, blockIdentifier = "pending") {
|
|
3402
3417
|
return this.fetchEndpoint("get_code", { contractAddress, blockIdentifier });
|
|
3403
3418
|
}
|
|
3404
|
-
async waitForTransaction(txHash, retryInterval = 8e3) {
|
|
3419
|
+
async waitForTransaction(txHash, retryInterval = 8e3, successStates = ["ACCEPTED_ON_L1", "ACCEPTED_ON_L2", "PENDING"]) {
|
|
3420
|
+
const errorStates = ["REJECTED", "NOT_RECEIVED"];
|
|
3405
3421
|
let onchain = false;
|
|
3422
|
+
let res;
|
|
3406
3423
|
while (!onchain) {
|
|
3407
3424
|
await wait(retryInterval);
|
|
3408
|
-
|
|
3409
|
-
const successStates = ["ACCEPTED_ON_L1", "ACCEPTED_ON_L2", "PENDING"];
|
|
3410
|
-
const errorStates = ["REJECTED", "NOT_RECEIVED"];
|
|
3425
|
+
res = await this.getTransactionStatus(txHash);
|
|
3411
3426
|
if (successStates.includes(res.tx_status)) {
|
|
3412
3427
|
onchain = true;
|
|
3413
3428
|
} else if (errorStates.includes(res.tx_status)) {
|
|
@@ -3418,6 +3433,8 @@ ${res.tx_failure_reason.error_message}` : res.tx_status;
|
|
|
3418
3433
|
throw error;
|
|
3419
3434
|
}
|
|
3420
3435
|
}
|
|
3436
|
+
const txReceipt = await this.getTransactionReceipt(txHash);
|
|
3437
|
+
return txReceipt;
|
|
3421
3438
|
}
|
|
3422
3439
|
async getTransactionStatus(txHash) {
|
|
3423
3440
|
const txHashHex = toHex(toBN(txHash));
|
|
@@ -3444,11 +3461,13 @@ ${res.tx_failure_reason.error_message}` : res.tx_status;
|
|
|
3444
3461
|
// src/provider/default.ts
|
|
3445
3462
|
var Provider = class {
|
|
3446
3463
|
constructor(providerOrOptions) {
|
|
3447
|
-
if (providerOrOptions
|
|
3464
|
+
if (providerOrOptions instanceof Provider) {
|
|
3465
|
+
this.provider = providerOrOptions.provider;
|
|
3466
|
+
} else if (providerOrOptions instanceof RpcProvider || providerOrOptions instanceof SequencerProvider) {
|
|
3448
3467
|
this.provider = providerOrOptions;
|
|
3449
|
-
} else if (providerOrOptions
|
|
3468
|
+
} else if (providerOrOptions && "rpc" in providerOrOptions) {
|
|
3450
3469
|
this.provider = new RpcProvider(providerOrOptions.rpc);
|
|
3451
|
-
} else if (providerOrOptions
|
|
3470
|
+
} else if (providerOrOptions && "sequencer" in providerOrOptions) {
|
|
3452
3471
|
this.provider = new SequencerProvider(providerOrOptions.sequencer);
|
|
3453
3472
|
} else {
|
|
3454
3473
|
this.provider = new SequencerProvider();
|
|
@@ -3482,8 +3501,8 @@ var Provider = class {
|
|
|
3482
3501
|
blockIdentifier
|
|
3483
3502
|
);
|
|
3484
3503
|
}
|
|
3485
|
-
async
|
|
3486
|
-
return this.provider.
|
|
3504
|
+
async getNonceForAddress(contractAddress, blockIdentifier) {
|
|
3505
|
+
return this.provider.getNonceForAddress(contractAddress, blockIdentifier);
|
|
3487
3506
|
}
|
|
3488
3507
|
async getStorageAt(contractAddress, key, blockIdentifier = "pending") {
|
|
3489
3508
|
return this.provider.getStorageAt(contractAddress, key, blockIdentifier);
|
|
@@ -3518,8 +3537,8 @@ var Provider = class {
|
|
|
3518
3537
|
async getCode(contractAddress, blockIdentifier) {
|
|
3519
3538
|
return this.provider.getCode(contractAddress, blockIdentifier);
|
|
3520
3539
|
}
|
|
3521
|
-
async waitForTransaction(txHash, retryInterval) {
|
|
3522
|
-
return this.provider.waitForTransaction(txHash, retryInterval);
|
|
3540
|
+
async waitForTransaction(txHash, retryInterval, successStates) {
|
|
3541
|
+
return this.provider.waitForTransaction(txHash, retryInterval, successStates);
|
|
3523
3542
|
}
|
|
3524
3543
|
};
|
|
3525
3544
|
|
|
@@ -3535,12 +3554,18 @@ function parseFelt(candidate) {
|
|
|
3535
3554
|
try {
|
|
3536
3555
|
return toBN(candidate);
|
|
3537
3556
|
} catch (e) {
|
|
3538
|
-
throw Error("
|
|
3557
|
+
throw Error("Could not parse felt");
|
|
3539
3558
|
}
|
|
3540
3559
|
}
|
|
3541
3560
|
function buildCall(contract, functionAbi) {
|
|
3542
3561
|
return async function(...args) {
|
|
3543
|
-
|
|
3562
|
+
let blockIdentifier = null;
|
|
3563
|
+
args.forEach((arg) => {
|
|
3564
|
+
if (arg.blockIdentifier) {
|
|
3565
|
+
blockIdentifier = arg.blockIdentifier;
|
|
3566
|
+
}
|
|
3567
|
+
});
|
|
3568
|
+
return contract.call(functionAbi.name, args, { blockIdentifier });
|
|
3544
3569
|
};
|
|
3545
3570
|
}
|
|
3546
3571
|
function buildInvoke(contract, functionAbi) {
|
|
@@ -3765,7 +3790,7 @@ var Contract = class {
|
|
|
3765
3790
|
}
|
|
3766
3791
|
if (input.type === "felt") {
|
|
3767
3792
|
(0, import_minimalistic_assert4.default)(
|
|
3768
|
-
typeof args[argPosition] === "string" || typeof args[argPosition] === "number" || args[argPosition] instanceof
|
|
3793
|
+
typeof args[argPosition] === "string" || typeof args[argPosition] === "number" || args[argPosition] instanceof import_bn3.default,
|
|
3769
3794
|
`arg ${input.name} should be a felt (string, number, BigNumber)`
|
|
3770
3795
|
);
|
|
3771
3796
|
argPosition += 1;
|
|
@@ -3790,7 +3815,7 @@ var Contract = class {
|
|
|
3790
3815
|
if (input.type === "felt*") {
|
|
3791
3816
|
args[argPosition].forEach((felt) => {
|
|
3792
3817
|
(0, import_minimalistic_assert4.default)(
|
|
3793
|
-
typeof felt === "string" || typeof felt === "number" || felt instanceof
|
|
3818
|
+
typeof felt === "string" || typeof felt === "number" || felt instanceof import_bn3.default,
|
|
3794
3819
|
`arg ${input.name} should be an array of string, number or BigNumber`
|
|
3795
3820
|
);
|
|
3796
3821
|
});
|
|
@@ -3803,7 +3828,7 @@ var Contract = class {
|
|
|
3803
3828
|
);
|
|
3804
3829
|
args[argPosition].forEach((felt) => {
|
|
3805
3830
|
(0, import_minimalistic_assert4.default)(
|
|
3806
|
-
typeof felt === "string" || typeof felt === "number" || felt instanceof
|
|
3831
|
+
typeof felt === "string" || typeof felt === "number" || felt instanceof import_bn3.default,
|
|
3807
3832
|
`arg ${input.name} should be an array of string, number or BigNumber`
|
|
3808
3833
|
);
|
|
3809
3834
|
});
|
|
@@ -3960,35 +3985,42 @@ var ContractInterface = class {
|
|
|
3960
3985
|
// src/contract/contractFactory.ts
|
|
3961
3986
|
var import_minimalistic_assert5 = __toESM(require("minimalistic-assert"));
|
|
3962
3987
|
var ContractFactory = class {
|
|
3963
|
-
constructor(compiledContract,
|
|
3988
|
+
constructor(compiledContract, classHash, account, abi = compiledContract.abi) {
|
|
3964
3989
|
this.abi = abi;
|
|
3965
3990
|
this.compiledContract = compiledContract;
|
|
3966
|
-
this.
|
|
3991
|
+
this.account = account;
|
|
3992
|
+
this.classHash = classHash;
|
|
3967
3993
|
}
|
|
3968
3994
|
async deploy(constructorCalldata, addressSalt) {
|
|
3969
|
-
const {
|
|
3995
|
+
const {
|
|
3996
|
+
deploy: { contract_address, transaction_hash }
|
|
3997
|
+
} = await this.account.declareDeploy({
|
|
3970
3998
|
contract: this.compiledContract,
|
|
3999
|
+
classHash: this.classHash,
|
|
3971
4000
|
constructorCalldata,
|
|
3972
|
-
addressSalt
|
|
4001
|
+
salt: addressSalt
|
|
3973
4002
|
});
|
|
3974
4003
|
(0, import_minimalistic_assert5.default)(Boolean(contract_address), "Deployment of the contract failed");
|
|
3975
4004
|
const contractInstance = new Contract(
|
|
3976
4005
|
this.compiledContract.abi,
|
|
3977
4006
|
contract_address,
|
|
3978
|
-
this.
|
|
4007
|
+
this.account
|
|
3979
4008
|
);
|
|
3980
4009
|
contractInstance.deployTransactionHash = transaction_hash;
|
|
3981
4010
|
return contractInstance;
|
|
3982
4011
|
}
|
|
3983
|
-
connect(
|
|
3984
|
-
this.
|
|
4012
|
+
connect(account) {
|
|
4013
|
+
this.account = account;
|
|
3985
4014
|
return this;
|
|
3986
4015
|
}
|
|
3987
4016
|
attach(address) {
|
|
3988
|
-
return new Contract(this.abi, address, this.
|
|
4017
|
+
return new Contract(this.abi, address, this.account);
|
|
3989
4018
|
}
|
|
3990
4019
|
};
|
|
3991
4020
|
|
|
4021
|
+
// src/account/default.ts
|
|
4022
|
+
var import_bn5 = require("bn.js");
|
|
4023
|
+
|
|
3992
4024
|
// src/signer/interface.ts
|
|
3993
4025
|
var SignerInterface = class {
|
|
3994
4026
|
};
|
|
@@ -4329,6 +4361,122 @@ var Signer = class {
|
|
|
4329
4361
|
}
|
|
4330
4362
|
};
|
|
4331
4363
|
|
|
4364
|
+
// src/utils/events.ts
|
|
4365
|
+
function parseUDCEvent(txReceipt) {
|
|
4366
|
+
if (!txReceipt.events) {
|
|
4367
|
+
throw new Error("UDC emited event is empty");
|
|
4368
|
+
}
|
|
4369
|
+
const event = txReceipt.events.find(
|
|
4370
|
+
(it) => cleanHex(it.from_address) === cleanHex(UDC.ADDRESS)
|
|
4371
|
+
) || {
|
|
4372
|
+
data: []
|
|
4373
|
+
};
|
|
4374
|
+
return {
|
|
4375
|
+
transaction_hash: txReceipt.transaction_hash,
|
|
4376
|
+
contract_address: event.data[0],
|
|
4377
|
+
address: event.data[0],
|
|
4378
|
+
deployer: event.data[1],
|
|
4379
|
+
unique: event.data[2],
|
|
4380
|
+
classHash: event.data[3],
|
|
4381
|
+
calldata_len: event.data[4],
|
|
4382
|
+
calldata: event.data.slice(5, 5 + parseInt(event.data[4], 16)),
|
|
4383
|
+
salt: event.data[event.data.length - 1]
|
|
4384
|
+
};
|
|
4385
|
+
}
|
|
4386
|
+
|
|
4387
|
+
// src/utils/starknetId.ts
|
|
4388
|
+
var import_bn4 = __toESM(require("bn.js"));
|
|
4389
|
+
var basicAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789-";
|
|
4390
|
+
var basicSizePlusOne = new import_bn4.default(basicAlphabet.length + 1);
|
|
4391
|
+
var bigAlphabet = "\u8FD9\u6765";
|
|
4392
|
+
var basicAlphabetSize = new import_bn4.default(basicAlphabet.length);
|
|
4393
|
+
var bigAlphabetSize = new import_bn4.default(bigAlphabet.length);
|
|
4394
|
+
var bigAlphabetSizePlusOne = new import_bn4.default(bigAlphabet.length + 1);
|
|
4395
|
+
function extractStars(str) {
|
|
4396
|
+
let k = 0;
|
|
4397
|
+
while (str.endsWith(bigAlphabet[bigAlphabet.length - 1])) {
|
|
4398
|
+
str = str.substring(0, str.length - 1);
|
|
4399
|
+
k += 1;
|
|
4400
|
+
}
|
|
4401
|
+
return [str, k];
|
|
4402
|
+
}
|
|
4403
|
+
function useDecoded(encoded) {
|
|
4404
|
+
let decoded = "";
|
|
4405
|
+
encoded.forEach((subdomain) => {
|
|
4406
|
+
while (!subdomain.isZero()) {
|
|
4407
|
+
const code = subdomain.mod(basicSizePlusOne).toNumber();
|
|
4408
|
+
subdomain = subdomain.div(basicSizePlusOne);
|
|
4409
|
+
if (code === basicAlphabet.length) {
|
|
4410
|
+
const nextSubdomain = subdomain.div(bigAlphabetSizePlusOne);
|
|
4411
|
+
if (nextSubdomain.isZero()) {
|
|
4412
|
+
const code2 = subdomain.mod(bigAlphabetSizePlusOne).toNumber();
|
|
4413
|
+
subdomain = nextSubdomain;
|
|
4414
|
+
if (code2 === 0)
|
|
4415
|
+
decoded += basicAlphabet[0];
|
|
4416
|
+
else
|
|
4417
|
+
decoded += bigAlphabet[code2 - 1];
|
|
4418
|
+
} else {
|
|
4419
|
+
const code2 = subdomain.mod(bigAlphabetSize).toNumber();
|
|
4420
|
+
decoded += bigAlphabet[code2];
|
|
4421
|
+
subdomain = subdomain.div(bigAlphabetSize);
|
|
4422
|
+
}
|
|
4423
|
+
} else
|
|
4424
|
+
decoded += basicAlphabet[code];
|
|
4425
|
+
}
|
|
4426
|
+
const [str, k] = extractStars(decoded);
|
|
4427
|
+
if (k)
|
|
4428
|
+
decoded = str + (k % 2 === 0 ? bigAlphabet[bigAlphabet.length - 1].repeat(k / 2 - 1) + bigAlphabet[0] + basicAlphabet[1] : bigAlphabet[bigAlphabet.length - 1].repeat((k - 1) / 2 + 1));
|
|
4429
|
+
decoded += ".";
|
|
4430
|
+
});
|
|
4431
|
+
return decoded.concat("stark");
|
|
4432
|
+
}
|
|
4433
|
+
function useEncoded(decoded) {
|
|
4434
|
+
let encoded = new import_bn4.default(0);
|
|
4435
|
+
let multiplier = new import_bn4.default(1);
|
|
4436
|
+
if (decoded.endsWith(bigAlphabet[0] + basicAlphabet[1])) {
|
|
4437
|
+
const [str, k] = extractStars(decoded.substring(0, decoded.length - 2));
|
|
4438
|
+
decoded = str + bigAlphabet[bigAlphabet.length - 1].repeat(2 * (k + 1));
|
|
4439
|
+
} else {
|
|
4440
|
+
const [str, k] = extractStars(decoded);
|
|
4441
|
+
if (k)
|
|
4442
|
+
decoded = str + bigAlphabet[bigAlphabet.length - 1].repeat(1 + 2 * (k - 1));
|
|
4443
|
+
}
|
|
4444
|
+
for (let i = 0; i < decoded.length; i += 1) {
|
|
4445
|
+
const char = decoded[i];
|
|
4446
|
+
const index = basicAlphabet.indexOf(char);
|
|
4447
|
+
const bnIndex = new import_bn4.default(basicAlphabet.indexOf(char));
|
|
4448
|
+
if (index !== -1) {
|
|
4449
|
+
if (i === decoded.length - 1 && decoded[i] === basicAlphabet[0]) {
|
|
4450
|
+
encoded = encoded.add(multiplier.mul(basicAlphabetSize));
|
|
4451
|
+
multiplier = multiplier.mul(basicSizePlusOne);
|
|
4452
|
+
multiplier = multiplier.mul(basicSizePlusOne);
|
|
4453
|
+
} else {
|
|
4454
|
+
encoded = encoded.add(multiplier.mul(bnIndex));
|
|
4455
|
+
multiplier = multiplier.mul(basicSizePlusOne);
|
|
4456
|
+
}
|
|
4457
|
+
} else if (bigAlphabet.indexOf(char) !== -1) {
|
|
4458
|
+
encoded = encoded.add(multiplier.mul(basicAlphabetSize));
|
|
4459
|
+
multiplier = multiplier.mul(basicSizePlusOne);
|
|
4460
|
+
const newid = (i === decoded.length - 1 ? 1 : 0) + bigAlphabet.indexOf(char);
|
|
4461
|
+
encoded = encoded.add(multiplier.mul(new import_bn4.default(newid)));
|
|
4462
|
+
multiplier = multiplier.mul(bigAlphabetSize);
|
|
4463
|
+
}
|
|
4464
|
+
}
|
|
4465
|
+
return encoded;
|
|
4466
|
+
}
|
|
4467
|
+
function getStarknetIdContract(chainId) {
|
|
4468
|
+
const starknetIdMainnetContract = "0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678";
|
|
4469
|
+
const starknetIdTestnetContract = "0x05cf267a0af6101667013fc6bd3f6c11116a14cda9b8c4b1198520d59f900b17";
|
|
4470
|
+
switch (chainId) {
|
|
4471
|
+
case "0x534e5f4d41494e" /* MAINNET */:
|
|
4472
|
+
return starknetIdMainnetContract;
|
|
4473
|
+
case "0x534e5f474f45524c49" /* TESTNET */:
|
|
4474
|
+
return starknetIdTestnetContract;
|
|
4475
|
+
default:
|
|
4476
|
+
throw new Error("Starknet.id is not yet deployed on this network");
|
|
4477
|
+
}
|
|
4478
|
+
}
|
|
4479
|
+
|
|
4332
4480
|
// src/account/default.ts
|
|
4333
4481
|
var Account = class extends Provider {
|
|
4334
4482
|
constructor(providerOrOptions, address, keyPairOrSigner) {
|
|
@@ -4337,7 +4485,41 @@ var Account = class extends Provider {
|
|
|
4337
4485
|
this.signer = "getPubKey" in keyPairOrSigner ? keyPairOrSigner : new Signer(keyPairOrSigner);
|
|
4338
4486
|
}
|
|
4339
4487
|
async getNonce(blockIdentifier) {
|
|
4340
|
-
return super.
|
|
4488
|
+
return super.getNonceForAddress(this.address, blockIdentifier);
|
|
4489
|
+
}
|
|
4490
|
+
async getStarkName(StarknetIdContract) {
|
|
4491
|
+
const chainId = await this.getChainId();
|
|
4492
|
+
const contract = StarknetIdContract ?? getStarknetIdContract(chainId);
|
|
4493
|
+
try {
|
|
4494
|
+
const hexDomain = await this.callContract({
|
|
4495
|
+
contractAddress: contract,
|
|
4496
|
+
entrypoint: "address_to_domain",
|
|
4497
|
+
calldata: compileCalldata({
|
|
4498
|
+
address: this.address
|
|
4499
|
+
})
|
|
4500
|
+
});
|
|
4501
|
+
const decimalDomain = hexDomain.result.map((element) => new import_bn5.BN(hexToDecimalString(element))).slice(1);
|
|
4502
|
+
const stringDomain = useDecoded(decimalDomain);
|
|
4503
|
+
return stringDomain;
|
|
4504
|
+
} catch {
|
|
4505
|
+
return Error("Could not get stark name");
|
|
4506
|
+
}
|
|
4507
|
+
}
|
|
4508
|
+
async getAddressFromStarkName(name, StarknetIdContract) {
|
|
4509
|
+
const chainId = await this.getChainId();
|
|
4510
|
+
const contract = StarknetIdContract ?? getStarknetIdContract(chainId);
|
|
4511
|
+
try {
|
|
4512
|
+
const addressData = await this.callContract({
|
|
4513
|
+
contractAddress: contract,
|
|
4514
|
+
entrypoint: "domain_to_address",
|
|
4515
|
+
calldata: compileCalldata({
|
|
4516
|
+
domain: [useEncoded(name.replace(".stark", "")).toString(10)]
|
|
4517
|
+
})
|
|
4518
|
+
});
|
|
4519
|
+
return addressData.result[0];
|
|
4520
|
+
} catch {
|
|
4521
|
+
return Error("Could not get address from stark name");
|
|
4522
|
+
}
|
|
4341
4523
|
}
|
|
4342
4524
|
async estimateFee(calls, estimateFeeDetails) {
|
|
4343
4525
|
return this.estimateInvokeFee(calls, estimateFeeDetails);
|
|
@@ -4424,7 +4606,7 @@ var Account = class extends Provider {
|
|
|
4424
4606
|
}
|
|
4425
4607
|
async estimateDeployFee({
|
|
4426
4608
|
classHash,
|
|
4427
|
-
salt,
|
|
4609
|
+
salt = "0",
|
|
4428
4610
|
unique = true,
|
|
4429
4611
|
constructorCalldata = [],
|
|
4430
4612
|
additionalCalls = []
|
|
@@ -4505,9 +4687,10 @@ var Account = class extends Provider {
|
|
|
4505
4687
|
unique = true,
|
|
4506
4688
|
constructorCalldata = [],
|
|
4507
4689
|
additionalCalls = []
|
|
4508
|
-
},
|
|
4690
|
+
}, invocationsDetails = {}) {
|
|
4509
4691
|
const compiledConstructorCallData = compileCalldata(constructorCalldata);
|
|
4510
4692
|
const callsArray = Array.isArray(additionalCalls) ? additionalCalls : [additionalCalls];
|
|
4693
|
+
const deploySalt = salt ?? randomAddress();
|
|
4511
4694
|
return this.execute(
|
|
4512
4695
|
[
|
|
4513
4696
|
{
|
|
@@ -4515,7 +4698,7 @@ var Account = class extends Provider {
|
|
|
4515
4698
|
entrypoint: UDC.ENTRYPOINT,
|
|
4516
4699
|
calldata: [
|
|
4517
4700
|
classHash,
|
|
4518
|
-
|
|
4701
|
+
deploySalt,
|
|
4519
4702
|
toCairoBool(unique),
|
|
4520
4703
|
compiledConstructorCallData.length,
|
|
4521
4704
|
...compiledConstructorCallData
|
|
@@ -4524,9 +4707,22 @@ var Account = class extends Provider {
|
|
|
4524
4707
|
...callsArray
|
|
4525
4708
|
],
|
|
4526
4709
|
void 0,
|
|
4527
|
-
|
|
4710
|
+
invocationsDetails
|
|
4528
4711
|
);
|
|
4529
4712
|
}
|
|
4713
|
+
async deployContract(payload, details = {}) {
|
|
4714
|
+
const deployTx = await this.deploy(payload, details);
|
|
4715
|
+
const txReceipt = await this.waitForTransaction(deployTx.transaction_hash, void 0, [
|
|
4716
|
+
"ACCEPTED_ON_L2"
|
|
4717
|
+
]);
|
|
4718
|
+
return parseUDCEvent(txReceipt);
|
|
4719
|
+
}
|
|
4720
|
+
async declareDeploy({ classHash, contract, constructorCalldata }, details) {
|
|
4721
|
+
const { transaction_hash } = await this.declare({ contract, classHash }, details);
|
|
4722
|
+
const declare = await this.waitForTransaction(transaction_hash, void 0, ["ACCEPTED_ON_L2"]);
|
|
4723
|
+
const deploy = await this.deployContract({ classHash, constructorCalldata }, details);
|
|
4724
|
+
return { declare: { ...declare, class_hash: classHash }, deploy };
|
|
4725
|
+
}
|
|
4530
4726
|
async deployAccount({
|
|
4531
4727
|
classHash,
|
|
4532
4728
|
constructorCalldata = [],
|