Skip to content

Commit

Permalink
Merge pull request #52 from Terran-One/feature/ethereum_signature_ver…
Browse files Browse the repository at this point in the history
…ify_tests

Feature/ethereum signature verify tests
  • Loading branch information
Bunchhieng authored Oct 21, 2022
2 parents ca89293 + c3e1a64 commit 3ff79d0
Show file tree
Hide file tree
Showing 7 changed files with 1,447 additions and 1,278 deletions.
66 changes: 33 additions & 33 deletions .github/workflows/version.yaml
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
name: Auto-increment patch version number
on:
workflow_dispatch: {}
push:
branches:
- main
jobs:
build:
name: Bump version
if: "!contains(github.event.head_commit.message, 'ci-skip')"
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
actions: read
steps:
- name: Checkout repo
uses: actions/checkout@v3
with:
token: ${{ secrets.PACKAGE_PAT }}
- name: Bump
id: bump-version
run: |
YARN_VERSION_GIT_TAG='' yarn version --patch
NEW_VERSION=$(grep -Po '(?<="version": ").*(?=",)' package.json)
echo "Bumped version to $NEW_VERSION."
echo "::set-output name=version::$NEW_VERSION"
- name: Pull Remote Changes
run: git pull --autostash
- uses: stefanzweifel/git-auto-commit-action@v4
name: Commit & Push
with:
commit_message: Bumped version to ${{ steps.bump-version.outputs.version }} [ci-skip]"
#name: Auto-increment patch version number
#on:
# workflow_dispatch: {}
# push:
# branches:
# - main
#jobs:
# build:
# name: Bump version
# if: "!contains(github.event.head_commit.message, 'ci-skip')"
# runs-on: ubuntu-latest
# permissions:
# id-token: write
# contents: read
# actions: read
# steps:
# - name: Checkout repo
# uses: actions/checkout@v3
# with:
# token: ${{ secrets.PACKAGE_PAT }}
# - name: Bump
# id: bump-version
# run: |
# YARN_VERSION_GIT_TAG='' yarn version --patch
# NEW_VERSION=$(grep -Po '(?<="version": ").*(?=",)' package.json)
# echo "Bumped version to $NEW_VERSION."
# echo "::set-output name=version::$NEW_VERSION"
# - name: Pull Remote Changes
# run: git pull --autostash
# - uses: stefanzweifel/git-auto-commit-action@v4
# name: Commit & Push
# with:
# commit_message: Bumped version to ${{ steps.bump-version.outputs.version }} [ci-skip]"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ dist
target/
.vscode/
.idea/
package-lock.json
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
"dependencies": {
"@cosmjs/crypto": "^0.28.4",
"@cosmjs/encoding": "^0.28.4",
"@polkadot/util": "^10.1.11",
"@polkadot/util-crypto": "^10.1.11",
"@types/elliptic": "^6.4.14",
"@types/secp256k1": "^4.0.3",
"bech32": "^2.0.0",
Expand Down
91 changes: 46 additions & 45 deletions src/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ export class VMInstance {
}

public allocate(size: number): Region {
let { allocate, memory } = this.exports;
let {allocate, memory} = this.exports;
let regPtr = allocate(size);
return new Region(memory, regPtr);
}

public deallocate(region: Region): void {
let { deallocate } = this.exports;
let {deallocate} = this.exports;
deallocate(region.ptr);
}

Expand Down Expand Up @@ -95,35 +95,35 @@ export class VMInstance {
}

public instantiate(env: Env, info: MessageInfo, msg: object): Region {
let { instantiate } = this.exports;
let {instantiate} = this.exports;
let args = [env, info, msg].map((x) => this.allocate_json(x).ptr);
let result = instantiate(...args);
return this.region(result);
}

public execute(env: Env, info: MessageInfo, msg: object): Region {
let { execute } = this.exports;
let {execute} = this.exports;
let args = [env, info, msg].map((x) => this.allocate_json(x).ptr);
let result = execute(...args);
return this.region(result);
}

public query(env: Env, msg: object): Region {
let { query } = this.exports;
let {query} = this.exports;
let args = [env, msg].map((x) => this.allocate_json(x).ptr);
let result = query(...args);
return this.region(result);
}

public migrate(env: Env, msg: object): Region {
let { migrate } = this.exports;
let {migrate} = this.exports;
let args = [env, msg].map((x) => this.allocate_json(x).ptr);
let result = migrate(...args);
return this.region(result);
}

public reply(env: Env, msg: object): Region {
let { reply } = this.exports;
let {reply} = this.exports;
let args = [env, msg].map((x) => this.allocate_json(x).ptr);
let result = reply(...args);
return this.region(result);
Expand Down Expand Up @@ -174,9 +174,9 @@ export class VMInstance {
}

secp256k1_verify(
hash_ptr: number,
signature_ptr: number,
pubkey_ptr: number
hash_ptr: number,
signature_ptr: number,
pubkey_ptr: number
): number {
let hash = this.region(hash_ptr);
let signature = this.region(signature_ptr);
Expand All @@ -185,19 +185,19 @@ export class VMInstance {
}

secp256k1_recover_pubkey(
hash_ptr: number,
signature_ptr: number,
recover_param: number
): Uint8Array {
hash_ptr: number,
signature_ptr: number,
recover_param: number
): bigint {
let hash = this.region(hash_ptr);
let signature = this.region(signature_ptr);
return this.do_secp256k1_recover_pubkey(hash, signature, recover_param);
return BigInt(this.do_secp256k1_recover_pubkey(hash, signature, recover_param).ptr);
}

ed25519_verify(
message_ptr: number,
signature_ptr: number,
pubkey_ptr: number
message_ptr: number,
signature_ptr: number,
pubkey_ptr: number
): number {
let message = this.region(message_ptr);
let signature = this.region(signature_ptr);
Expand All @@ -206,9 +206,9 @@ export class VMInstance {
}

ed25519_batch_verify(
messages_ptr: number,
signatures_ptr: number,
public_keys_ptr: number
messages_ptr: number,
signatures_ptr: number,
public_keys_ptr: number
): number {
let messages = this.region(messages_ptr);
let signatures = this.region(signatures_ptr);
Expand Down Expand Up @@ -284,12 +284,12 @@ export class VMInstance {
}

return this.allocate_bytes(new Uint8Array(
[
...record.key,
...toByteArray(record.key.length, 4),
...record.value,
...toByteArray(record.value.length, 4)
]));
[
...record.key,
...toByteArray(record.key.length, 4),
...record.value,
...toByteArray(record.value.length, 4)
]));
}

do_addr_humanize(source: Region, destination: Region): Region {
Expand Down Expand Up @@ -329,7 +329,7 @@ export class VMInstance {
}

const canonical = this.bech32.fromWords(
this.bech32.decode(source.str).words
this.bech32.decode(source.str).words
);

if (canonical.length === 0) {
Expand All @@ -338,8 +338,8 @@ export class VMInstance {

// TODO: Change prefix to be configurable per environment
const human = this.bech32.encode(
this.PREFIX,
this.bech32.toWords(canonical)
this.PREFIX,
this.bech32.toWords(canonical)
);
if (human !== source.str) {
throw new Error('Invalid address.');
Expand All @@ -351,12 +351,12 @@ export class VMInstance {
// Returns 0 on verification success, 1 on verification failure
do_secp256k1_verify(hash: Region, signature: Region, pubkey: Region): number {
console.log(
`signature length: ${signature.str.length}, pubkey length: ${pubkey.str.length}, message length: ${hash.str.length}`
`signature length: ${signature.str.length}, pubkey length: ${pubkey.str.length}, message length: ${hash.str.length}`
);
const isValidSignature = ecdsaVerify(
signature.data,
hash.data,
pubkey.data
signature.data,
hash.data,
pubkey.data
);

if (isValidSignature) {
Expand All @@ -367,19 +367,20 @@ export class VMInstance {
}

do_secp256k1_recover_pubkey(
hash: Region,
signature: Region,
recover_param: number
): Uint8Array {
return ecdsaRecover(signature.data, recover_param, hash.data, false);
msgHash: Region,
signature: Region,
recover_param: number
): Region {
const pub = ecdsaRecover(signature.data, recover_param, msgHash.data, false);
return this.allocate_bytes(pub);
}

// Verifies a message against a signature with a public key, using the ed25519 EdDSA scheme.
// Returns 0 on verification success, 1 on verification failure
do_ed25519_verify(
message: Region,
signature: Region,
pubkey: Region
message: Region,
signature: Region,
pubkey: Region
): number {
const sig = Buffer.from(signature.data).toString('hex');
const pub = Buffer.from(pubkey.data).toString('hex');
Expand All @@ -400,9 +401,9 @@ export class VMInstance {
// using the ed25519 EdDSA scheme.
// Returns 0 on verification success (all batches verify correctly), 1 on verification failure
do_ed25519_batch_verify(
messages_ptr: Region,
signatures_ptr: Region,
public_keys_ptr: Region
messages_ptr: Region,
signatures_ptr: Region,
public_keys_ptr: Region
): number {
let messages = decodeSections(messages_ptr.data);
let signatures = decodeSections(signatures_ptr.data);
Expand Down
Loading

0 comments on commit 3ff79d0

Please sign in to comment.