Skip to content

Commit 46f7173

Browse files
committed
feat: implement needed helper methods
1 parent 7e78a2c commit 46f7173

File tree

5 files changed

+103
-5
lines changed

5 files changed

+103
-5
lines changed

__tests__/utils.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs';
2-
import { compressProgram, makeAddress, isBrowser, JsonParser } from '..';
2+
import { compressProgram, makeAddress, isBrowser, JsonParser, getSelectorFromName } from '../src';
33

44
const compiledArgentAccount = JsonParser.parse(
55
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
@@ -33,3 +33,15 @@ describe('makeAddress()', () => {
3333
expect(starkAddress).toBe('0xdfd0f27fce99b50909de0bdd328aed6eabe76bc5');
3434
});
3535
});
36+
describe('starknetKeccak()', () => {
37+
test('hash works for value="test"', () => {
38+
expect(getSelectorFromName('test')).toBe(
39+
'0x22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658'
40+
);
41+
});
42+
test('hash works for value="initialize"', () => {
43+
expect(getSelectorFromName('initialize')).toBe(
44+
'0x79dc0da7c54b95f10aa182ad0a46400db63156920adb65eca2654c0945a463'
45+
);
46+
});
47+
});

package-lock.json

Lines changed: 53 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
},
5757
"dependencies": {
5858
"axios": "^0.23.0",
59+
"ethereum-cryptography": "^0.2.0",
5960
"json-bigint": "^1.0.0",
6061
"pako": "^2.0.4"
6162
},

src/utils.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { gzip } from 'pako';
22
import Json from 'json-bigint';
3+
import { keccak256 } from 'ethereum-cryptography/keccak';
34
import { CompressedProgram, Program } from './types';
45
import { CONTRACT_ADDRESS_LOWER_BOUND, CONTRACT_ADDRESS_UPPER_BOUND } from './constants';
56

67
export const isBrowser = typeof window !== 'undefined';
78

9+
export const arrayBufferToString = (array: ArrayBuffer): string =>
10+
String.fromCharCode.apply(null, array as any);
11+
12+
export const stringToUint8Array = (str: string): Uint8Array => new TextEncoder().encode(str);
13+
814
export const btoaUniversal = (b: ArrayBuffer): string =>
9-
isBrowser ? btoa(String.fromCharCode.apply(null, b as any)) : Buffer.from(b).toString('base64');
15+
isBrowser ? btoa(arrayBufferToString(b)) : Buffer.from(b).toString('base64');
1016

1117
export function randomIntFromInterval(min: number, max: number) {
1218
return Math.floor(Math.random() * (max - min + 1) + min);
@@ -42,3 +48,31 @@ export function compressProgram(jsonProgram: Program | string): CompressedProgra
4248
const base64 = btoaUniversal(compressedProgram);
4349
return base64;
4450
}
51+
52+
function buf2hex(buffer: Uint8Array) {
53+
return [...buffer].map((x) => x.toString(16).padStart(2, '0')).join('');
54+
}
55+
56+
const keccakHex = (value: string): string => buf2hex(keccak256(stringToUint8Array(value)));
57+
58+
/**
59+
* Function to get the starknet keccak hash from a string
60+
*
61+
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L17-L22)
62+
* @param value - string you want to get the starknetKeccak hash from
63+
* @returns starknet keccak hash as hex string
64+
*/
65+
export function starknetKeccak(value: string): string {
66+
return `0x${keccakHex(value).substr(2)}`;
67+
}
68+
69+
/**
70+
* Function to get the hex selector from a given function name
71+
*
72+
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
73+
* @param funcName - selectors abi function name
74+
* @returns hex selector of given abi function name
75+
*/
76+
export function getSelectorFromName(funcName: string) {
77+
return starknetKeccak(funcName);
78+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
// "noEmit": true, /* Disable emitting files from a compilation. */
5858
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
5959
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
60-
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
60+
"downlevelIteration": true /* Emit more compliant, but verbose and less performant JavaScript for iteration. */,
6161
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
6262
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
6363
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */

0 commit comments

Comments
 (0)