starknet 2.2.0 → 2.3.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 CHANGED
@@ -1,3 +1,10 @@
1
+ # [2.3.0](https://github.com/seanjameshan/starknet.js/compare/v2.2.0...v2.3.0) (2021-12-01)
2
+
3
+ ### Features
4
+
5
+ - add compileStructToCalldata ([e5bdb18](https://github.com/seanjameshan/starknet.js/commit/e5bdb1890e0bc53b03d4b145069cf7fbc639e830))
6
+ - **utils:** support shortstring and uint256 ([f7ff057](https://github.com/seanjameshan/starknet.js/commit/f7ff05753d9bc39b31bdd4e7f893ee04cab77823))
7
+
1
8
  # [2.2.0](https://github.com/seanjameshan/starknet.js/compare/v2.1.0...v2.2.0) (2021-11-30)
2
9
 
3
10
  ### Bug Fixes
@@ -0,0 +1,22 @@
1
+ import { decodeShortString, encodeShortString } from '../../src/utils/shortString';
2
+
3
+ describe('shortString', () => {
4
+ test('should convert string to number', () => {
5
+ expect(encodeShortString('hello')).toMatchInlineSnapshot(`"0x68656c6c6f"`);
6
+ });
7
+ test('should convert number to string', () => {
8
+ expect(decodeShortString('0x68656c6c6f')).toMatchInlineSnapshot(`"hello"`);
9
+ });
10
+ test('should throw if string is too long', () => {
11
+ expect(() =>
12
+ encodeShortString('hello world hello world hello world hello world hello world hello world')
13
+ ).toThrowErrorMatchingInlineSnapshot(
14
+ `"hello world hello world hello world hello world hello world hello world is too long"`
15
+ );
16
+ });
17
+ test('should throw if string contains non ascii chars', () => {
18
+ expect(() => encodeShortString('hello\uD83D\uDE00')).toThrowErrorMatchingInlineSnapshot(
19
+ `"hello😀 is not an ASCII string"`
20
+ );
21
+ });
22
+ });
@@ -0,0 +1,32 @@
1
+ import { ONE } from '../../src/constants';
2
+ import { toBN } from '../../src/utils/number';
3
+ import { UINT_128_MAX, UINT_256_MAX, bnToUint256, uint256ToBN } from '../../src/utils/uint256';
4
+
5
+ describe('cairo uint256', () => {
6
+ test('should convert 0 from BN to uint256 struct', () => {
7
+ const uint256 = bnToUint256('0');
8
+ expect(uint256).toMatchInlineSnapshot(`
9
+ Object {
10
+ "high": "0x0",
11
+ "low": "0x0",
12
+ }
13
+ `);
14
+ });
15
+ test('should convert 0 from uint256 to BN', () => {
16
+ expect(uint256ToBN({ low: '0x0', high: '0x0' }).toString()).toMatchInlineSnapshot(`"0"`);
17
+ });
18
+ test('should convert BN over 2^128 to uint256 struct', () => {
19
+ const uint256 = bnToUint256(UINT_128_MAX.add(ONE));
20
+ expect(uint256).toMatchInlineSnapshot(`
21
+ Object {
22
+ "high": "0x1",
23
+ "low": "0x0",
24
+ }
25
+ `);
26
+ });
27
+ test('should throw if BN over uint256 range', () => {
28
+ expect(() => bnToUint256(UINT_256_MAX.add(toBN(1)))).toThrowErrorMatchingInlineSnapshot(
29
+ `"Number is too large"`
30
+ );
31
+ });
32
+ });
package/constants.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import BN from 'bn.js';
1
+ /// <reference types="bn.js" />
2
2
  export { IS_BROWSER } from './utils/encode';
3
- export declare const ZERO: BN;
4
- export declare const ONE: BN;
5
- export declare const TWO: BN;
6
- export declare const MASK_250: BN;
3
+ export declare const ZERO: import('bn.js');
4
+ export declare const ONE: import('bn.js');
5
+ export declare const TWO: import('bn.js');
6
+ export declare const MASK_250: import('bn.js');
7
7
  /**
8
8
  * The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
9
9
  * Please do not edit until the JSON changes.
@@ -1,9 +1,9 @@
1
- import BN from 'bn.js';
1
+ /// <reference types="bn.js" />
2
2
  export { IS_BROWSER } from './utils/encode';
3
- export declare const ZERO: BN;
4
- export declare const ONE: BN;
5
- export declare const TWO: BN;
6
- export declare const MASK_250: BN;
3
+ export declare const ZERO: import("bn.js");
4
+ export declare const ONE: import("bn.js");
5
+ export declare const TWO: import("bn.js");
6
+ export declare const MASK_250: import("bn.js");
7
7
  /**
8
8
  * The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
9
9
  * Please do not edit until the JSON changes.
package/dist/index.d.ts CHANGED
@@ -15,3 +15,5 @@ export * as json from './utils/json';
15
15
  export * as number from './utils/number';
16
16
  export * as stark from './utils/stark';
17
17
  export * as ec from './utils/ellipticCurve';
18
+ export * as uint256 from './utils/uint256';
19
+ export * as shortString from './utils/shortString';
package/dist/index.js CHANGED
@@ -22,7 +22,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  return result;
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.ec = exports.stark = exports.number = exports.json = exports.hash = exports.encode = exports.constants = void 0;
25
+ exports.shortString = exports.uint256 = exports.ec = exports.stark = exports.number = exports.json = exports.hash = exports.encode = exports.constants = void 0;
26
26
  /**
27
27
  * Main
28
28
  */
@@ -40,3 +40,5 @@ exports.json = __importStar(require("./utils/json"));
40
40
  exports.number = __importStar(require("./utils/number"));
41
41
  exports.stark = __importStar(require("./utils/stark"));
42
42
  exports.ec = __importStar(require("./utils/ellipticCurve"));
43
+ exports.uint256 = __importStar(require("./utils/uint256"));
44
+ exports.shortString = __importStar(require("./utils/shortString"));
@@ -0,0 +1,4 @@
1
+ export declare function isASCII(str: string): boolean;
2
+ export declare function isShortString(str: string): boolean;
3
+ export declare function encodeShortString(str: string): string;
4
+ export declare function decodeShortString(str: string): string;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decodeShortString = exports.encodeShortString = exports.isShortString = exports.isASCII = void 0;
4
+ var encode_1 = require("./encode");
5
+ function isASCII(str) {
6
+ // eslint-disable-next-line no-control-regex
7
+ return /^[\x00-\x7F]*$/.test(str);
8
+ }
9
+ exports.isASCII = isASCII;
10
+ // function to check if string has less or equal 31 characters
11
+ function isShortString(str) {
12
+ return str.length <= 31;
13
+ }
14
+ exports.isShortString = isShortString;
15
+ function encodeShortString(str) {
16
+ if (!isASCII(str))
17
+ throw new Error(str + " is not an ASCII string");
18
+ if (!isShortString(str))
19
+ throw new Error(str + " is too long");
20
+ return (0, encode_1.addHexPrefix)(str.replace(/./g, function (char) { return char.charCodeAt(0).toString(16); }));
21
+ }
22
+ exports.encodeShortString = encodeShortString;
23
+ function decodeShortString(str) {
24
+ return (0, encode_1.removeHexPrefix)(str).replace(/.{2}/g, function (hex) { return String.fromCharCode(parseInt(hex, 16)); });
25
+ }
26
+ exports.decodeShortString = decodeShortString;
@@ -19,3 +19,6 @@ export declare function getSelectorFromName(funcName: string): string;
19
19
  export declare function randomAddress(): string;
20
20
  export declare function makeAddress(input: string): string;
21
21
  export declare function formatSignature(sig?: [BigNumberish, BigNumberish]): [string, string] | [];
22
+ export declare function compileStructToCalldata<S extends {
23
+ [k: string]: string;
24
+ }>(struct: S): string[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatSignature = exports.makeAddress = exports.randomAddress = exports.getSelectorFromName = exports.compressProgram = void 0;
3
+ exports.compileStructToCalldata = exports.formatSignature = exports.makeAddress = exports.randomAddress = exports.getSelectorFromName = exports.compressProgram = void 0;
4
4
  var pako_1 = require("pako");
5
5
  var ellipticCurve_1 = require("./ellipticCurve");
6
6
  var encode_1 = require("./encode");
@@ -52,3 +52,7 @@ function formatSignature(sig) {
52
52
  }
53
53
  }
54
54
  exports.formatSignature = formatSignature;
55
+ function compileStructToCalldata(struct) {
56
+ return Object.values(struct);
57
+ }
58
+ exports.compileStructToCalldata = compileStructToCalldata;
@@ -0,0 +1,11 @@
1
+ /// <reference types="bn.js" />
2
+ import { BigNumberish } from './number';
3
+ export interface Uint256 {
4
+ low: BigNumberish;
5
+ high: BigNumberish;
6
+ }
7
+ export declare function uint256ToBN(uint256: Uint256): import("bn.js");
8
+ export declare const UINT_128_MAX: import("bn.js");
9
+ export declare const UINT_256_MAX: import("bn.js");
10
+ export declare function isUint256(bn: BigNumberish): boolean;
11
+ export declare function bnToUint256(bignumber: BigNumberish): Uint256;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bnToUint256 = exports.isUint256 = exports.UINT_256_MAX = exports.UINT_128_MAX = exports.uint256ToBN = void 0;
4
+ var encode_1 = require("./encode");
5
+ var number_1 = require("./number");
6
+ // function to convert Uint256 to BN
7
+ function uint256ToBN(uint256) {
8
+ return (0, number_1.toBN)(uint256.high).shln(128).add((0, number_1.toBN)(uint256.low));
9
+ }
10
+ exports.uint256ToBN = uint256ToBN;
11
+ exports.UINT_128_MAX = (0, number_1.toBN)(1).shln(128).sub((0, number_1.toBN)(1));
12
+ exports.UINT_256_MAX = (0, number_1.toBN)(1).shln(256).sub((0, number_1.toBN)(1));
13
+ // function to check if BN is smaller or equal 2**256-1
14
+ function isUint256(bn) {
15
+ return (0, number_1.toBN)(bn).lte(exports.UINT_256_MAX);
16
+ }
17
+ exports.isUint256 = isUint256;
18
+ // function to convert BN to Uint256
19
+ function bnToUint256(bignumber) {
20
+ var bn = (0, number_1.toBN)(bignumber);
21
+ if (!isUint256(bn))
22
+ throw new Error('Number is too large');
23
+ return {
24
+ low: (0, encode_1.addHexPrefix)(bn.maskn(128).toString(16)),
25
+ high: (0, encode_1.addHexPrefix)(bn.shrn(128).toString(16)),
26
+ };
27
+ }
28
+ exports.bnToUint256 = bnToUint256;
package/index.d.ts CHANGED
@@ -15,3 +15,5 @@ export * as json from './utils/json';
15
15
  export * as number from './utils/number';
16
16
  export * as stark from './utils/stark';
17
17
  export * as ec from './utils/ellipticCurve';
18
+ export * as uint256 from './utils/uint256';
19
+ export * as shortString from './utils/shortString';
package/index.js CHANGED
@@ -44,7 +44,9 @@ var __importStar =
44
44
  return result;
45
45
  };
46
46
  Object.defineProperty(exports, '__esModule', { value: true });
47
- exports.ec =
47
+ exports.shortString =
48
+ exports.uint256 =
49
+ exports.ec =
48
50
  exports.stark =
49
51
  exports.number =
50
52
  exports.json =
@@ -69,3 +71,5 @@ exports.json = __importStar(require('./utils/json'));
69
71
  exports.number = __importStar(require('./utils/number'));
70
72
  exports.stark = __importStar(require('./utils/stark'));
71
73
  exports.ec = __importStar(require('./utils/ellipticCurve'));
74
+ exports.uint256 = __importStar(require('./utils/uint256'));
75
+ exports.shortString = __importStar(require('./utils/shortString'));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starknet",
3
- "version": "2.2.0",
3
+ "version": "2.3.0",
4
4
  "description": "JavaScript library for StarkNet",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/constants.ts CHANGED
@@ -1,13 +1,11 @@
1
- import BN from 'bn.js';
2
-
3
1
  import { toBN } from './utils/number';
4
2
 
5
3
  export { IS_BROWSER } from './utils/encode';
6
4
 
7
- export const ZERO: BN = toBN(0);
8
- export const ONE: BN = toBN(1);
9
- export const TWO: BN = toBN(2);
10
- export const MASK_250: BN = TWO.pow(toBN(250)).sub(ONE); // 2 ** 250 - 1
5
+ export const ZERO = toBN(0);
6
+ export const ONE = toBN(1);
7
+ export const TWO = toBN(2);
8
+ export const MASK_250 = TWO.pow(toBN(250)).sub(ONE); // 2 ** 250 - 1
11
9
 
12
10
  /**
13
11
  * The following is taken from https://github.com/starkware-libs/starkex-resources/blob/master/crypto/starkware/crypto/signature/pedersen_params.json but converted to hex, because JS is very bad handling big integers by default
package/src/index.ts CHANGED
@@ -16,3 +16,5 @@ export * as json from './utils/json';
16
16
  export * as number from './utils/number';
17
17
  export * as stark from './utils/stark';
18
18
  export * as ec from './utils/ellipticCurve';
19
+ export * as uint256 from './utils/uint256';
20
+ export * as shortString from './utils/shortString';
@@ -0,0 +1,21 @@
1
+ import { addHexPrefix, removeHexPrefix } from './encode';
2
+
3
+ export function isASCII(str: string) {
4
+ // eslint-disable-next-line no-control-regex
5
+ return /^[\x00-\x7F]*$/.test(str);
6
+ }
7
+
8
+ // function to check if string has less or equal 31 characters
9
+ export function isShortString(str: string) {
10
+ return str.length <= 31;
11
+ }
12
+
13
+ export function encodeShortString(str: string) {
14
+ if (!isASCII(str)) throw new Error(`${str} is not an ASCII string`);
15
+ if (!isShortString(str)) throw new Error(`${str} is too long`);
16
+ return addHexPrefix(str.replace(/./g, (char) => char.charCodeAt(0).toString(16)));
17
+ }
18
+
19
+ export function decodeShortString(str: string) {
20
+ return removeHexPrefix(str).replace(/.{2}/g, (hex) => String.fromCharCode(parseInt(hex, 16)));
21
+ }
@@ -49,3 +49,7 @@ export function formatSignature(sig?: [BigNumberish, BigNumberish]): [string, st
49
49
  return [];
50
50
  }
51
51
  }
52
+
53
+ export function compileStructToCalldata<S extends { [k: string]: string }>(struct: S): string[] {
54
+ return Object.values(struct);
55
+ }
@@ -0,0 +1,32 @@
1
+ import { addHexPrefix } from './encode';
2
+ import { BigNumberish, toBN } from './number';
3
+
4
+ // Represents an integer in the range [0, 2^256).
5
+ export interface Uint256 {
6
+ // The low 128 bits of the value.
7
+ low: BigNumberish;
8
+ // The high 128 bits of the value.
9
+ high: BigNumberish;
10
+ }
11
+
12
+ // function to convert Uint256 to BN
13
+ export function uint256ToBN(uint256: Uint256) {
14
+ return toBN(uint256.high).shln(128).add(toBN(uint256.low));
15
+ }
16
+
17
+ export const UINT_128_MAX = toBN(1).shln(128).sub(toBN(1));
18
+ export const UINT_256_MAX = toBN(1).shln(256).sub(toBN(1));
19
+ // function to check if BN is smaller or equal 2**256-1
20
+ export function isUint256(bn: BigNumberish): boolean {
21
+ return toBN(bn).lte(UINT_256_MAX);
22
+ }
23
+
24
+ // function to convert BN to Uint256
25
+ export function bnToUint256(bignumber: BigNumberish): Uint256 {
26
+ const bn = toBN(bignumber);
27
+ if (!isUint256(bn)) throw new Error('Number is too large');
28
+ return {
29
+ low: addHexPrefix(bn.maskn(128).toString(16)),
30
+ high: addHexPrefix(bn.shrn(128).toString(16)),
31
+ };
32
+ }
@@ -0,0 +1,4 @@
1
+ export declare function isASCII(str: string): boolean;
2
+ export declare function isShortString(str: string): boolean;
3
+ export declare function encodeShortString(str: string): string;
4
+ export declare function decodeShortString(str: string): string;
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.decodeShortString =
4
+ exports.encodeShortString =
5
+ exports.isShortString =
6
+ exports.isASCII =
7
+ void 0;
8
+ var encode_1 = require('./encode');
9
+ function isASCII(str) {
10
+ // eslint-disable-next-line no-control-regex
11
+ return /^[\x00-\x7F]*$/.test(str);
12
+ }
13
+ exports.isASCII = isASCII;
14
+ // function to check if string has less or equal 31 characters
15
+ function isShortString(str) {
16
+ return str.length <= 31;
17
+ }
18
+ exports.isShortString = isShortString;
19
+ function encodeShortString(str) {
20
+ if (!isASCII(str)) throw new Error(str + ' is not an ASCII string');
21
+ if (!isShortString(str)) throw new Error(str + ' is too long');
22
+ return (0, encode_1.addHexPrefix)(
23
+ str.replace(/./g, function (char) {
24
+ return char.charCodeAt(0).toString(16);
25
+ })
26
+ );
27
+ }
28
+ exports.encodeShortString = encodeShortString;
29
+ function decodeShortString(str) {
30
+ return (0, encode_1.removeHexPrefix)(str).replace(/.{2}/g, function (hex) {
31
+ return String.fromCharCode(parseInt(hex, 16));
32
+ });
33
+ }
34
+ exports.decodeShortString = decodeShortString;
package/utils/stark.d.ts CHANGED
@@ -19,3 +19,8 @@ export declare function getSelectorFromName(funcName: string): string;
19
19
  export declare function randomAddress(): string;
20
20
  export declare function makeAddress(input: string): string;
21
21
  export declare function formatSignature(sig?: [BigNumberish, BigNumberish]): [string, string] | [];
22
+ export declare function compileStructToCalldata<
23
+ S extends {
24
+ [k: string]: string;
25
+ }
26
+ >(struct: S): string[];
package/utils/stark.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
  Object.defineProperty(exports, '__esModule', { value: true });
3
- exports.formatSignature =
3
+ exports.compileStructToCalldata =
4
+ exports.formatSignature =
4
5
  exports.makeAddress =
5
6
  exports.randomAddress =
6
7
  exports.getSelectorFromName =
@@ -62,3 +63,7 @@ function formatSignature(sig) {
62
63
  }
63
64
  }
64
65
  exports.formatSignature = formatSignature;
66
+ function compileStructToCalldata(struct) {
67
+ return Object.values(struct);
68
+ }
69
+ exports.compileStructToCalldata = compileStructToCalldata;
@@ -0,0 +1,11 @@
1
+ /// <reference types="bn.js" />
2
+ import { BigNumberish } from './number';
3
+ export interface Uint256 {
4
+ low: BigNumberish;
5
+ high: BigNumberish;
6
+ }
7
+ export declare function uint256ToBN(uint256: Uint256): import('bn.js');
8
+ export declare const UINT_128_MAX: import('bn.js');
9
+ export declare const UINT_256_MAX: import('bn.js');
10
+ export declare function isUint256(bn: BigNumberish): boolean;
11
+ export declare function bnToUint256(bignumber: BigNumberish): Uint256;
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+ Object.defineProperty(exports, '__esModule', { value: true });
3
+ exports.bnToUint256 =
4
+ exports.isUint256 =
5
+ exports.UINT_256_MAX =
6
+ exports.UINT_128_MAX =
7
+ exports.uint256ToBN =
8
+ void 0;
9
+ var encode_1 = require('./encode');
10
+ var number_1 = require('./number');
11
+ // function to convert Uint256 to BN
12
+ function uint256ToBN(uint256) {
13
+ return (0, number_1.toBN)(uint256.high)
14
+ .shln(128)
15
+ .add((0, number_1.toBN)(uint256.low));
16
+ }
17
+ exports.uint256ToBN = uint256ToBN;
18
+ exports.UINT_128_MAX = (0, number_1.toBN)(1)
19
+ .shln(128)
20
+ .sub((0, number_1.toBN)(1));
21
+ exports.UINT_256_MAX = (0, number_1.toBN)(1)
22
+ .shln(256)
23
+ .sub((0, number_1.toBN)(1));
24
+ // function to check if BN is smaller or equal 2**256-1
25
+ function isUint256(bn) {
26
+ return (0, number_1.toBN)(bn).lte(exports.UINT_256_MAX);
27
+ }
28
+ exports.isUint256 = isUint256;
29
+ // function to convert BN to Uint256
30
+ function bnToUint256(bignumber) {
31
+ var bn = (0, number_1.toBN)(bignumber);
32
+ if (!isUint256(bn)) throw new Error('Number is too large');
33
+ return {
34
+ low: (0, encode_1.addHexPrefix)(bn.maskn(128).toString(16)),
35
+ high: (0, encode_1.addHexPrefix)(bn.shrn(128).toString(16)),
36
+ };
37
+ }
38
+ exports.bnToUint256 = bnToUint256;