Skip to content

Commit

Permalink
add keccak256 mock
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Nov 9, 2023
1 parent f020b6f commit 7273646
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
25 changes: 25 additions & 0 deletions assembly/__tests__/vm-mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
sha256,
getKeysOf,
validateAddress,
keccak256,
} from '../std';
import { changeCallStack, resetStorage } from '../vm-mock/storage';
import {
Expand Down Expand Up @@ -106,6 +107,30 @@ describe('Testing mocked Storage and CallStack', () => {
);
});

test('Testing keccak256', () => {
function staticArrayToHexString(arr: StaticArray<u8>): string {
let result = '';
for (let i = 0; i < arr.length; i++) {
let hexValue = arr[i].toString(16).padStart(2, '0'); // Convert to hex and pad with zero if needed
result += hexValue;
}
return result;
}

const result = keccak256(
stringToBytes('The quick brown fox jumps over the lazy dog'),
);
expect(result).toStrictEqual([
77, 116, 27, 111, 30, 178, 156, 178, 169, 185, 145, 28, 130, 245, 111,
168, 215, 59, 4, 149, 157, 61, 157, 34, 40, 149, 223, 108, 11, 40, 170,
21,
]);

expect(staticArrayToHexString(result)).toBe(
'4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15',
);
});

test('Testing getKeysOf', () => {
Storage.setOf(testAddress, keyTest, valueTest);
Storage.setOf(testAddress, 'test2', valueTest);
Expand Down
35 changes: 18 additions & 17 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 @@ -37,6 +37,7 @@
"@massalabs/as-types": "^1.0.0",
"@massalabs/eslint-config": "^0.0.8",
"@massalabs/prettier-config-as": "^0.0.2",
"js-sha3": "^0.9.2",
"as-bignum": "^0.2.40",
"husky": "^8.0.2",
"lint-staged": "^13.0.3",
Expand Down
7 changes: 7 additions & 0 deletions vm-mock/vm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { createHash } = await import('node:crypto');
import sha3 from 'js-sha3';

/**
* Addresses and callstack
Expand Down Expand Up @@ -744,6 +745,12 @@ export default function createMockedABI(
return newString(generateRandOpId()
);
},

assembly_script_keccak256_hash(dataPtr) {
const data = getArrayBuffer(dataPtr);
const hash = sha3.keccak256.arrayBuffer(data);
return newArrayBuffer(hash);
},
},
};

Expand Down

0 comments on commit 7273646

Please sign in to comment.