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.mjs
CHANGED
|
@@ -5,7 +5,7 @@ var __export = (target, all) => {
|
|
|
5
5
|
};
|
|
6
6
|
|
|
7
7
|
// src/contract/default.ts
|
|
8
|
-
import
|
|
8
|
+
import BN3 from "bn.js";
|
|
9
9
|
import assert4 from "minimalistic-assert";
|
|
10
10
|
|
|
11
11
|
// src/utils/fetchPonyfill.ts
|
|
@@ -62,6 +62,7 @@ __export(number_exports, {
|
|
|
62
62
|
assertInRange: () => assertInRange,
|
|
63
63
|
bigNumberishArrayToDecimalStringArray: () => bigNumberishArrayToDecimalStringArray,
|
|
64
64
|
bigNumberishArrayToHexadecimalStringArray: () => bigNumberishArrayToHexadecimalStringArray,
|
|
65
|
+
cleanHex: () => cleanHex,
|
|
65
66
|
getDecimalString: () => getDecimalString,
|
|
66
67
|
getHexString: () => getHexString,
|
|
67
68
|
getHexStringArray: () => getHexStringArray,
|
|
@@ -164,6 +165,7 @@ function toFelt(num) {
|
|
|
164
165
|
}
|
|
165
166
|
return toBN(num).toString();
|
|
166
167
|
}
|
|
168
|
+
var cleanHex = (hex) => hex.toLowerCase().replace(/^(0x)0+/, "$1");
|
|
167
169
|
function assertInRange(input, lowerBound, upperBound, inputName = "") {
|
|
168
170
|
const messageSuffix = inputName === "" ? "invalid length" : `invalid ${inputName} length`;
|
|
169
171
|
const inputBn = toBN(input);
|
|
@@ -2595,6 +2597,8 @@ var RPCResponseParser = class {
|
|
|
2595
2597
|
};
|
|
2596
2598
|
|
|
2597
2599
|
// src/provider/utils.ts
|
|
2600
|
+
import { BN as BN2 } from "bn.js";
|
|
2601
|
+
var validBlockTags = ["latest", "pending"];
|
|
2598
2602
|
var Block = class {
|
|
2599
2603
|
constructor(_identifier) {
|
|
2600
2604
|
this.hash = null;
|
|
@@ -2602,17 +2606,21 @@ var Block = class {
|
|
|
2602
2606
|
this.tag = null;
|
|
2603
2607
|
this.valueOf = () => this.number;
|
|
2604
2608
|
this.toString = () => this.hash;
|
|
2605
|
-
this.setIdentifier = function(__identifier) {
|
|
2606
|
-
if (typeof __identifier === "string" && isHex(__identifier)) {
|
|
2607
|
-
this.hash = __identifier;
|
|
2608
|
-
} else if (typeof __identifier === "number") {
|
|
2609
|
-
this.number = __identifier;
|
|
2610
|
-
} else {
|
|
2611
|
-
this.tag = __identifier;
|
|
2612
|
-
}
|
|
2613
|
-
};
|
|
2614
2609
|
this.setIdentifier(_identifier);
|
|
2615
2610
|
}
|
|
2611
|
+
setIdentifier(__identifier) {
|
|
2612
|
+
if (typeof __identifier === "string" && isHex(__identifier)) {
|
|
2613
|
+
this.hash = __identifier;
|
|
2614
|
+
} else if (BN2.isBN(__identifier)) {
|
|
2615
|
+
this.hash = toHex(__identifier);
|
|
2616
|
+
} else if (typeof __identifier === "number") {
|
|
2617
|
+
this.number = __identifier;
|
|
2618
|
+
} else if (typeof __identifier === "string" && validBlockTags.includes(__identifier)) {
|
|
2619
|
+
this.tag = __identifier;
|
|
2620
|
+
} else {
|
|
2621
|
+
this.tag = "pending";
|
|
2622
|
+
}
|
|
2623
|
+
}
|
|
2616
2624
|
get queryIdentifier() {
|
|
2617
2625
|
if (this.number !== null) {
|
|
2618
2626
|
return `blockNumber=${this.number}`;
|
|
@@ -2699,7 +2707,7 @@ var RpcProvider = class {
|
|
|
2699
2707
|
contract_address: contractAddress
|
|
2700
2708
|
});
|
|
2701
2709
|
}
|
|
2702
|
-
async
|
|
2710
|
+
async getNonceForAddress(contractAddress, blockIdentifier = "pending") {
|
|
2703
2711
|
const block_id = new Block(blockIdentifier).identifier;
|
|
2704
2712
|
return this.fetchEndpoint("starknet_getNonce", {
|
|
2705
2713
|
contract_address: contractAddress,
|
|
@@ -2884,25 +2892,25 @@ var RpcProvider = class {
|
|
|
2884
2892
|
async traceBlockTransactions(blockHash) {
|
|
2885
2893
|
return this.fetchEndpoint("starknet_traceBlockTransactions", { block_hash: blockHash });
|
|
2886
2894
|
}
|
|
2887
|
-
async waitForTransaction(txHash, retryInterval = 8e3) {
|
|
2895
|
+
async waitForTransaction(txHash, retryInterval = 8e3, successStates = ["ACCEPTED_ON_L1", "ACCEPTED_ON_L2", "PENDING"]) {
|
|
2896
|
+
const errorStates = ["REJECTED", "NOT_RECEIVED"];
|
|
2888
2897
|
let { retries } = this;
|
|
2889
2898
|
let onchain = false;
|
|
2899
|
+
let txReceipt = {};
|
|
2890
2900
|
while (!onchain) {
|
|
2891
|
-
const successStates = ["ACCEPTED_ON_L1", "ACCEPTED_ON_L2", "PENDING"];
|
|
2892
|
-
const errorStates = ["REJECTED", "NOT_RECEIVED"];
|
|
2893
2901
|
await wait(retryInterval);
|
|
2894
2902
|
try {
|
|
2895
|
-
|
|
2896
|
-
if (!("status" in
|
|
2903
|
+
txReceipt = await this.getTransactionReceipt(txHash);
|
|
2904
|
+
if (!("status" in txReceipt)) {
|
|
2897
2905
|
const error = new Error("pending transaction");
|
|
2898
2906
|
throw error;
|
|
2899
2907
|
}
|
|
2900
|
-
if (
|
|
2908
|
+
if (txReceipt.status && successStates.includes(txReceipt.status)) {
|
|
2901
2909
|
onchain = true;
|
|
2902
|
-
} else if (
|
|
2903
|
-
const message =
|
|
2910
|
+
} else if (txReceipt.status && errorStates.includes(txReceipt.status)) {
|
|
2911
|
+
const message = txReceipt.status;
|
|
2904
2912
|
const error = new Error(message);
|
|
2905
|
-
error.response =
|
|
2913
|
+
error.response = txReceipt;
|
|
2906
2914
|
throw error;
|
|
2907
2915
|
}
|
|
2908
2916
|
} catch (error) {
|
|
@@ -2916,6 +2924,7 @@ var RpcProvider = class {
|
|
|
2916
2924
|
retries -= 1;
|
|
2917
2925
|
}
|
|
2918
2926
|
await wait(retryInterval);
|
|
2927
|
+
return txReceipt;
|
|
2919
2928
|
}
|
|
2920
2929
|
async getTransactionCount(blockIdentifier = "pending") {
|
|
2921
2930
|
const block_id = new Block(blockIdentifier).identifier;
|
|
@@ -3074,8 +3083,11 @@ var HttpError = class extends CustomError {
|
|
|
3074
3083
|
function isEmptyQueryObject(obj) {
|
|
3075
3084
|
return obj === void 0 || Object.keys(obj).length === 0 || Object.keys(obj).length === 1 && Object.entries(obj).every(([k, v]) => k === "blockIdentifier" && v === null);
|
|
3076
3085
|
}
|
|
3086
|
+
var defaultOptions = {
|
|
3087
|
+
network: "goerli-alpha-2"
|
|
3088
|
+
};
|
|
3077
3089
|
var SequencerProvider = class {
|
|
3078
|
-
constructor(optionsOrProvider =
|
|
3090
|
+
constructor(optionsOrProvider = defaultOptions) {
|
|
3079
3091
|
this.responseParser = new SequencerAPIResponseParser();
|
|
3080
3092
|
if ("network" in optionsOrProvider) {
|
|
3081
3093
|
this.baseUrl = SequencerProvider.getNetworkFromName(optionsOrProvider.network);
|
|
@@ -3112,6 +3124,9 @@ var SequencerProvider = class {
|
|
|
3112
3124
|
if (url.host.includes("mainnet.starknet.io")) {
|
|
3113
3125
|
return "0x534e5f4d41494e" /* MAINNET */;
|
|
3114
3126
|
}
|
|
3127
|
+
if (url.host.includes("alpha4-2.starknet.io")) {
|
|
3128
|
+
return "0x534e5f474f45524c4932" /* TESTNET2 */;
|
|
3129
|
+
}
|
|
3115
3130
|
} catch {
|
|
3116
3131
|
console.error(`Could not parse baseUrl: ${baseUrl}`);
|
|
3117
3132
|
}
|
|
@@ -3214,7 +3229,7 @@ var SequencerProvider = class {
|
|
|
3214
3229
|
this.responseParser.parseGetBlockResponse
|
|
3215
3230
|
);
|
|
3216
3231
|
}
|
|
3217
|
-
async
|
|
3232
|
+
async getNonceForAddress(contractAddress, blockIdentifier = "pending") {
|
|
3218
3233
|
return this.fetchEndpoint("get_nonce", { contractAddress, blockIdentifier });
|
|
3219
3234
|
}
|
|
3220
3235
|
async getStorageAt(contractAddress, key, blockIdentifier = "pending") {
|
|
@@ -3344,13 +3359,13 @@ var SequencerProvider = class {
|
|
|
3344
3359
|
async getCode(contractAddress, blockIdentifier = "pending") {
|
|
3345
3360
|
return this.fetchEndpoint("get_code", { contractAddress, blockIdentifier });
|
|
3346
3361
|
}
|
|
3347
|
-
async waitForTransaction(txHash, retryInterval = 8e3) {
|
|
3362
|
+
async waitForTransaction(txHash, retryInterval = 8e3, successStates = ["ACCEPTED_ON_L1", "ACCEPTED_ON_L2", "PENDING"]) {
|
|
3363
|
+
const errorStates = ["REJECTED", "NOT_RECEIVED"];
|
|
3348
3364
|
let onchain = false;
|
|
3365
|
+
let res;
|
|
3349
3366
|
while (!onchain) {
|
|
3350
3367
|
await wait(retryInterval);
|
|
3351
|
-
|
|
3352
|
-
const successStates = ["ACCEPTED_ON_L1", "ACCEPTED_ON_L2", "PENDING"];
|
|
3353
|
-
const errorStates = ["REJECTED", "NOT_RECEIVED"];
|
|
3368
|
+
res = await this.getTransactionStatus(txHash);
|
|
3354
3369
|
if (successStates.includes(res.tx_status)) {
|
|
3355
3370
|
onchain = true;
|
|
3356
3371
|
} else if (errorStates.includes(res.tx_status)) {
|
|
@@ -3361,6 +3376,8 @@ ${res.tx_failure_reason.error_message}` : res.tx_status;
|
|
|
3361
3376
|
throw error;
|
|
3362
3377
|
}
|
|
3363
3378
|
}
|
|
3379
|
+
const txReceipt = await this.getTransactionReceipt(txHash);
|
|
3380
|
+
return txReceipt;
|
|
3364
3381
|
}
|
|
3365
3382
|
async getTransactionStatus(txHash) {
|
|
3366
3383
|
const txHashHex = toHex(toBN(txHash));
|
|
@@ -3387,11 +3404,13 @@ ${res.tx_failure_reason.error_message}` : res.tx_status;
|
|
|
3387
3404
|
// src/provider/default.ts
|
|
3388
3405
|
var Provider = class {
|
|
3389
3406
|
constructor(providerOrOptions) {
|
|
3390
|
-
if (providerOrOptions
|
|
3407
|
+
if (providerOrOptions instanceof Provider) {
|
|
3408
|
+
this.provider = providerOrOptions.provider;
|
|
3409
|
+
} else if (providerOrOptions instanceof RpcProvider || providerOrOptions instanceof SequencerProvider) {
|
|
3391
3410
|
this.provider = providerOrOptions;
|
|
3392
|
-
} else if (providerOrOptions
|
|
3411
|
+
} else if (providerOrOptions && "rpc" in providerOrOptions) {
|
|
3393
3412
|
this.provider = new RpcProvider(providerOrOptions.rpc);
|
|
3394
|
-
} else if (providerOrOptions
|
|
3413
|
+
} else if (providerOrOptions && "sequencer" in providerOrOptions) {
|
|
3395
3414
|
this.provider = new SequencerProvider(providerOrOptions.sequencer);
|
|
3396
3415
|
} else {
|
|
3397
3416
|
this.provider = new SequencerProvider();
|
|
@@ -3425,8 +3444,8 @@ var Provider = class {
|
|
|
3425
3444
|
blockIdentifier
|
|
3426
3445
|
);
|
|
3427
3446
|
}
|
|
3428
|
-
async
|
|
3429
|
-
return this.provider.
|
|
3447
|
+
async getNonceForAddress(contractAddress, blockIdentifier) {
|
|
3448
|
+
return this.provider.getNonceForAddress(contractAddress, blockIdentifier);
|
|
3430
3449
|
}
|
|
3431
3450
|
async getStorageAt(contractAddress, key, blockIdentifier = "pending") {
|
|
3432
3451
|
return this.provider.getStorageAt(contractAddress, key, blockIdentifier);
|
|
@@ -3461,8 +3480,8 @@ var Provider = class {
|
|
|
3461
3480
|
async getCode(contractAddress, blockIdentifier) {
|
|
3462
3481
|
return this.provider.getCode(contractAddress, blockIdentifier);
|
|
3463
3482
|
}
|
|
3464
|
-
async waitForTransaction(txHash, retryInterval) {
|
|
3465
|
-
return this.provider.waitForTransaction(txHash, retryInterval);
|
|
3483
|
+
async waitForTransaction(txHash, retryInterval, successStates) {
|
|
3484
|
+
return this.provider.waitForTransaction(txHash, retryInterval, successStates);
|
|
3466
3485
|
}
|
|
3467
3486
|
};
|
|
3468
3487
|
|
|
@@ -3478,12 +3497,18 @@ function parseFelt(candidate) {
|
|
|
3478
3497
|
try {
|
|
3479
3498
|
return toBN(candidate);
|
|
3480
3499
|
} catch (e) {
|
|
3481
|
-
throw Error("
|
|
3500
|
+
throw Error("Could not parse felt");
|
|
3482
3501
|
}
|
|
3483
3502
|
}
|
|
3484
3503
|
function buildCall(contract, functionAbi) {
|
|
3485
3504
|
return async function(...args) {
|
|
3486
|
-
|
|
3505
|
+
let blockIdentifier = null;
|
|
3506
|
+
args.forEach((arg) => {
|
|
3507
|
+
if (arg.blockIdentifier) {
|
|
3508
|
+
blockIdentifier = arg.blockIdentifier;
|
|
3509
|
+
}
|
|
3510
|
+
});
|
|
3511
|
+
return contract.call(functionAbi.name, args, { blockIdentifier });
|
|
3487
3512
|
};
|
|
3488
3513
|
}
|
|
3489
3514
|
function buildInvoke(contract, functionAbi) {
|
|
@@ -3708,7 +3733,7 @@ var Contract = class {
|
|
|
3708
3733
|
}
|
|
3709
3734
|
if (input.type === "felt") {
|
|
3710
3735
|
assert4(
|
|
3711
|
-
typeof args[argPosition] === "string" || typeof args[argPosition] === "number" || args[argPosition] instanceof
|
|
3736
|
+
typeof args[argPosition] === "string" || typeof args[argPosition] === "number" || args[argPosition] instanceof BN3,
|
|
3712
3737
|
`arg ${input.name} should be a felt (string, number, BigNumber)`
|
|
3713
3738
|
);
|
|
3714
3739
|
argPosition += 1;
|
|
@@ -3733,7 +3758,7 @@ var Contract = class {
|
|
|
3733
3758
|
if (input.type === "felt*") {
|
|
3734
3759
|
args[argPosition].forEach((felt) => {
|
|
3735
3760
|
assert4(
|
|
3736
|
-
typeof felt === "string" || typeof felt === "number" || felt instanceof
|
|
3761
|
+
typeof felt === "string" || typeof felt === "number" || felt instanceof BN3,
|
|
3737
3762
|
`arg ${input.name} should be an array of string, number or BigNumber`
|
|
3738
3763
|
);
|
|
3739
3764
|
});
|
|
@@ -3746,7 +3771,7 @@ var Contract = class {
|
|
|
3746
3771
|
);
|
|
3747
3772
|
args[argPosition].forEach((felt) => {
|
|
3748
3773
|
assert4(
|
|
3749
|
-
typeof felt === "string" || typeof felt === "number" || felt instanceof
|
|
3774
|
+
typeof felt === "string" || typeof felt === "number" || felt instanceof BN3,
|
|
3750
3775
|
`arg ${input.name} should be an array of string, number or BigNumber`
|
|
3751
3776
|
);
|
|
3752
3777
|
});
|
|
@@ -3903,35 +3928,42 @@ var ContractInterface = class {
|
|
|
3903
3928
|
// src/contract/contractFactory.ts
|
|
3904
3929
|
import assert5 from "minimalistic-assert";
|
|
3905
3930
|
var ContractFactory = class {
|
|
3906
|
-
constructor(compiledContract,
|
|
3931
|
+
constructor(compiledContract, classHash, account, abi = compiledContract.abi) {
|
|
3907
3932
|
this.abi = abi;
|
|
3908
3933
|
this.compiledContract = compiledContract;
|
|
3909
|
-
this.
|
|
3934
|
+
this.account = account;
|
|
3935
|
+
this.classHash = classHash;
|
|
3910
3936
|
}
|
|
3911
3937
|
async deploy(constructorCalldata, addressSalt) {
|
|
3912
|
-
const {
|
|
3938
|
+
const {
|
|
3939
|
+
deploy: { contract_address, transaction_hash }
|
|
3940
|
+
} = await this.account.declareDeploy({
|
|
3913
3941
|
contract: this.compiledContract,
|
|
3942
|
+
classHash: this.classHash,
|
|
3914
3943
|
constructorCalldata,
|
|
3915
|
-
addressSalt
|
|
3944
|
+
salt: addressSalt
|
|
3916
3945
|
});
|
|
3917
3946
|
assert5(Boolean(contract_address), "Deployment of the contract failed");
|
|
3918
3947
|
const contractInstance = new Contract(
|
|
3919
3948
|
this.compiledContract.abi,
|
|
3920
3949
|
contract_address,
|
|
3921
|
-
this.
|
|
3950
|
+
this.account
|
|
3922
3951
|
);
|
|
3923
3952
|
contractInstance.deployTransactionHash = transaction_hash;
|
|
3924
3953
|
return contractInstance;
|
|
3925
3954
|
}
|
|
3926
|
-
connect(
|
|
3927
|
-
this.
|
|
3955
|
+
connect(account) {
|
|
3956
|
+
this.account = account;
|
|
3928
3957
|
return this;
|
|
3929
3958
|
}
|
|
3930
3959
|
attach(address) {
|
|
3931
|
-
return new Contract(this.abi, address, this.
|
|
3960
|
+
return new Contract(this.abi, address, this.account);
|
|
3932
3961
|
}
|
|
3933
3962
|
};
|
|
3934
3963
|
|
|
3964
|
+
// src/account/default.ts
|
|
3965
|
+
import { BN as BN5 } from "bn.js";
|
|
3966
|
+
|
|
3935
3967
|
// src/signer/interface.ts
|
|
3936
3968
|
var SignerInterface = class {
|
|
3937
3969
|
};
|
|
@@ -4272,6 +4304,122 @@ var Signer = class {
|
|
|
4272
4304
|
}
|
|
4273
4305
|
};
|
|
4274
4306
|
|
|
4307
|
+
// src/utils/events.ts
|
|
4308
|
+
function parseUDCEvent(txReceipt) {
|
|
4309
|
+
if (!txReceipt.events) {
|
|
4310
|
+
throw new Error("UDC emited event is empty");
|
|
4311
|
+
}
|
|
4312
|
+
const event = txReceipt.events.find(
|
|
4313
|
+
(it) => cleanHex(it.from_address) === cleanHex(UDC.ADDRESS)
|
|
4314
|
+
) || {
|
|
4315
|
+
data: []
|
|
4316
|
+
};
|
|
4317
|
+
return {
|
|
4318
|
+
transaction_hash: txReceipt.transaction_hash,
|
|
4319
|
+
contract_address: event.data[0],
|
|
4320
|
+
address: event.data[0],
|
|
4321
|
+
deployer: event.data[1],
|
|
4322
|
+
unique: event.data[2],
|
|
4323
|
+
classHash: event.data[3],
|
|
4324
|
+
calldata_len: event.data[4],
|
|
4325
|
+
calldata: event.data.slice(5, 5 + parseInt(event.data[4], 16)),
|
|
4326
|
+
salt: event.data[event.data.length - 1]
|
|
4327
|
+
};
|
|
4328
|
+
}
|
|
4329
|
+
|
|
4330
|
+
// src/utils/starknetId.ts
|
|
4331
|
+
import BN4 from "bn.js";
|
|
4332
|
+
var basicAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789-";
|
|
4333
|
+
var basicSizePlusOne = new BN4(basicAlphabet.length + 1);
|
|
4334
|
+
var bigAlphabet = "\u8FD9\u6765";
|
|
4335
|
+
var basicAlphabetSize = new BN4(basicAlphabet.length);
|
|
4336
|
+
var bigAlphabetSize = new BN4(bigAlphabet.length);
|
|
4337
|
+
var bigAlphabetSizePlusOne = new BN4(bigAlphabet.length + 1);
|
|
4338
|
+
function extractStars(str) {
|
|
4339
|
+
let k = 0;
|
|
4340
|
+
while (str.endsWith(bigAlphabet[bigAlphabet.length - 1])) {
|
|
4341
|
+
str = str.substring(0, str.length - 1);
|
|
4342
|
+
k += 1;
|
|
4343
|
+
}
|
|
4344
|
+
return [str, k];
|
|
4345
|
+
}
|
|
4346
|
+
function useDecoded(encoded) {
|
|
4347
|
+
let decoded = "";
|
|
4348
|
+
encoded.forEach((subdomain) => {
|
|
4349
|
+
while (!subdomain.isZero()) {
|
|
4350
|
+
const code = subdomain.mod(basicSizePlusOne).toNumber();
|
|
4351
|
+
subdomain = subdomain.div(basicSizePlusOne);
|
|
4352
|
+
if (code === basicAlphabet.length) {
|
|
4353
|
+
const nextSubdomain = subdomain.div(bigAlphabetSizePlusOne);
|
|
4354
|
+
if (nextSubdomain.isZero()) {
|
|
4355
|
+
const code2 = subdomain.mod(bigAlphabetSizePlusOne).toNumber();
|
|
4356
|
+
subdomain = nextSubdomain;
|
|
4357
|
+
if (code2 === 0)
|
|
4358
|
+
decoded += basicAlphabet[0];
|
|
4359
|
+
else
|
|
4360
|
+
decoded += bigAlphabet[code2 - 1];
|
|
4361
|
+
} else {
|
|
4362
|
+
const code2 = subdomain.mod(bigAlphabetSize).toNumber();
|
|
4363
|
+
decoded += bigAlphabet[code2];
|
|
4364
|
+
subdomain = subdomain.div(bigAlphabetSize);
|
|
4365
|
+
}
|
|
4366
|
+
} else
|
|
4367
|
+
decoded += basicAlphabet[code];
|
|
4368
|
+
}
|
|
4369
|
+
const [str, k] = extractStars(decoded);
|
|
4370
|
+
if (k)
|
|
4371
|
+
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));
|
|
4372
|
+
decoded += ".";
|
|
4373
|
+
});
|
|
4374
|
+
return decoded.concat("stark");
|
|
4375
|
+
}
|
|
4376
|
+
function useEncoded(decoded) {
|
|
4377
|
+
let encoded = new BN4(0);
|
|
4378
|
+
let multiplier = new BN4(1);
|
|
4379
|
+
if (decoded.endsWith(bigAlphabet[0] + basicAlphabet[1])) {
|
|
4380
|
+
const [str, k] = extractStars(decoded.substring(0, decoded.length - 2));
|
|
4381
|
+
decoded = str + bigAlphabet[bigAlphabet.length - 1].repeat(2 * (k + 1));
|
|
4382
|
+
} else {
|
|
4383
|
+
const [str, k] = extractStars(decoded);
|
|
4384
|
+
if (k)
|
|
4385
|
+
decoded = str + bigAlphabet[bigAlphabet.length - 1].repeat(1 + 2 * (k - 1));
|
|
4386
|
+
}
|
|
4387
|
+
for (let i = 0; i < decoded.length; i += 1) {
|
|
4388
|
+
const char = decoded[i];
|
|
4389
|
+
const index = basicAlphabet.indexOf(char);
|
|
4390
|
+
const bnIndex = new BN4(basicAlphabet.indexOf(char));
|
|
4391
|
+
if (index !== -1) {
|
|
4392
|
+
if (i === decoded.length - 1 && decoded[i] === basicAlphabet[0]) {
|
|
4393
|
+
encoded = encoded.add(multiplier.mul(basicAlphabetSize));
|
|
4394
|
+
multiplier = multiplier.mul(basicSizePlusOne);
|
|
4395
|
+
multiplier = multiplier.mul(basicSizePlusOne);
|
|
4396
|
+
} else {
|
|
4397
|
+
encoded = encoded.add(multiplier.mul(bnIndex));
|
|
4398
|
+
multiplier = multiplier.mul(basicSizePlusOne);
|
|
4399
|
+
}
|
|
4400
|
+
} else if (bigAlphabet.indexOf(char) !== -1) {
|
|
4401
|
+
encoded = encoded.add(multiplier.mul(basicAlphabetSize));
|
|
4402
|
+
multiplier = multiplier.mul(basicSizePlusOne);
|
|
4403
|
+
const newid = (i === decoded.length - 1 ? 1 : 0) + bigAlphabet.indexOf(char);
|
|
4404
|
+
encoded = encoded.add(multiplier.mul(new BN4(newid)));
|
|
4405
|
+
multiplier = multiplier.mul(bigAlphabetSize);
|
|
4406
|
+
}
|
|
4407
|
+
}
|
|
4408
|
+
return encoded;
|
|
4409
|
+
}
|
|
4410
|
+
function getStarknetIdContract(chainId) {
|
|
4411
|
+
const starknetIdMainnetContract = "0x6ac597f8116f886fa1c97a23fa4e08299975ecaf6b598873ca6792b9bbfb678";
|
|
4412
|
+
const starknetIdTestnetContract = "0x05cf267a0af6101667013fc6bd3f6c11116a14cda9b8c4b1198520d59f900b17";
|
|
4413
|
+
switch (chainId) {
|
|
4414
|
+
case "0x534e5f4d41494e" /* MAINNET */:
|
|
4415
|
+
return starknetIdMainnetContract;
|
|
4416
|
+
case "0x534e5f474f45524c49" /* TESTNET */:
|
|
4417
|
+
return starknetIdTestnetContract;
|
|
4418
|
+
default:
|
|
4419
|
+
throw new Error("Starknet.id is not yet deployed on this network");
|
|
4420
|
+
}
|
|
4421
|
+
}
|
|
4422
|
+
|
|
4275
4423
|
// src/account/default.ts
|
|
4276
4424
|
var Account = class extends Provider {
|
|
4277
4425
|
constructor(providerOrOptions, address, keyPairOrSigner) {
|
|
@@ -4280,7 +4428,41 @@ var Account = class extends Provider {
|
|
|
4280
4428
|
this.signer = "getPubKey" in keyPairOrSigner ? keyPairOrSigner : new Signer(keyPairOrSigner);
|
|
4281
4429
|
}
|
|
4282
4430
|
async getNonce(blockIdentifier) {
|
|
4283
|
-
return super.
|
|
4431
|
+
return super.getNonceForAddress(this.address, blockIdentifier);
|
|
4432
|
+
}
|
|
4433
|
+
async getStarkName(StarknetIdContract) {
|
|
4434
|
+
const chainId = await this.getChainId();
|
|
4435
|
+
const contract = StarknetIdContract ?? getStarknetIdContract(chainId);
|
|
4436
|
+
try {
|
|
4437
|
+
const hexDomain = await this.callContract({
|
|
4438
|
+
contractAddress: contract,
|
|
4439
|
+
entrypoint: "address_to_domain",
|
|
4440
|
+
calldata: compileCalldata({
|
|
4441
|
+
address: this.address
|
|
4442
|
+
})
|
|
4443
|
+
});
|
|
4444
|
+
const decimalDomain = hexDomain.result.map((element) => new BN5(hexToDecimalString(element))).slice(1);
|
|
4445
|
+
const stringDomain = useDecoded(decimalDomain);
|
|
4446
|
+
return stringDomain;
|
|
4447
|
+
} catch {
|
|
4448
|
+
return Error("Could not get stark name");
|
|
4449
|
+
}
|
|
4450
|
+
}
|
|
4451
|
+
async getAddressFromStarkName(name, StarknetIdContract) {
|
|
4452
|
+
const chainId = await this.getChainId();
|
|
4453
|
+
const contract = StarknetIdContract ?? getStarknetIdContract(chainId);
|
|
4454
|
+
try {
|
|
4455
|
+
const addressData = await this.callContract({
|
|
4456
|
+
contractAddress: contract,
|
|
4457
|
+
entrypoint: "domain_to_address",
|
|
4458
|
+
calldata: compileCalldata({
|
|
4459
|
+
domain: [useEncoded(name.replace(".stark", "")).toString(10)]
|
|
4460
|
+
})
|
|
4461
|
+
});
|
|
4462
|
+
return addressData.result[0];
|
|
4463
|
+
} catch {
|
|
4464
|
+
return Error("Could not get address from stark name");
|
|
4465
|
+
}
|
|
4284
4466
|
}
|
|
4285
4467
|
async estimateFee(calls, estimateFeeDetails) {
|
|
4286
4468
|
return this.estimateInvokeFee(calls, estimateFeeDetails);
|
|
@@ -4367,7 +4549,7 @@ var Account = class extends Provider {
|
|
|
4367
4549
|
}
|
|
4368
4550
|
async estimateDeployFee({
|
|
4369
4551
|
classHash,
|
|
4370
|
-
salt,
|
|
4552
|
+
salt = "0",
|
|
4371
4553
|
unique = true,
|
|
4372
4554
|
constructorCalldata = [],
|
|
4373
4555
|
additionalCalls = []
|
|
@@ -4448,9 +4630,10 @@ var Account = class extends Provider {
|
|
|
4448
4630
|
unique = true,
|
|
4449
4631
|
constructorCalldata = [],
|
|
4450
4632
|
additionalCalls = []
|
|
4451
|
-
},
|
|
4633
|
+
}, invocationsDetails = {}) {
|
|
4452
4634
|
const compiledConstructorCallData = compileCalldata(constructorCalldata);
|
|
4453
4635
|
const callsArray = Array.isArray(additionalCalls) ? additionalCalls : [additionalCalls];
|
|
4636
|
+
const deploySalt = salt ?? randomAddress();
|
|
4454
4637
|
return this.execute(
|
|
4455
4638
|
[
|
|
4456
4639
|
{
|
|
@@ -4458,7 +4641,7 @@ var Account = class extends Provider {
|
|
|
4458
4641
|
entrypoint: UDC.ENTRYPOINT,
|
|
4459
4642
|
calldata: [
|
|
4460
4643
|
classHash,
|
|
4461
|
-
|
|
4644
|
+
deploySalt,
|
|
4462
4645
|
toCairoBool(unique),
|
|
4463
4646
|
compiledConstructorCallData.length,
|
|
4464
4647
|
...compiledConstructorCallData
|
|
@@ -4467,9 +4650,22 @@ var Account = class extends Provider {
|
|
|
4467
4650
|
...callsArray
|
|
4468
4651
|
],
|
|
4469
4652
|
void 0,
|
|
4470
|
-
|
|
4653
|
+
invocationsDetails
|
|
4471
4654
|
);
|
|
4472
4655
|
}
|
|
4656
|
+
async deployContract(payload, details = {}) {
|
|
4657
|
+
const deployTx = await this.deploy(payload, details);
|
|
4658
|
+
const txReceipt = await this.waitForTransaction(deployTx.transaction_hash, void 0, [
|
|
4659
|
+
"ACCEPTED_ON_L2"
|
|
4660
|
+
]);
|
|
4661
|
+
return parseUDCEvent(txReceipt);
|
|
4662
|
+
}
|
|
4663
|
+
async declareDeploy({ classHash, contract, constructorCalldata }, details) {
|
|
4664
|
+
const { transaction_hash } = await this.declare({ contract, classHash }, details);
|
|
4665
|
+
const declare = await this.waitForTransaction(transaction_hash, void 0, ["ACCEPTED_ON_L2"]);
|
|
4666
|
+
const deploy = await this.deployContract({ classHash, constructorCalldata }, details);
|
|
4667
|
+
return { declare: { ...declare, class_hash: classHash }, deploy };
|
|
4668
|
+
}
|
|
4473
4669
|
async deployAccount({
|
|
4474
4670
|
classHash,
|
|
4475
4671
|
constructorCalldata = [],
|