Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mocks for evm signature functions #309

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Massa-as-sdk

![check-code-coverage](https://img.shields.io/badge/coverage-82%25-green)
![check-code-coverage](https://img.shields.io/badge/coverage-83%%25-red)

Massa-as-sdk is a collection of tools, objects, and functions specifically designed for Massa smart contracts in AssemblyScript. This SDK enables you to import object classes, such as address and storage objects, and use them without having to write them from scratch every time. Additionally, it allows you to use Massa's ABI functions.

Expand Down
11 changes: 11 additions & 0 deletions assembly/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,14 @@ export function staticArrayToHexString(arr: StaticArray<u8>): string {
}
return result;
}

export function hexStringToStaticArray(hexString: string): StaticArray<u8> {
let result = new StaticArray<u8>(hexString.length / 2);

for (let i = 0; i < hexString.length; i += 2) {
let byte = parseInt(hexString.substr(i, 2), 16);
result[i / 2] = <u8>byte;
}

return result;
}
35 changes: 34 additions & 1 deletion assembly/__tests__/vm-mock.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-len */
import {
Address,
Storage,
Expand All @@ -7,6 +8,8 @@ import {
getKeysOf,
validateAddress,
keccak256,
isEvmSignatureValid,
evmGetPubkeyFromSignature,
} from '../std';
import { changeCallStack, resetStorage } from '../vm-mock/storage';
import {
Expand All @@ -17,7 +20,7 @@ import {
import { Args, bytesToString, stringToBytes } from '@massalabs/as-types';
import { env } from '../env/index';
import { callee, caller, isDeployingContract } from '../std/context';
import { staticArrayToHexString } from './utils';
import { hexStringToStaticArray, staticArrayToHexString } from './utils';

const testAddress = new Address(
'AU12E6N5BFAdC2wyiBV6VJjqkWhpz1kLVp2XpbRdSnL1mKjCWT6oR',
Expand Down Expand Up @@ -256,4 +259,34 @@ describe('Testing mocked Context', () => {

expect(caller().toString()).toStrictEqual(callee().toString());
});

it('should verify evm signature', () => {
const data = stringToBytes('Hello World');
const signature = hexStringToStaticArray(
'1617966ef37eaff4312132243df65cfb66ccda057e996b586f910d6eb422787453b0a8465be9716493650d6706bc84efe85a5d76a0111c89d0cdf8ba57e510571c',
);
// secp256k1 raw public key (compression header byte has been removed)
const publicKey = hexStringToStaticArray(
'ae04a0fb4545138d22ed46eee76e683c50412ffb8cb02ee8fa5a5c8eec35f72dc076cb1b7468f8cacf136a7e0609b31ed580746d8efc659a993ccd695d6387ff',
);

expect(isEvmSignatureValid(data, signature, publicKey)).toStrictEqual(true);
});

it('should get evm public key from signature', () => {
const digest = hexStringToStaticArray(
'a1de988600a42c4b4ab089b619297c17d53cffae5d5120d82d8a92d0bb3b78f2',
);
const signature = hexStringToStaticArray(
'1617966ef37eaff4312132243df65cfb66ccda057e996b586f910d6eb422787453b0a8465be9716493650d6706bc84efe85a5d76a0111c89d0cdf8ba57e510571c',
);
// secp256k1 public key (with compression header byte)
const publicKey = hexStringToStaticArray(
'04ae04a0fb4545138d22ed46eee76e683c50412ffb8cb02ee8fa5a5c8eec35f72dc076cb1b7468f8cacf136a7e0609b31ed580746d8efc659a993ccd695d6387ff',
);

expect(evmGetPubkeyFromSignature(digest, signature)).toStrictEqual(
publicKey,
);
});
});
161 changes: 155 additions & 6 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@
"@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",
"ethers": "^6.8.1",
"husky": "^8.0.2",
"js-sha3": "^0.9.2",
"lint-staged": "^13.0.3",
"prettier": "^2.7.1",
"typedoc": "^0.23.23",
Expand Down
Loading
Loading