Skip to content

Commit ab44d89

Browse files
committed
fix tests, wasm fetch
1 parent 0eeb44e commit ab44d89

32 files changed

+181
-126
lines changed

.yarn/install-state.gz

-6 Bytes
Binary file not shown.

package.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22
"name": "@namada/sdk",
33
"version": "1.0.0",
44
"description": "Namada SDK Package",
5-
"main": "dist/index.js",
6-
"types": "dist/index.d.ts",
5+
"main": "lib/cjs/index.js",
6+
"module": "lib/esm/index.js",
7+
"types": "lib/esm/index.d.ts",
8+
"exports": {
9+
"import": "./lib/esm/index.js",
10+
"require": "./lib/cjs/index.js"
11+
},
12+
"files": [
13+
"lib",
14+
"src"
15+
],
716
"repository": "https://github.com/anoma/namada-sdkjs/",
817
"author": "Heliax Dev <info@heliax.dev>",
918
"license": "MIT",
@@ -69,9 +78,6 @@
6978
"typescript-transform-paths": "^3.4.7",
7079
"wasm-pack": "^0.13.0"
7180
},
72-
"files": [
73-
"dist/**/*.{js,ts,wasm}"
74-
],
7581
"publishConfig": {
7682
"registry": "https://registry.npmjs.org",
7783
"access": "public"

scripts/copy.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { execSync } from "child_process";
22

3+
const modules = ["esm", "cjs"];
34
const targets = ["web", "nodejs"];
45
const sdkBuilds = ["sdk", "sdk-multicore"];
56
const srcDir = "./src/wasm/target";
6-
const baseDir = "./dist/wasm/target";
7+
const baseDir = "./dist/lib";
78

8-
execSync(`rm -rf ${baseDir}`);
9-
10-
targets.forEach((target) => {
11-
sdkBuilds.forEach((build) => {
12-
execSync(`mkdir -p ${baseDir}/${target}/${build}`);
13-
execSync(
14-
`cp ${srcDir}/${target}/${build}/sdk_bg.wasm* ${baseDir}/${target}/${build}/`,
15-
);
9+
modules.forEach((mod) => {
10+
targets.forEach((target) => {
11+
sdkBuilds.forEach((build) => {
12+
// execSync(`rm ${baseDir}/${mod}/wasm/${target}/${build}`);
13+
execSync(`mkdir -p ${baseDir}/${mod}/wasm/target/${target}/${build}`);
14+
execSync(
15+
`cp -r ${srcDir}/${target}/${build}/* ${baseDir}/${mod}/wasm/target/${target}/${build}/`,
16+
);
17+
});
1618
});
1719
});

src/crypto/crypto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
Salt,
88
VecU8Pointer,
99
readVecU8Pointer,
10-
} from "wasm";
10+
} from "../wasm";
1111
import { Argon2Config, CryptoRecord, EncryptionParams, KdfType } from "./types";
1212

1313
/**

src/crypto/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Argon2Params as Argon2ParamsWasm, VecU8Pointer } from "wasm";
1+
import { Argon2Params as Argon2ParamsWasm, VecU8Pointer } from "../wasm";
22

33
export const Argon2Config = {
44
// Number of memory blocks in kibibytes

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ export type { Signing } from "./signing";
6565
export type { Tx } from "./tx";
6666

6767
// Export SDK initializer and types
68-
export { initSdk } from "wasm";
68+
export { initSdk } from "./wasm";
6969
export type {
7070
WasmTarget,
7171
SdkBuildOptions,
7272
SdkWasmOptions,
7373
InitProps,
74-
} from "wasm";
74+
} from "./wasm";

src/keys/keys.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {
99
ExtendedViewingKey,
1010
gen_payment_address,
1111
public_key_to_bech32,
12-
} from "wasm";
13-
import { Bip44Path, Zip32Path } from "types";
14-
import { makeBip44PathArray, makeSaplingPathArray } from "utils";
12+
} from "../wasm";
13+
import { Bip44Path, NAMADA_COIN_TYPE as coinType, Zip32Path } from "../types";
14+
import { makeBip44PathArray, makeSaplingPathArray } from "../utils";
1515
import {
1616
Address,
1717
DEFAULT_BIP44_PATH,
@@ -21,7 +21,6 @@ import {
2121
ShieldedKeys,
2222
TransparentKeys,
2323
} from "./types";
24-
import { NAMADA_COIN_TYPE as coinType } from "types";
2524

2625
/**
2726
* Namespace for key related functions

src/keys/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Bip44Path, Zip32Path } from "types";
1+
import { Bip44Path, Zip32Path } from "../types";
22

33
/**
44
* Address and public key type

src/ledger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import semver from "semver";
1414
import { makeBip44Path, makeSaplingPath } from "./utils";
1515

16-
import { NAMADA_COIN_TYPE as coinType } from "types";
16+
import { NAMADA_COIN_TYPE as coinType } from "./types";
1717

1818
export type LedgerAddressAndPublicKey = { address: string; publicKey: string };
1919
export type LedgerViewingKey = {

src/masp/masp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Sdk as SdkWasm } from "wasm";
1+
import { Sdk as SdkWasm } from "../wasm";
22

33
/**
44
* Class representing utilities related to MASP

src/masp/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ export {
22
ExtendedViewingKey,
33
ProofGenerationKey,
44
PseudoExtendedKey,
5-
} from "wasm";
5+
} from "../wasm";

src/mnemonic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
StringPointer,
44
readVecStringPointer,
55
readVecU8Pointer,
6-
} from "wasm";
6+
} from "./wasm";
77

88
export enum PhraseSize {
99
N12 = 12,

src/rpc/rpc.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
// @ts-nocheck
12
import { deserialize } from "@dao-xyz/borsh";
23
import {
34
DatedViewingKey as DatedViewingKeyWasm,
45
Query as QueryWasm,
56
Sdk as SdkWasm,
67
TransferToEthereum,
7-
} from "wasm";
8+
} from "../wasm";
89
import {
910
BroadcastTxError,
1011
DatedViewingKey,
1112
TxResponseMsgValue,
1213
TxResponseProps,
13-
} from "types";
14+
} from "../types";
1415

1516
import {
1617
Balance,

src/sdk.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Transport from "@ledgerhq/hw-transport";
2-
import { Query as QueryWasm, Sdk as SdkWasm } from "wasm";
2+
import { Query as QueryWasm, Sdk as SdkWasm } from "./wasm";
33
import packageJson from "../package.json";
44
import { Crypto } from "./crypto";
55
import { Keys } from "./keys";
@@ -10,7 +10,7 @@ import { Rpc } from "./rpc";
1010
import { Signing } from "./signing";
1111
import { Tx } from "./tx";
1212

13-
export { ProgressBarNames, SdkEvents } from "wasm";
13+
export { ProgressBarNames, SdkEvents } from "./wasm";
1414

1515
/**
1616
* API for interacting with Namada SDK
@@ -19,14 +19,14 @@ export class Sdk {
1919
/**
2020
* @param sdk - Instance of Sdk struct from wasm lib
2121
* @param query - Instance of Query struct from wasm lib
22-
* @param cryptoMemory - Memory accessor for crypto lib
22+
* @param memory - Memory accessor for wasm lib
2323
* @param url - RPC url
2424
* @param nativeToken - Address of chain's native token
2525
*/
2626
constructor(
2727
protected sdk: SdkWasm,
2828
protected query: QueryWasm,
29-
public readonly cryptoMemory: WebAssembly.Memory,
29+
public readonly memory: WebAssembly.Memory,
3030
public readonly url: string,
3131
public readonly nativeToken: string,
3232
) {}
@@ -69,15 +69,15 @@ export class Sdk {
6969
* @returns mnemonic-related functionality
7070
*/
7171
getMnemonic(): Mnemonic {
72-
return new Mnemonic(this.cryptoMemory);
72+
return new Mnemonic(this.memory);
7373
}
7474

7575
/**
7676
* Return initialized Keys class
7777
* @returns key-related functionality
7878
*/
7979
getKeys(): Keys {
80-
return new Keys(this.cryptoMemory);
80+
return new Keys(this.memory);
8181
}
8282

8383
/**
@@ -101,7 +101,7 @@ export class Sdk {
101101
* @returns Utilities for encrypting and decrypting data
102102
*/
103103
getCrypto(): Crypto {
104-
return new Crypto(this.cryptoMemory);
104+
return new Crypto(this.memory);
105105
}
106106

107107
/**

src/signing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Sdk as SdkWasm } from "wasm";
2-
import { Message, TxMsgValue, TxProps } from "types";
1+
import { Sdk as SdkWasm } from "./wasm";
2+
import { Message, TxMsgValue, TxProps } from "./types";
33

44
type Signature = [string, string];
55

src/tests/crypto.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import { initSdk } from "./initSdk";
44
const PASSWORD = "super secure";
55

66
describe("Crypto", () => {
7-
it("should encrypt and decrypt data successfully", () => {
8-
const { crypto } = initSdk();
7+
it("should encrypt and decrypt data successfully", async () => {
8+
const { crypto } = await initSdk();
99
const plainText = "This is sensitive data";
1010
const cryptoRecord = crypto.encrypt(plainText, PASSWORD);
1111
const decryptedData = crypto.decrypt(cryptoRecord, PASSWORD);
1212

1313
expect(decryptedData).toBe(plainText);
1414
});
1515

16-
it("should decrypt stored crypto record successfully", () => {
17-
const { crypto } = initSdk();
16+
it("should decrypt stored crypto record successfully", async () => {
17+
const { crypto } = await initSdk();
1818
const plainText = "This is sensitive data";
1919
const decryptedData = crypto.decrypt(CRYPTO_RECORD, PASSWORD);
2020
expect(decryptedData).toBe(plainText);

src/tests/initSdk.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import { Query as QueryWasm, Sdk as SdkWasm } from "wasm";
2-
import init from "../initNode";
31
import { Sdk } from "../sdk";
2+
import { initSdk as init } from "../wasm";
3+
44
import { NATIVE_TOKEN as nativeToken, RPC_URL as rpcUrl } from "./data";
55

66
// Simplified wrapper to handle initializing SDK for tests
7-
export const initSdk = (): Sdk => {
8-
const { cryptoMemory } = init();
9-
const query = new QueryWasm(rpcUrl);
10-
const sdk = new SdkWasm(rpcUrl, nativeToken, "some db name");
11-
12-
return new Sdk(sdk, query, cryptoMemory, rpcUrl, nativeToken);
7+
export const initSdk = async (): Promise<Sdk> => {
8+
return await init({ rpcUrl, token: nativeToken });
139
};

src/tests/keys.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
import { initSdk } from "./initSdk";
1010

1111
describe("Keys", () => {
12-
it("should derive transparent keys from mnemonic phrase", () => {
13-
const { keys } = initSdk();
12+
it("should derive transparent keys from mnemonic phrase", async () => {
13+
const { keys } = await initSdk();
1414

1515
const { address, publicKey, privateKey } =
1616
keys.deriveFromMnemonic(mnemonic1);
@@ -20,14 +20,14 @@ describe("Keys", () => {
2020
expect(privateKey).toBe(account1.privateKey);
2121
});
2222

23-
it("should derive shielded keys from seed", () => {
24-
const { keys, mnemonic } = initSdk();
23+
it("should derive shielded keys from seed", async () => {
24+
const { keys, mnemonic } = await initSdk();
2525
const seed = mnemonic.toSeed(mnemonic1);
2626
const seed2 = mnemonic.toSeed(mnemonic2);
2727

2828
const { address, viewingKey, spendingKey } = keys.deriveShieldedFromSeed(
2929
seed,
30-
shieldedAccount1.path
30+
shieldedAccount1.path,
3131
);
3232

3333
expect(address).toBe(shieldedAccount1.paymentAddress);
@@ -45,8 +45,8 @@ describe("Keys", () => {
4545
expect(spendingKey2).toBe(shieldedAccount2.spendingKey);
4646
});
4747

48-
it("should derive keys from seed", () => {
49-
const { keys, mnemonic } = initSdk();
48+
it("should derive keys from seed", async () => {
49+
const { keys, mnemonic } = await initSdk();
5050
// Generate a seed from a mnemonic phrase
5151
const seed = mnemonic.toSeed(mnemonic1);
5252
// Derive account from that seed
@@ -57,8 +57,8 @@ describe("Keys", () => {
5757
expect(privateKey).toBe(account1.privateKey);
5858
});
5959

60-
it("should generate valid sequential payment addresses", () => {
61-
const { keys } = initSdk();
60+
it("should generate valid sequential payment addresses", async () => {
61+
const { keys } = await initSdk();
6262
const { viewingKey } = shieldedAccount1;
6363

6464
// Test starting with undefined diversifierIndex, as this will be undefined
@@ -74,7 +74,7 @@ describe("Keys", () => {
7474
while (paymentAddresses.length < 100) {
7575
const { diversifierIndex, address } = keys.genPaymentAddress(
7676
viewingKey,
77-
currentIndex + 1
77+
currentIndex + 1,
7878
);
7979
paymentAddresses.push([diversifierIndex, address]);
8080
currentIndex = diversifierIndex;

src/tests/mnemonic.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import { initSdk } from "./initSdk";
44

55
describe("mnemonic", () => {
66
it("should generate a 12 word mnemonic phrase", async () => {
7-
const { mnemonic } = initSdk();
7+
const { mnemonic } = await initSdk();
88
const words = mnemonic.generate();
99
expect(words.length).toEqual(12);
1010
});
1111

1212
it("should generate a 24 word mnemonic phrase", async () => {
13-
const { mnemonic } = initSdk();
13+
const { mnemonic } = await initSdk();
1414
const words = mnemonic.generate(PhraseSize.N24);
1515
expect(words.length).toEqual(24);
1616
});
1717

18-
it("should generate a seed from mnemonic", () => {
19-
const { mnemonic } = initSdk();
18+
it("should generate a seed from mnemonic", async () => {
19+
const { mnemonic } = await initSdk();
2020
const seed = mnemonic.toSeed(mnemonic1);
2121
expect(seed).toBeDefined();
2222
expect(seed.length).toEqual(64);

src/tests/sdk.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { Tx } from "tx";
77
import { initSdk } from "./initSdk";
88

99
describe("Sdk", () => {
10-
it("should initialize Sdk with all sub-components", () => {
11-
const { tx, keys, mnemonic, rpc, masp, signing } = initSdk();
10+
it("should initialize Sdk with all sub-components", async () => {
11+
const { tx, keys, mnemonic, rpc, masp, signing } = await initSdk();
1212
expect(tx).toBeInstanceOf(Tx);
1313
expect(keys).toBeInstanceOf(Keys);
1414
expect(mnemonic).toBeInstanceOf(Mnemonic);

0 commit comments

Comments
 (0)