Skip to content

Commit 3ff79d0

Browse files
authored
Merge pull request #52 from Terran-One/feature/ethereum_signature_verify_tests
Feature/ethereum signature verify tests
2 parents ca89293 + c3e1a64 commit 3ff79d0

File tree

7 files changed

+1447
-1278
lines changed

7 files changed

+1447
-1278
lines changed

.github/workflows/version.yaml

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
name: Auto-increment patch version number
2-
on:
3-
workflow_dispatch: {}
4-
push:
5-
branches:
6-
- main
7-
jobs:
8-
build:
9-
name: Bump version
10-
if: "!contains(github.event.head_commit.message, 'ci-skip')"
11-
runs-on: ubuntu-latest
12-
permissions:
13-
id-token: write
14-
contents: read
15-
actions: read
16-
steps:
17-
- name: Checkout repo
18-
uses: actions/checkout@v3
19-
with:
20-
token: ${{ secrets.PACKAGE_PAT }}
21-
- name: Bump
22-
id: bump-version
23-
run: |
24-
YARN_VERSION_GIT_TAG='' yarn version --patch
25-
NEW_VERSION=$(grep -Po '(?<="version": ").*(?=",)' package.json)
26-
echo "Bumped version to $NEW_VERSION."
27-
echo "::set-output name=version::$NEW_VERSION"
28-
- name: Pull Remote Changes
29-
run: git pull --autostash
30-
- uses: stefanzweifel/git-auto-commit-action@v4
31-
name: Commit & Push
32-
with:
33-
commit_message: Bumped version to ${{ steps.bump-version.outputs.version }} [ci-skip]"
1+
#name: Auto-increment patch version number
2+
#on:
3+
# workflow_dispatch: {}
4+
# push:
5+
# branches:
6+
# - main
7+
#jobs:
8+
# build:
9+
# name: Bump version
10+
# if: "!contains(github.event.head_commit.message, 'ci-skip')"
11+
# runs-on: ubuntu-latest
12+
# permissions:
13+
# id-token: write
14+
# contents: read
15+
# actions: read
16+
# steps:
17+
# - name: Checkout repo
18+
# uses: actions/checkout@v3
19+
# with:
20+
# token: ${{ secrets.PACKAGE_PAT }}
21+
# - name: Bump
22+
# id: bump-version
23+
# run: |
24+
# YARN_VERSION_GIT_TAG='' yarn version --patch
25+
# NEW_VERSION=$(grep -Po '(?<="version": ").*(?=",)' package.json)
26+
# echo "Bumped version to $NEW_VERSION."
27+
# echo "::set-output name=version::$NEW_VERSION"
28+
# - name: Pull Remote Changes
29+
# run: git pull --autostash
30+
# - uses: stefanzweifel/git-auto-commit-action@v4
31+
# name: Commit & Push
32+
# with:
33+
# commit_message: Bumped version to ${{ steps.bump-version.outputs.version }} [ci-skip]"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ dist
77
target/
88
.vscode/
99
.idea/
10+
package-lock.json

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
"dependencies": {
6767
"@cosmjs/crypto": "^0.28.4",
6868
"@cosmjs/encoding": "^0.28.4",
69+
"@polkadot/util": "^10.1.11",
70+
"@polkadot/util-crypto": "^10.1.11",
6971
"@types/elliptic": "^6.4.14",
7072
"@types/secp256k1": "^4.0.3",
7173
"bech32": "^2.0.0",

src/instance.ts

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ export class VMInstance {
5656
}
5757

5858
public allocate(size: number): Region {
59-
let { allocate, memory } = this.exports;
59+
let {allocate, memory} = this.exports;
6060
let regPtr = allocate(size);
6161
return new Region(memory, regPtr);
6262
}
6363

6464
public deallocate(region: Region): void {
65-
let { deallocate } = this.exports;
65+
let {deallocate} = this.exports;
6666
deallocate(region.ptr);
6767
}
6868

@@ -95,35 +95,35 @@ export class VMInstance {
9595
}
9696

9797
public instantiate(env: Env, info: MessageInfo, msg: object): Region {
98-
let { instantiate } = this.exports;
98+
let {instantiate} = this.exports;
9999
let args = [env, info, msg].map((x) => this.allocate_json(x).ptr);
100100
let result = instantiate(...args);
101101
return this.region(result);
102102
}
103103

104104
public execute(env: Env, info: MessageInfo, msg: object): Region {
105-
let { execute } = this.exports;
105+
let {execute} = this.exports;
106106
let args = [env, info, msg].map((x) => this.allocate_json(x).ptr);
107107
let result = execute(...args);
108108
return this.region(result);
109109
}
110110

111111
public query(env: Env, msg: object): Region {
112-
let { query } = this.exports;
112+
let {query} = this.exports;
113113
let args = [env, msg].map((x) => this.allocate_json(x).ptr);
114114
let result = query(...args);
115115
return this.region(result);
116116
}
117117

118118
public migrate(env: Env, msg: object): Region {
119-
let { migrate } = this.exports;
119+
let {migrate} = this.exports;
120120
let args = [env, msg].map((x) => this.allocate_json(x).ptr);
121121
let result = migrate(...args);
122122
return this.region(result);
123123
}
124124

125125
public reply(env: Env, msg: object): Region {
126-
let { reply } = this.exports;
126+
let {reply} = this.exports;
127127
let args = [env, msg].map((x) => this.allocate_json(x).ptr);
128128
let result = reply(...args);
129129
return this.region(result);
@@ -174,9 +174,9 @@ export class VMInstance {
174174
}
175175

176176
secp256k1_verify(
177-
hash_ptr: number,
178-
signature_ptr: number,
179-
pubkey_ptr: number
177+
hash_ptr: number,
178+
signature_ptr: number,
179+
pubkey_ptr: number
180180
): number {
181181
let hash = this.region(hash_ptr);
182182
let signature = this.region(signature_ptr);
@@ -185,19 +185,19 @@ export class VMInstance {
185185
}
186186

187187
secp256k1_recover_pubkey(
188-
hash_ptr: number,
189-
signature_ptr: number,
190-
recover_param: number
191-
): Uint8Array {
188+
hash_ptr: number,
189+
signature_ptr: number,
190+
recover_param: number
191+
): bigint {
192192
let hash = this.region(hash_ptr);
193193
let signature = this.region(signature_ptr);
194-
return this.do_secp256k1_recover_pubkey(hash, signature, recover_param);
194+
return BigInt(this.do_secp256k1_recover_pubkey(hash, signature, recover_param).ptr);
195195
}
196196

197197
ed25519_verify(
198-
message_ptr: number,
199-
signature_ptr: number,
200-
pubkey_ptr: number
198+
message_ptr: number,
199+
signature_ptr: number,
200+
pubkey_ptr: number
201201
): number {
202202
let message = this.region(message_ptr);
203203
let signature = this.region(signature_ptr);
@@ -206,9 +206,9 @@ export class VMInstance {
206206
}
207207

208208
ed25519_batch_verify(
209-
messages_ptr: number,
210-
signatures_ptr: number,
211-
public_keys_ptr: number
209+
messages_ptr: number,
210+
signatures_ptr: number,
211+
public_keys_ptr: number
212212
): number {
213213
let messages = this.region(messages_ptr);
214214
let signatures = this.region(signatures_ptr);
@@ -284,12 +284,12 @@ export class VMInstance {
284284
}
285285

286286
return this.allocate_bytes(new Uint8Array(
287-
[
288-
...record.key,
289-
...toByteArray(record.key.length, 4),
290-
...record.value,
291-
...toByteArray(record.value.length, 4)
292-
]));
287+
[
288+
...record.key,
289+
...toByteArray(record.key.length, 4),
290+
...record.value,
291+
...toByteArray(record.value.length, 4)
292+
]));
293293
}
294294

295295
do_addr_humanize(source: Region, destination: Region): Region {
@@ -329,7 +329,7 @@ export class VMInstance {
329329
}
330330

331331
const canonical = this.bech32.fromWords(
332-
this.bech32.decode(source.str).words
332+
this.bech32.decode(source.str).words
333333
);
334334

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

339339
// TODO: Change prefix to be configurable per environment
340340
const human = this.bech32.encode(
341-
this.PREFIX,
342-
this.bech32.toWords(canonical)
341+
this.PREFIX,
342+
this.bech32.toWords(canonical)
343343
);
344344
if (human !== source.str) {
345345
throw new Error('Invalid address.');
@@ -351,12 +351,12 @@ export class VMInstance {
351351
// Returns 0 on verification success, 1 on verification failure
352352
do_secp256k1_verify(hash: Region, signature: Region, pubkey: Region): number {
353353
console.log(
354-
`signature length: ${signature.str.length}, pubkey length: ${pubkey.str.length}, message length: ${hash.str.length}`
354+
`signature length: ${signature.str.length}, pubkey length: ${pubkey.str.length}, message length: ${hash.str.length}`
355355
);
356356
const isValidSignature = ecdsaVerify(
357-
signature.data,
358-
hash.data,
359-
pubkey.data
357+
signature.data,
358+
hash.data,
359+
pubkey.data
360360
);
361361

362362
if (isValidSignature) {
@@ -367,19 +367,20 @@ export class VMInstance {
367367
}
368368

369369
do_secp256k1_recover_pubkey(
370-
hash: Region,
371-
signature: Region,
372-
recover_param: number
373-
): Uint8Array {
374-
return ecdsaRecover(signature.data, recover_param, hash.data, false);
370+
msgHash: Region,
371+
signature: Region,
372+
recover_param: number
373+
): Region {
374+
const pub = ecdsaRecover(signature.data, recover_param, msgHash.data, false);
375+
return this.allocate_bytes(pub);
375376
}
376377

377378
// Verifies a message against a signature with a public key, using the ed25519 EdDSA scheme.
378379
// Returns 0 on verification success, 1 on verification failure
379380
do_ed25519_verify(
380-
message: Region,
381-
signature: Region,
382-
pubkey: Region
381+
message: Region,
382+
signature: Region,
383+
pubkey: Region
383384
): number {
384385
const sig = Buffer.from(signature.data).toString('hex');
385386
const pub = Buffer.from(pubkey.data).toString('hex');
@@ -400,9 +401,9 @@ export class VMInstance {
400401
// using the ed25519 EdDSA scheme.
401402
// Returns 0 on verification success (all batches verify correctly), 1 on verification failure
402403
do_ed25519_batch_verify(
403-
messages_ptr: Region,
404-
signatures_ptr: Region,
405-
public_keys_ptr: Region
404+
messages_ptr: Region,
405+
signatures_ptr: Region,
406+
public_keys_ptr: Region
406407
): number {
407408
let messages = decodeSections(messages_ptr.data);
408409
let signatures = decodeSections(signatures_ptr.data);

0 commit comments

Comments
 (0)