Skip to content

Commit 8577b09

Browse files
committed
tmp3
1 parent e0827f7 commit 8577b09

File tree

11 files changed

+119
-107
lines changed

11 files changed

+119
-107
lines changed

.dockerignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ test/config.ini
4545

4646
# wasm
4747
wasm-wrappers/pkg/
48-
wasm-wrappers/js-bindings-test/test.js
49-
wasm-wrappers/js-bindings-test/test.js.map
48+
wasm-wrappers/js-bindings-test/dist/
5049

5150
# 'mintlayer-data' will be mapped to home directories of docker containers, so everything
5251
# inside it will be generated by the containers.

.github/workflows/wasm.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ jobs:
3535
uses: actions/setup-node@v3
3636
with:
3737
node-version: ${{ matrix.node-version }}
38-
- name: Install TypeScript
39-
run: npm install typescript
38+
- name: Install TypeScript & Knip
39+
run: npm install typescript knip
4040
- name: Install wasm-pack
4141
run: cargo install wasm-pack
4242
- name: Build the wasm module
@@ -48,6 +48,9 @@ jobs:
4848
- name: Run the tests
4949
working-directory: ./wasm-wrappers
5050
run: node --enable-source-maps js-bindings-test/node-entry.js
51+
- name: Run Knip
52+
working-directory: ./wasm-wrappers/js-bindings-test
53+
run: npx knip
5154

5255
wasm_artifacts:
5356
runs-on: ubuntu-latest

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ test/config.ini
4343

4444
# wasm
4545
wasm-wrappers/pkg/
46-
wasm-wrappers/js-bindings-test/test.js
47-
wasm-wrappers/js-bindings-test/test.js.map
46+
wasm-wrappers/js-bindings-test/dist/
4847

4948
# 'mintlayer-data' will be mapped to home directories of docker containers, so everything
5049
# inside it will be generated by the containers.

wasm-wrappers/README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ In the wasm Cargo.toml directory, run:
3535
tsc --project js-bindings-test/tsconfig.json
3636
```
3737

38-
### Running the tests in a web browser
38+
### Run the tests in a web browser
3939

4040
To build the wasm package from the crate, run (in the wasm Cargo.toml directory):
4141

@@ -63,7 +63,7 @@ http-server --port 8080 --host 0.0.0.0
6363

6464
The ported wasm functions are exported to the file `js-bindings-test/index.js` and used in the file `js-bindings-test/index.html` with a basic test/example in them using JavaScript. Use your browser's console to see the output.
6565

66-
### Running the tests in Node.js
66+
### Run the tests in Node.js
6767

6868
To build the wasm package from the crate, run (in the wasm Cargo.toml directory):
6969

@@ -77,6 +77,25 @@ Finally, to run the example, run:
7777
node --enable-source-maps js-bindings-test/node-entry.js
7878
```
7979

80+
### Run `knip`
81+
82+
We use `knip` to make sure that there are no unused exports in `js-bindings-test/tests` (which could
83+
mean that some of the tests are never run).
84+
85+
**Note: unused local definitions are caught by the TypeScript compiler itself, via the `noUnusedLocals` setting.**
86+
87+
To run `knip` locally, first install it:
88+
```
89+
npm install -g knip
90+
```
91+
92+
And then run (in the wasm Cargo.toml directory):
93+
```
94+
(cd js-bindings-test && npx knip)
95+
```
96+
97+
**Note: to explicitly exclude an export from `knip`'s report, annotate it with `/** @public */`.**
98+
8099
### Further documentation on wasm
81100

82101
- https://developer.mozilla.org/en-US/docs/WebAssembly/Rust_to_wasm
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "https://unpkg.com/knip@5/schema.json",
3+
"entry": ["tests/main.ts"],
4+
"project": ["tests/**/*.ts"],
5+
"includeEntryExports": true,
6+
"ignoreExportsUsedInFile": true
7+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { run_test } from "./test.js";
1+
import { run_all_tests } from "./dist/main.js";
22

33
async function node_run() {
4-
await run_test();
4+
await run_all_tests();
55
}
66

77
node_run();

wasm-wrappers/js-bindings-test/test.ts renamed to wasm-wrappers/js-bindings-test/tests/main.ts

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -62,43 +62,60 @@ import {
6262
encode_input_for_unfreeze_token,
6363
encode_input_for_change_token_authority,
6464
encode_input_for_change_token_metadata_uri,
65-
} from "../pkg/wasm_wrappers.js";
65+
} from "../../pkg/wasm_wrappers.js";
6666

67-
import { test_signed_transaction_intent } from "./tests/signed_transaction_intent_tests.js";
67+
import { qwe } from "./signed_transaction_intent_tests.js";
6868
import {
69+
MNEMONIC,
6970
TEXT_ENCODER,
71+
PRIVATE_KEY,
72+
PUBLIC_KEY,
7073
ORDERS_V1_TESTNET_FORK_HEIGHT, assert_eq_arrays
71-
} from "./tests/utils.js";
74+
} from "./utils.js";
7275

7376
function run_one_test(test_func) {
7477
console.log(`>> Running ${test_func.name}`);
7578
test_func();
7679
console.log(`<< Done running ${test_func.name}`);
7780
}
7881

79-
export async function run_test() {
82+
/** @public */
83+
export async function run_all_tests() {
84+
run_one_test(main_test);
85+
run_one_test(test_verify_signature_for_spending);
86+
run_one_test(test_public_key_from_bad_private_key);
87+
run_one_test(test_make_default_account_privkey_from_bad_mnemonic);
88+
run_one_test(test_sign_challenge);
89+
run_one_test(test_address_generation);
90+
91+
run_one_test(test_get_transaction_id);
92+
// run_one_test(test_signed_transaction_intent);
93+
94+
qwe();
95+
}
96+
97+
async function test_verify_signature_for_spending() {
8098
// Try signature verification
81-
const priv_key = make_private_key();
82-
console.log(`priv key = ${priv_key}`);
83-
const pub_key = public_key_from_private_key(priv_key);
84-
console.log(`pub key = ${pub_key}`);
8599
const message = TEXT_ENCODER.encode("Hello, world!");
86-
const signature = sign_message_for_spending(priv_key, message);
87-
console.log(`signature = ${signature}`);
88-
const verified = verify_signature_for_spending(pub_key, signature, message);
89-
console.log(`verified valid message with correct key = ${verified}`);
100+
101+
const signature = sign_message_for_spending(PRIVATE_KEY, message);
102+
103+
const verified = verify_signature_for_spending(PUBLIC_KEY, signature, message);
104+
90105
if (!verified) {
91106
throw new Error("Signature verification failed!");
92107
}
93108
const verified_bad = verify_signature_for_spending(
94-
pub_key,
109+
PUBLIC_KEY,
95110
signature,
96111
TEXT_ENCODER.encode("bro!")
97112
);
98113
if (verified_bad) {
99114
throw new Error("Invalid message signature verification passed!");
100115
}
116+
}
101117

118+
async function test_public_key_from_bad_private_key() {
102119
// Attempt to use a bad private key to get a public key (test returned Result<> object, which will become a string error)
103120
const bad_priv_key = TEXT_ENCODER.encode("bad");
104121
try {
@@ -112,7 +129,9 @@ export async function run_test() {
112129
}
113130
console.log("Tested decoding bad private key successfully");
114131
}
132+
}
115133

134+
async function test_make_default_account_privkey_from_bad_mnemonic() {
116135
try {
117136
const invalid_mnemonic = "asd asd";
118137
make_default_account_privkey(invalid_mnemonic, Network.Mainnet);
@@ -123,27 +142,33 @@ export async function run_test() {
123142
}
124143
console.log("Tested invalid mnemonic successfully");
125144
}
145+
}
126146

127-
{
128-
let challenge = sign_challenge(priv_key, message);
129-
let address = pubkey_to_pubkeyhash_address(pub_key, Network.Testnet);
130-
let result = verify_challenge(address, Network.Testnet, challenge, message);
131-
if (!result) {
132-
throw new Error("Invalid sing and verify challenge");
133-
}
147+
async function test_sign_challenge() {
148+
const message = TEXT_ENCODER.encode("Hello, world!");
134149

135-
const different_priv_key = make_private_key();
136-
const different_pub_key = public_key_from_private_key(different_priv_key);
137-
let different_address = pubkey_to_pubkeyhash_address(different_pub_key, Network.Testnet);
138-
try {
139-
verify_challenge(different_address, Network.Testnet, challenge, message);
140-
} catch (e) {
141-
if (!e.includes("Public key to public key hash mismatch")) {
142-
throw e;
143-
}
144-
console.log("Tested verify with different address successfully");
150+
let challenge = sign_challenge(PRIVATE_KEY, message);
151+
let address = pubkey_to_pubkeyhash_address(PUBLIC_KEY, Network.Testnet);
152+
let result = verify_challenge(address, Network.Testnet, challenge, message);
153+
if (!result) {
154+
throw new Error("Invalid sing and verify challenge");
155+
}
156+
157+
const different_priv_key = make_private_key();
158+
const different_pub_key = public_key_from_private_key(different_priv_key);
159+
let different_address = pubkey_to_pubkeyhash_address(different_pub_key, Network.Testnet);
160+
try {
161+
verify_challenge(different_address, Network.Testnet, challenge, message);
162+
} catch (e) {
163+
if (!e.includes("Public key to public key hash mismatch")) {
164+
throw e;
145165
}
166+
console.log("Tested verify with different address successfully");
146167
}
168+
}
169+
170+
async function test_address_generation() {
171+
const bad_priv_key = TEXT_ENCODER.encode("bad");
147172

148173
try {
149174
make_receiving_address(bad_priv_key, 0);
@@ -165,11 +190,9 @@ export async function run_test() {
165190
console.log("Tested decoding bad account private key successfully");
166191
}
167192

168-
const mnemonic =
169-
"walk exile faculty near leg neutral license matrix maple invite cupboard hat opinion excess coffee leopard latin regret document core limb crew dizzy movie";
170193
{
171194
const account_private_key = make_default_account_privkey(
172-
mnemonic,
195+
MNEMONIC,
173196
Network.Mainnet
174197
);
175198
console.log(`acc private key = ${account_private_key}`);
@@ -234,7 +257,7 @@ export async function run_test() {
234257
{
235258
// Test generating an address for Testnet
236259
const account_private_key = make_default_account_privkey(
237-
mnemonic,
260+
MNEMONIC,
238261
Network.Testnet
239262
);
240263
console.log(`acc private key = ${account_private_key}`);
@@ -252,7 +275,9 @@ export async function run_test() {
252275
throw new Error("Incorrect address generated");
253276
}
254277
}
278+
}
255279

280+
async function main_test() {
256281
{
257282
const lock_for_blocks = staking_pool_spend_maturity_block_count(
258283
BigInt(1000),
@@ -574,7 +599,7 @@ export async function run_test() {
574599
assert_eq_arrays(encoded_fungible_token, expected_fungible_token);
575600

576601
const account_pubkey = make_default_account_privkey(
577-
mnemonic,
602+
MNEMONIC,
578603
Network.Testnet
579604
);
580605
const receiving_privkey = make_receiving_address(account_pubkey, 0);
@@ -1423,9 +1448,6 @@ export async function run_test() {
14231448
throw new Error(`Effective balance test failed ${eff_bal}`);
14241449
}
14251450
}
1426-
1427-
run_one_test(test_get_transaction_id);
1428-
run_one_test(test_signed_transaction_intent);
14291451
}
14301452

14311453
function test_get_transaction_id() {

wasm-wrappers/js-bindings-test/tests/signed_transaction_intent_tests.ts

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,18 @@
11
import {
2-
make_private_key,
3-
public_key_from_private_key,
4-
sign_message_for_spending,
5-
verify_signature_for_spending,
6-
make_default_account_privkey,
7-
make_receiving_address,
8-
make_change_address,
9-
pubkey_to_pubkeyhash_address,
10-
make_receiving_address_public_key,
11-
make_change_address_public_key,
12-
extended_public_key_from_extended_private_key,
132
Network,
14-
encode_input_for_utxo,
15-
encode_output_coin_burn,
16-
encode_output_token_burn,
17-
encode_transaction,
18-
encode_witness_no_signature,
19-
encode_signed_transaction,
20-
encode_lock_until_height,
21-
encode_output_create_stake_pool,
22-
encode_output_token_transfer,
23-
encode_output_lock_then_transfer,
24-
encode_output_token_lock_then_transfer,
25-
encode_stake_pool_data,
26-
encode_output_htlc,
27-
encode_witness,
28-
encode_witness_htlc_secret,
29-
encode_multisig_challenge,
30-
encode_witness_htlc_multisig,
31-
extract_htlc_secret,
32-
encode_create_order_output,
33-
encode_input_for_fill_order,
34-
encode_input_for_freeze_order,
35-
encode_input_for_conclude_order,
36-
SignatureHashType,
37-
encode_input_for_withdraw_from_delegation,
38-
estimate_transaction_size,
39-
staking_pool_spend_maturity_block_count,
40-
get_transaction_id,
41-
effective_pool_balance,
42-
Amount,
43-
TotalSupply,
44-
FreezableToken,
45-
encode_output_issue_nft,
46-
encode_output_issue_fungible_token,
473
sign_challenge,
48-
verify_challenge,
49-
get_token_id,
504
make_transaction_intent_message_to_sign,
515
encode_signed_transaction_intent,
526
verify_transaction_intent,
53-
encode_input_for_mint_tokens,
54-
encode_input_for_unmint_tokens,
55-
encode_input_for_lock_token_supply,
56-
encode_input_for_freeze_token,
57-
TokenUnfreezable,
58-
encode_input_for_unfreeze_token,
59-
encode_input_for_change_token_authority,
60-
encode_input_for_change_token_metadata_uri,
617
} from "../../pkg/wasm_wrappers.js";
628

639
import {
6410
TEXT_ENCODER,
6511
assert_eq_arrays
6612
} from "./utils.js";
6713

14+
export function qwe() {}
15+
6816
export function test_signed_transaction_intent() {
6917
try {
7018
const invalid_tx_id = "invalid tx id";

wasm-wrappers/js-bindings-test/tests/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1+
import {
2+
make_private_key,
3+
public_key_from_private_key,
4+
} from "../../pkg/wasm_wrappers.js";
5+
16
// Taken from TESTNET_FORK_HEIGHT_5_ORDERS_V1 in common/src/chain/config/builder.rs.
27
// This will be updated to the actual height after we choose one.
38
export const ORDERS_V1_TESTNET_FORK_HEIGHT = 999_999_999;
49

510
export const TEXT_ENCODER = new TextEncoder();
611

12+
export const MNEMONIC =
13+
"walk exile faculty near leg neutral license matrix maple invite cupboard hat opinion excess coffee leopard latin regret document core limb crew dizzy movie";
14+
15+
export const PRIVATE_KEY = make_private_key();
16+
export const PUBLIC_KEY = public_key_from_private_key(PRIVATE_KEY);
17+
18+
console.log(`PRIVATE_KEY = ${PRIVATE_KEY}`);
19+
console.log(`PUBLIC_KEY = ${PUBLIC_KEY}`);
20+
721
export function assert_eq_arrays(arr1, arr2) {
822
const equal = arr1.length == arr2.length && arr1.every((value, index) => value == arr2[index]);
923

wasm-wrappers/js-bindings-test/tsconfig.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"compilerOptions": {
33
"target": "es2022",
44
"module": "nodenext",
5-
"sourceMap": true
6-
},
7-
"include": ["test.ts", "tests/**/*.ts" ],
5+
"sourceMap": true,
6+
"outDir": "./dist",
7+
"noUnusedLocals": true
8+
}
89
}

0 commit comments

Comments
 (0)