Skip to content

Commit

Permalink
feat: implement needed helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Oct 25, 2021
1 parent 7e78a2c commit 46f7173
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 5 deletions.
14 changes: 13 additions & 1 deletion __tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import { compressProgram, makeAddress, isBrowser, JsonParser } from '..';
import { compressProgram, makeAddress, isBrowser, JsonParser, getSelectorFromName } from '../src';

const compiledArgentAccount = JsonParser.parse(
fs.readFileSync('./__mocks__/ArgentAccount.json').toString('ascii')
Expand Down Expand Up @@ -33,3 +33,15 @@ describe('makeAddress()', () => {
expect(starkAddress).toBe('0xdfd0f27fce99b50909de0bdd328aed6eabe76bc5');
});
});
describe('starknetKeccak()', () => {
test('hash works for value="test"', () => {
expect(getSelectorFromName('test')).toBe(
'0x22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658'
);
});
test('hash works for value="initialize"', () => {
expect(getSelectorFromName('initialize')).toBe(
'0x79dc0da7c54b95f10aa182ad0a46400db63156920adb65eca2654c0945a463'
);
});
});
55 changes: 53 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
},
"dependencies": {
"axios": "^0.23.0",
"ethereum-cryptography": "^0.2.0",
"json-bigint": "^1.0.0",
"pako": "^2.0.4"
},
Expand Down
36 changes: 35 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { gzip } from 'pako';
import Json from 'json-bigint';
import { keccak256 } from 'ethereum-cryptography/keccak';
import { CompressedProgram, Program } from './types';
import { CONTRACT_ADDRESS_LOWER_BOUND, CONTRACT_ADDRESS_UPPER_BOUND } from './constants';

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

export const arrayBufferToString = (array: ArrayBuffer): string =>
String.fromCharCode.apply(null, array as any);

export const stringToUint8Array = (str: string): Uint8Array => new TextEncoder().encode(str);

export const btoaUniversal = (b: ArrayBuffer): string =>
isBrowser ? btoa(String.fromCharCode.apply(null, b as any)) : Buffer.from(b).toString('base64');
isBrowser ? btoa(arrayBufferToString(b)) : Buffer.from(b).toString('base64');

export function randomIntFromInterval(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1) + min);
Expand Down Expand Up @@ -42,3 +48,31 @@ export function compressProgram(jsonProgram: Program | string): CompressedProgra
const base64 = btoaUniversal(compressedProgram);
return base64;
}

function buf2hex(buffer: Uint8Array) {
return [...buffer].map((x) => x.toString(16).padStart(2, '0')).join('');
}

const keccakHex = (value: string): string => buf2hex(keccak256(stringToUint8Array(value)));

/**
* Function to get the starknet keccak hash from a string
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L17-L22)
* @param value - string you want to get the starknetKeccak hash from
* @returns starknet keccak hash as hex string
*/
export function starknetKeccak(value: string): string {
return `0x${keccakHex(value).substr(2)}`;
}

/**
* Function to get the hex selector from a given function name
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/public/abi.py#L25-L26)
* @param funcName - selectors abi function name
* @returns hex selector of given abi function name
*/
export function getSelectorFromName(funcName: string) {
return starknetKeccak(funcName);
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
"downlevelIteration": true /* Emit more compliant, but verbose and less performant JavaScript for iteration. */,
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
Expand Down

0 comments on commit 46f7173

Please sign in to comment.