starknet 3.14.1 → 3.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 +10 -0
- package/__tests__/utils/utils.test.ts +27 -0
- package/dist/utils/hash.d.ts +2 -0
- package/dist/utils/hash.js +14 -1
- package/package.json +1 -1
- package/src/utils/hash.ts +23 -1
- package/utils/hash.d.ts +7 -0
- package/utils/hash.js +17 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
# [3.15.0](https://github.com/0xs34n/starknet.js/compare/v3.14.1...v3.15.0) (2022-06-16)
|
|
2
|
+
|
|
3
|
+
### Bug Fixes
|
|
4
|
+
|
|
5
|
+
- **tests:** update test ([c71f482](https://github.com/0xs34n/starknet.js/commit/c71f482facdaac914c45f4bf91f48e05a930abff))
|
|
6
|
+
|
|
7
|
+
### Features
|
|
8
|
+
|
|
9
|
+
- add calculateContractAddressFromHash ([e22c346](https://github.com/0xs34n/starknet.js/commit/e22c3464036f97eee3c617d3790aac35b3d95379))
|
|
10
|
+
|
|
1
11
|
## [3.14.1](https://github.com/0xs34n/starknet.js/compare/v3.14.0...v3.14.1) (2022-06-15)
|
|
2
12
|
|
|
3
13
|
### Bug Fixes
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
|
|
3
3
|
import { constants, hash, json, number, stark } from '../../src';
|
|
4
|
+
import { pedersen } from '../../src/utils/hash';
|
|
4
5
|
|
|
5
6
|
const { IS_BROWSER } = constants;
|
|
6
7
|
|
|
@@ -87,3 +88,29 @@ describe('estimatedFeeToMaxFee()', () => {
|
|
|
87
88
|
expect(res).toBe(11_500);
|
|
88
89
|
});
|
|
89
90
|
});
|
|
91
|
+
|
|
92
|
+
describe('calculateContractAddressFromHash()', () => {
|
|
93
|
+
// This test just show how to use calculateContractAddressFromHash for new devs
|
|
94
|
+
|
|
95
|
+
test('calculated contract address should match the snapshot', () => {
|
|
96
|
+
const ethAddress = '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7';
|
|
97
|
+
|
|
98
|
+
const daiAddress = '0x03e85bfbb8e2a42b7bead9e88e9a1b19dbccf661471061807292120462396ec9';
|
|
99
|
+
const factoryAddress = '0x249827618A01858A72B7D04339C47195A324D20D6037033DFE2829F98AFF4FC';
|
|
100
|
+
const classHash = '0x55187E68C60664A947048E0C9E5322F9BF55F7D435ECDCF17ED75724E77368F';
|
|
101
|
+
|
|
102
|
+
// Any type of salt can be used. It depends on the dApp what kind of salt it wants to use.
|
|
103
|
+
const salt = pedersen([ethAddress, daiAddress]);
|
|
104
|
+
|
|
105
|
+
const res = hash.calculateContractAddressFromHash(
|
|
106
|
+
salt,
|
|
107
|
+
classHash,
|
|
108
|
+
[ethAddress, daiAddress, factoryAddress],
|
|
109
|
+
factoryAddress
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
expect(res).toMatchInlineSnapshot(
|
|
113
|
+
`"0x36dc8dcb3440596472ddde11facacc45d0cd250df764ae7c3d1a360c853c324"`
|
|
114
|
+
);
|
|
115
|
+
});
|
|
116
|
+
});
|
package/dist/utils/hash.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import BN from 'bn.js';
|
|
2
2
|
import { StarknetChainId, TransactionHashPrefix } from '../constants';
|
|
3
|
+
import { RawCalldata } from '../types/lib';
|
|
3
4
|
import { BigNumberish } from './number';
|
|
4
5
|
export declare const transactionVersion = 0;
|
|
5
6
|
export declare const feeTransactionVersion: BN;
|
|
@@ -24,3 +25,4 @@ export declare function computeHashOnElements(data: BigNumberish[]): string;
|
|
|
24
25
|
export declare function calculateTransactionHashCommon(txHashPrefix: TransactionHashPrefix, version: BigNumberish, contractAddress: BigNumberish, entryPointSelector: BigNumberish, calldata: BigNumberish[], maxFee: BigNumberish, chainId: StarknetChainId, additionalData?: BigNumberish[]): string;
|
|
25
26
|
export declare function calculateDeployTransactionHash(contractAddress: BigNumberish, constructorCalldata: BigNumberish[], version: BigNumberish, chainId: StarknetChainId): string;
|
|
26
27
|
export declare function calculcateTransactionHash(contractAddress: BigNumberish, version: BigNumberish, entryPointSelector: BigNumberish, calldata: BigNumberish[], maxFee: BigNumberish, chainId: StarknetChainId): string;
|
|
28
|
+
export declare function calculateContractAddressFromHash(salt: BigNumberish, classHash: BigNumberish, constructorCalldata: RawCalldata, deployerAddress: BigNumberish): string;
|
package/dist/utils/hash.js
CHANGED
|
@@ -28,7 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
28
28
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
29
29
|
};
|
|
30
30
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
-
exports.calculcateTransactionHash = exports.calculateDeployTransactionHash = exports.calculateTransactionHashCommon = exports.computeHashOnElements = exports.pedersen = exports.getSelectorFromName = exports.starknetKeccak = exports.feeTransactionVersion = exports.transactionVersion = void 0;
|
|
31
|
+
exports.calculateContractAddressFromHash = exports.calculcateTransactionHash = exports.calculateDeployTransactionHash = exports.calculateTransactionHashCommon = exports.computeHashOnElements = exports.pedersen = exports.getSelectorFromName = exports.starknetKeccak = exports.feeTransactionVersion = exports.transactionVersion = void 0;
|
|
32
32
|
var keccak_1 = require("ethereum-cryptography/keccak");
|
|
33
33
|
var minimalistic_assert_1 = __importDefault(require("minimalistic-assert"));
|
|
34
34
|
var constants_1 = require("../constants");
|
|
@@ -112,3 +112,16 @@ function calculcateTransactionHash(contractAddress, version, entryPointSelector,
|
|
|
112
112
|
return calculateTransactionHashCommon(constants_1.TransactionHashPrefix.INVOKE, version, contractAddress, entryPointSelector, calldata, maxFee, chainId);
|
|
113
113
|
}
|
|
114
114
|
exports.calculcateTransactionHash = calculcateTransactionHash;
|
|
115
|
+
function calculateContractAddressFromHash(salt, classHash, constructorCalldata, deployerAddress) {
|
|
116
|
+
var constructorCalldataHash = computeHashOnElements(constructorCalldata);
|
|
117
|
+
var CONTRACT_ADDRESS_PREFIX = (0, number_1.toFelt)('0x535441524b4e45545f434f4e54524143545f41444452455353'); // Equivalent to 'STARKNET_CONTRACT_ADDRESS'
|
|
118
|
+
var dataToHash = [
|
|
119
|
+
CONTRACT_ADDRESS_PREFIX,
|
|
120
|
+
deployerAddress,
|
|
121
|
+
salt,
|
|
122
|
+
classHash,
|
|
123
|
+
constructorCalldataHash,
|
|
124
|
+
];
|
|
125
|
+
return computeHashOnElements(dataToHash);
|
|
126
|
+
}
|
|
127
|
+
exports.calculateContractAddressFromHash = calculateContractAddressFromHash;
|
package/package.json
CHANGED
package/src/utils/hash.ts
CHANGED
|
@@ -11,9 +11,10 @@ import {
|
|
|
11
11
|
TransactionHashPrefix,
|
|
12
12
|
ZERO,
|
|
13
13
|
} from '../constants';
|
|
14
|
+
import { RawCalldata } from '../types/lib';
|
|
14
15
|
import { ec } from './ellipticCurve';
|
|
15
16
|
import { addHexPrefix, buf2hex, utf8ToArray } from './encode';
|
|
16
|
-
import { BigNumberish, toBN, toHex } from './number';
|
|
17
|
+
import { BigNumberish, toBN, toFelt, toHex } from './number';
|
|
17
18
|
|
|
18
19
|
export const transactionVersion = 0;
|
|
19
20
|
export const feeTransactionVersion = toBN(2).pow(toBN(128)).add(toBN(transactionVersion));
|
|
@@ -132,3 +133,24 @@ export function calculcateTransactionHash(
|
|
|
132
133
|
chainId
|
|
133
134
|
);
|
|
134
135
|
}
|
|
136
|
+
|
|
137
|
+
export function calculateContractAddressFromHash(
|
|
138
|
+
salt: BigNumberish,
|
|
139
|
+
classHash: BigNumberish,
|
|
140
|
+
constructorCalldata: RawCalldata,
|
|
141
|
+
deployerAddress: BigNumberish
|
|
142
|
+
) {
|
|
143
|
+
const constructorCalldataHash = computeHashOnElements(constructorCalldata);
|
|
144
|
+
|
|
145
|
+
const CONTRACT_ADDRESS_PREFIX = toFelt('0x535441524b4e45545f434f4e54524143545f41444452455353'); // Equivalent to 'STARKNET_CONTRACT_ADDRESS'
|
|
146
|
+
|
|
147
|
+
const dataToHash = [
|
|
148
|
+
CONTRACT_ADDRESS_PREFIX,
|
|
149
|
+
deployerAddress,
|
|
150
|
+
salt,
|
|
151
|
+
classHash,
|
|
152
|
+
constructorCalldataHash,
|
|
153
|
+
];
|
|
154
|
+
|
|
155
|
+
return computeHashOnElements(dataToHash);
|
|
156
|
+
}
|
package/utils/hash.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import BN from 'bn.js';
|
|
2
2
|
|
|
3
3
|
import { StarknetChainId, TransactionHashPrefix } from '../constants';
|
|
4
|
+
import { RawCalldata } from '../types/lib';
|
|
4
5
|
import { BigNumberish } from './number';
|
|
5
6
|
export declare const transactionVersion = 0;
|
|
6
7
|
export declare const feeTransactionVersion: BN;
|
|
@@ -46,3 +47,9 @@ export declare function calculcateTransactionHash(
|
|
|
46
47
|
maxFee: BigNumberish,
|
|
47
48
|
chainId: StarknetChainId
|
|
48
49
|
): string;
|
|
50
|
+
export declare function calculateContractAddressFromHash(
|
|
51
|
+
salt: BigNumberish,
|
|
52
|
+
classHash: BigNumberish,
|
|
53
|
+
constructorCalldata: RawCalldata,
|
|
54
|
+
deployerAddress: BigNumberish
|
|
55
|
+
): string;
|
package/utils/hash.js
CHANGED
|
@@ -39,7 +39,8 @@ var __importDefault =
|
|
|
39
39
|
return mod && mod.__esModule ? mod : { default: mod };
|
|
40
40
|
};
|
|
41
41
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
42
|
-
exports.
|
|
42
|
+
exports.calculateContractAddressFromHash =
|
|
43
|
+
exports.calculcateTransactionHash =
|
|
43
44
|
exports.calculateDeployTransactionHash =
|
|
44
45
|
exports.calculateTransactionHashCommon =
|
|
45
46
|
exports.computeHashOnElements =
|
|
@@ -174,3 +175,18 @@ function calculcateTransactionHash(
|
|
|
174
175
|
);
|
|
175
176
|
}
|
|
176
177
|
exports.calculcateTransactionHash = calculcateTransactionHash;
|
|
178
|
+
function calculateContractAddressFromHash(salt, classHash, constructorCalldata, deployerAddress) {
|
|
179
|
+
var constructorCalldataHash = computeHashOnElements(constructorCalldata);
|
|
180
|
+
var CONTRACT_ADDRESS_PREFIX = (0, number_1.toFelt)(
|
|
181
|
+
'0x535441524b4e45545f434f4e54524143545f41444452455353'
|
|
182
|
+
); // Equivalent to 'STARKNET_CONTRACT_ADDRESS'
|
|
183
|
+
var dataToHash = [
|
|
184
|
+
CONTRACT_ADDRESS_PREFIX,
|
|
185
|
+
deployerAddress,
|
|
186
|
+
salt,
|
|
187
|
+
classHash,
|
|
188
|
+
constructorCalldataHash,
|
|
189
|
+
];
|
|
190
|
+
return computeHashOnElements(dataToHash);
|
|
191
|
+
}
|
|
192
|
+
exports.calculateContractAddressFromHash = calculateContractAddressFromHash;
|