Skip to content

Commit

Permalink
feat: add computeHashOnElements
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Nov 16, 2021
1 parent 063198a commit ec6d3d3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
18 changes: 18 additions & 0 deletions __tests__/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,21 @@ describe('getSelectorFromName()', () => {
);
});
});
describe('computeHashOnElements()', () => {
test('should return valid hash for empty array', () => {
const res = stark.computeHashOnElements([]);
expect(res).toMatchInlineSnapshot(
`"0x49ee3eba8c1600700ee1b87eb599f16716b0b1022947733551fde4050ca6804"`
);
});
test('should return valid hash for valid array', () => {
const res = stark.computeHashOnElements([
number.toBN(123782376),
number.toBN(213984),
number.toBN(128763521321),
]);
expect(res).toMatchInlineSnapshot(
`"0x7b422405da6571242dfc245a43de3b0fe695e7021c148b918cd9cdb462cac59"`
);
});
});
6 changes: 5 additions & 1 deletion src/utils/stark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { gzip } from 'pako';
import { CompressedProgram, Program } from '../types';
import { genKeyPair, getStarkKey } from './ellipticCurve';
import { addHexPrefix, btoaUniversal } from './encode';
import { starknetKeccak } from './hash';
import { pedersen, starknetKeccak } from './hash';
import { stringify } from './json';
import { BigNumberish, toBN, toHex } from './number';

Expand Down Expand Up @@ -49,3 +49,7 @@ export function formatSignature(sig?: [BigNumberish, BigNumberish]): [string, st
return [];
}
}

export function computeHashOnElements(data: BigNumberish[]) {
return [...data, data.length].reduce((x, y) => pedersen([x, y]), 0);
}

0 comments on commit ec6d3d3

Please sign in to comment.