@spirobel/monero-wallet-api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # @spirobel/monero-wallet-api
2
+
3
+ To install dependencies:
4
+
5
+ ```bash
6
+ cd typescript || cd ../typescript
7
+ bun install
8
+ ```
9
+
10
+ To build:
11
+
12
+ ```bash
13
+ cd typescript || cd ../typescript
14
+ bun build
15
+ ```
16
+
17
+ rust release build
18
+
19
+ ```bash
20
+ cd rust || cd ../rust
21
+ cargo wasi build --target wasm32-wasip1 --release --lib
22
+ ```
23
+
24
+ ## reproducible build with pinned cargo + rust + cargo wasi
25
+
26
+ make the image
27
+
28
+ ```bash
29
+ cd rust || cd ../rust
30
+ docker build -t monero-wallet-api-build .
31
+ ```
32
+
33
+ build the library -> find the result in target/wasm32-wasip1/release
34
+
35
+ ```bash
36
+ docker run -v $(pwd):/app -it monero-wallet-api-build
37
+ ```
38
+
39
+ ```bash
40
+ cd typescript || cd ../typescript
41
+ bun run build
42
+ bun run inlinesum
43
+ ```
44
+
45
+ if the content of the checksum.txt file stays the same, the build was reproduced.
46
+
47
+ to verify that the wasm file distributed on npm matches the checksum,
48
+ add the npm package as a dependency to a project and compare the sha256sum output with the checksum.txt file in the git repo.
49
+
50
+ ```bash
51
+ cd /tmp
52
+ bun init
53
+ bun add @spirobel/monero-wallet-api
54
+ cat node_modules/@spirobel/monero-wallet-api/dist/wasmFile.js | sha256sum
55
+ ```
package/dist/api.d.ts ADDED
@@ -0,0 +1,55 @@
1
+ import { type ScanResult, type ErrorResponse, type GetBlocksBinMetaCallback, type GetBlocksBinRequest } from "./node-interaction/binaryEndpoints";
2
+ import { WasmProcessor } from "./wasm-processing/wasmProcessor";
3
+ export * from "./node-interaction/binaryEndpoints";
4
+ export * from "./node-interaction/jsonEndpoints";
5
+ export type ScanResultCallback = (result: ScanResult | ErrorResponse) => void;
6
+ /**
7
+ * This class is useful to interact with Moneros DaemonRpc binary requests in a convenient way.
8
+ * (similar to how you would interact with a REST api that gives you json back.)
9
+ * The wasm part will handle the creation of the binary requests and parse the responses and then parse them
10
+ * and return outputs that belong to the ViewPair.
11
+ * {@link https://docs.getmonero.org/rpc-library/monerod-rpc/#get_blocksbin}
12
+ */
13
+ export declare class ViewPair extends WasmProcessor {
14
+ static create(primary_address: string, secret_view_key: string, node_url?: string): Promise<ViewPair>;
15
+ /**
16
+ * This request helps making requests to the get_blocks.bin endpoint of the Monerod nodes.
17
+ * @link https://docs.getmonero.org/rpc-library/monerod-rpc/#get_blocksbin
18
+ * @param params params that will be turned into epee (moner lib that does binary serialization)
19
+ * @param metaCallBack contains meta information about the getBlocksbin call (new sync height = start_height param + number of blocks)
20
+ * @returns The difference to the same method on NodeUrl is: It returns {@link ScanResult} (outputs that belong to viewpair) and not just the blocks as json.
21
+ */
22
+ getBlocksBin(params: GetBlocksBinRequest, metaCallBack?: GetBlocksBinMetaCallback): Promise<ScanResult | ErrorResponse>;
23
+ /**
24
+ * This method will use getBlocks.bin from start height to daemon height.
25
+ * This is CPU bound work, so it should be executed in a seperate thread (worker).
26
+ * The scanner.ts worker in the standard-checkout dir shows how to keep scanning after the tip is reached.
27
+ * It also shows how the outputs are saved (note the unqiue requirement for the stealth_adress).
28
+ * @param start_height the height to start syncing from.
29
+ * @param callback this function will get the new outputs as they are found as a parameter
30
+ */
31
+ scan(start_height: number, callback: ScanResultCallback): Promise<void>;
32
+ /**
33
+ * This method makes an integrated Address for the Address of the Viewpair it was opened with.
34
+ * The network (mainnet, stagenet, testnet) is the same as the one of the Viewpairaddress.
35
+ * @param paymentId (u64 under the hood) you can use a random number or a primary key of an sqlite db to associate payments with customer sessions.
36
+ * @returns Adressstring
37
+ */
38
+ makeIntegratedAddress(paymentId: number): string;
39
+ }
40
+ /**
41
+ * This class is useful to interact with Moneros DaemonRpc binary requests in a convenient way.
42
+ * (similar to how you would interact with a REST api that gives you json back.)
43
+ * The wasm part will handle the creation of the binary requests and parse the responses and return them as json.
44
+ * {@link https://docs.getmonero.org/rpc-library/monerod-rpc/#get_blocksbin}
45
+ */
46
+ export declare class NodeUrl extends WasmProcessor {
47
+ static create(node_url?: string): Promise<NodeUrl>;
48
+ /**
49
+ * This request helps making requests to the get_blocks.bin endpoint of the Monerod nodes.
50
+ * @link https://docs.getmonero.org/rpc-library/monerod-rpc/#get_blocksbin
51
+ * @param params params that will be turned into epee (moner lib that does binary serialization)
52
+ * @returns after the request is made it will return epee serialized objects that are then parsed into json.
53
+ */
54
+ getBlocksBin(params: GetBlocksBinRequest): Promise<import("./api").GetBlocksBinResponse | ErrorResponse>;
55
+ }
package/dist/api.js ADDED
@@ -0,0 +1,103 @@
1
+ import { getBlocksBinJson, getBlocksBinScan, } from "./node-interaction/binaryEndpoints";
2
+ import { TinyWASI } from "./wasm-processing/wasi";
3
+ import { WasmProcessor } from "./wasm-processing/wasmProcessor";
4
+ export * from "./node-interaction/binaryEndpoints";
5
+ export * from "./node-interaction/jsonEndpoints";
6
+ /**
7
+ * This class is useful to interact with Moneros DaemonRpc binary requests in a convenient way.
8
+ * (similar to how you would interact with a REST api that gives you json back.)
9
+ * The wasm part will handle the creation of the binary requests and parse the responses and then parse them
10
+ * and return outputs that belong to the ViewPair.
11
+ * {@link https://docs.getmonero.org/rpc-library/monerod-rpc/#get_blocksbin}
12
+ */
13
+ export class ViewPair extends WasmProcessor {
14
+ static async create(primary_address, secret_view_key, node_url) {
15
+ const viewPair = new ViewPair(new TinyWASI(), node_url || "http://localhost:38081");
16
+ const tinywasi = await viewPair.initWasmModule();
17
+ viewPair.writeToWasmMemory = (ptr, len) => {
18
+ viewPair.writeString(ptr, len, primary_address);
19
+ viewPair.writeToWasmMemory = (ptr, len) => {
20
+ viewPair.writeString(ptr, len, secret_view_key);
21
+ };
22
+ };
23
+ //@ts-ignore
24
+ tinywasi.instance.exports.init_viewpair(primary_address.length, secret_view_key.length);
25
+ return viewPair;
26
+ }
27
+ /**
28
+ * This request helps making requests to the get_blocks.bin endpoint of the Monerod nodes.
29
+ * @link https://docs.getmonero.org/rpc-library/monerod-rpc/#get_blocksbin
30
+ * @param params params that will be turned into epee (moner lib that does binary serialization)
31
+ * @param metaCallBack contains meta information about the getBlocksbin call (new sync height = start_height param + number of blocks)
32
+ * @returns The difference to the same method on NodeUrl is: It returns {@link ScanResult} (outputs that belong to viewpair) and not just the blocks as json.
33
+ */
34
+ getBlocksBin(params, metaCallBack) {
35
+ return getBlocksBinScan(this, params, metaCallBack);
36
+ }
37
+ /**
38
+ * This method will use getBlocks.bin from start height to daemon height.
39
+ * This is CPU bound work, so it should be executed in a seperate thread (worker).
40
+ * The scanner.ts worker in the standard-checkout dir shows how to keep scanning after the tip is reached.
41
+ * It also shows how the outputs are saved (note the unqiue requirement for the stealth_adress).
42
+ * @param start_height the height to start syncing from.
43
+ * @param callback this function will get the new outputs as they are found as a parameter
44
+ */
45
+ async scan(start_height, callback) {
46
+ let latest_meta = {
47
+ new_height: start_height,
48
+ daemon_height: start_height + 1,
49
+ status: "",
50
+ };
51
+ while (latest_meta.new_height < latest_meta.daemon_height) {
52
+ const res = await this.getBlocksBin({
53
+ start_height: latest_meta.new_height - 1,
54
+ }, (meta) => {
55
+ latest_meta = meta;
56
+ });
57
+ callback(res);
58
+ }
59
+ }
60
+ /**
61
+ * This method makes an integrated Address for the Address of the Viewpair it was opened with.
62
+ * The network (mainnet, stagenet, testnet) is the same as the one of the Viewpairaddress.
63
+ * @param paymentId (u64 under the hood) you can use a random number or a primary key of an sqlite db to associate payments with customer sessions.
64
+ * @returns Adressstring
65
+ */
66
+ makeIntegratedAddress(paymentId) {
67
+ let address = "";
68
+ this.readFromWasmMemory = (ptr, len) => {
69
+ address = this.readString(ptr, len);
70
+ };
71
+ //@ts-ignore
72
+ this.tinywasi.instance.exports.make_integrated_address(BigInt(paymentId));
73
+ return address;
74
+ }
75
+ }
76
+ /**
77
+ * This class is useful to interact with Moneros DaemonRpc binary requests in a convenient way.
78
+ * (similar to how you would interact with a REST api that gives you json back.)
79
+ * The wasm part will handle the creation of the binary requests and parse the responses and return them as json.
80
+ * {@link https://docs.getmonero.org/rpc-library/monerod-rpc/#get_blocksbin}
81
+ */
82
+ export class NodeUrl extends WasmProcessor {
83
+ static async create(node_url) {
84
+ const nodeUrl = new NodeUrl(new TinyWASI(), node_url || "http://localhost:38081");
85
+ await nodeUrl.initWasmModule();
86
+ return nodeUrl;
87
+ }
88
+ /**
89
+ * This request helps making requests to the get_blocks.bin endpoint of the Monerod nodes.
90
+ * @link https://docs.getmonero.org/rpc-library/monerod-rpc/#get_blocksbin
91
+ * @param params params that will be turned into epee (moner lib that does binary serialization)
92
+ * @returns after the request is made it will return epee serialized objects that are then parsed into json.
93
+ */
94
+ getBlocksBin(params) {
95
+ return getBlocksBinJson(this, params);
96
+ }
97
+ }
98
+ // const nodeurl = await NodeUrl.create("http://stagenet.community.rino.io:38081");
99
+ // nodeurl.getBlocksBin({ start_height: 1731707 });
100
+ // const viewpair = await ViewPair.create(
101
+ // "5B5ieVKGSyfAyh68X6AFB48Gnx9diT8jPbWN6UcZHJUZVQSLRhaaHuHQz3dGuxxZDXPYgCXzrkerK3m6Q1tHoougR7VYyd9",
102
+ // "10b9885324933ee6055b001a3ee4b70f6832b866db389ad023b51fe7e2e7ca01"
103
+ // );
package/dist/cli.js ADDED
@@ -0,0 +1,2 @@
1
+ // cli.tsx
2
+ console.log(`Your random number is ${0.7962579739535381}`);
@@ -0,0 +1 @@
1
+ export declare function init(): Promise<void>;
package/dist/index.js ADDED
@@ -0,0 +1,23 @@
1
+ import * as fs from "fs";
2
+ import { TinyWASI } from "./wasi";
3
+ const source = fs.readFileSync("../rust/target/wasm32-wasip1/release/monero_wallet_api.wasm");
4
+ const typedArray = new Uint8Array(source);
5
+ let ffiRegister = (ptr, len) => { };
6
+ export async function init() {
7
+ const tinywasi = new TinyWASI();
8
+ const imports = {
9
+ env: {
10
+ input: (ptr, len) => {
11
+ console.log("input", ptr, len);
12
+ ffiRegister(ptr, len);
13
+ },
14
+ },
15
+ ...tinywasi.imports,
16
+ };
17
+ const { module, instance } = await WebAssembly.instantiate(typedArray, imports);
18
+ tinywasi.initialize(instance);
19
+ console.log(instance.exports);
20
+ //@ts-ignore
21
+ instance.exports.init_viewpair(1, 3);
22
+ }
23
+ init();
@@ -0,0 +1,60 @@
1
+ import type { WasmProcessor } from "../wasm-processing/wasmProcessor";
2
+ export type GetBlocksBinRequest = {
3
+ requested_info?: "BLOCKS_ONLY" | "BLOCKS_AND_POOL" | "POOL_ONLY";
4
+ start_height: number;
5
+ prune?: boolean;
6
+ no_miner_tx?: boolean;
7
+ pool_info_since?: number;
8
+ };
9
+ export type PoolInfo = {};
10
+ export type Transaction = {};
11
+ export type Block = {
12
+ pruned: boolean;
13
+ block: number[];
14
+ block_weight: number;
15
+ txs: "None" | Transaction[];
16
+ };
17
+ export type OutputIndex = {
18
+ indices: {
19
+ indices: number[];
20
+ }[];
21
+ };
22
+ export type GetBlocksBinResponse = {
23
+ status: "OK";
24
+ untrusted: false;
25
+ credits: number;
26
+ top_hash: string;
27
+ blocks: Block[];
28
+ start_height: number;
29
+ current_height: number;
30
+ output_indices: OutputIndex[];
31
+ daemon_time: number;
32
+ pool_info: "None" | PoolInfo;
33
+ new_height: number;
34
+ };
35
+ export type Status = "OK" | "BUSY" | "NOT MINING" | "PAYMENT REQUIRED" | "Failed." | string;
36
+ export type GetBlocksResultMeta = {
37
+ new_height: number;
38
+ daemon_height: number;
39
+ status: Status;
40
+ };
41
+ export type Output = {
42
+ amount: number;
43
+ block_height: number;
44
+ index_in_transaction: number;
45
+ index_on_blockchain: number;
46
+ payment_id: number;
47
+ stealth_address: string;
48
+ tx_hash: string;
49
+ };
50
+ export type ScanResult = {
51
+ outputs: Output[];
52
+ new_height: number;
53
+ };
54
+ export type ErrorResponse = {
55
+ error: string;
56
+ };
57
+ export type GetBlocksBinMetaCallback = (meta: GetBlocksResultMeta) => void;
58
+ export declare function getBlocksBinScan<T extends WasmProcessor>(processor: T, params: GetBlocksBinRequest, metaCallBack?: GetBlocksBinMetaCallback): Promise<ScanResult | ErrorResponse>;
59
+ export declare function getBlocksBinJson<T extends WasmProcessor>(processor: T, params: GetBlocksBinRequest): Promise<GetBlocksBinResponse | ErrorResponse>;
60
+ export declare function binaryFetchRequest(url: string, body: Uint8Array): Promise<Uint8Array>;
@@ -0,0 +1,109 @@
1
+ export async function getBlocksBinScan(processor, params, metaCallBack) {
2
+ // https://github.com/monero-project/monero/blob/941ecefab21db382e88065c16659864cb8e763ae/src/rpc/core_rpc_server_commands_defs.h#L178
3
+ // enum REQUESTED_INFO
4
+ // {
5
+ // BLOCKS_ONLY = 0,
6
+ // BLOCKS_AND_POOL = 1,
7
+ // POOL_ONLY = 2
8
+ // };
9
+ let requested_info = 0;
10
+ if (params.requested_info === "BLOCKS_AND_POOL") {
11
+ requested_info = 1;
12
+ }
13
+ else if (params.requested_info === "POOL_ONLY") {
14
+ requested_info = 2;
15
+ }
16
+ let prune_num = 1;
17
+ if (params.prune === false) {
18
+ prune_num = 0;
19
+ }
20
+ let no_miner_tx_num = 0;
21
+ if (params.no_miner_tx) {
22
+ no_miner_tx_num = 1;
23
+ }
24
+ let getBlocksArray;
25
+ processor.readFromWasmMemory = (ptr, len) => {
26
+ getBlocksArray = processor.readArray(ptr, len);
27
+ };
28
+ //@ts-ignore
29
+ processor.tinywasi.instance.exports.build_getblocksbin_request(requested_info, BigInt(params.start_height), prune_num, no_miner_tx_num, BigInt(params.pool_info_since || 0));
30
+ const getBlocksBinResponseBuffer = await binaryFetchRequest(processor.node_url + "/getblocks.bin", getBlocksArray // written in build_getblocksbin_request call to readFromWasmMemory
31
+ );
32
+ processor.writeToWasmMemory = (ptr, len) => {
33
+ processor.writeArray(ptr, len, getBlocksBinResponseBuffer);
34
+ };
35
+ let resultMeta;
36
+ let result;
37
+ processor.readFromWasmMemory = (ptr, len) => {
38
+ resultMeta = JSON.parse(processor.readString(ptr, len));
39
+ if (metaCallBack)
40
+ metaCallBack(resultMeta);
41
+ processor.readFromWasmMemory = (ptr, len) => {
42
+ result = JSON.parse(processor.readString(ptr, len));
43
+ if (!("error" in result)) {
44
+ result.new_height = resultMeta.new_height;
45
+ }
46
+ };
47
+ };
48
+ //@ts-ignore
49
+ processor.tinywasi.instance.exports.scan_blocks_with_get_blocks_bin(getBlocksBinResponseBuffer.length);
50
+ return result; //result written in scan_blocks_with_get_blocks_bin
51
+ }
52
+ export async function getBlocksBinJson(processor, params) {
53
+ // https://github.com/monero-project/monero/blob/941ecefab21db382e88065c16659864cb8e763ae/src/rpc/core_rpc_server_commands_defs.h#L178
54
+ // enum REQUESTED_INFO
55
+ // {
56
+ // BLOCKS_ONLY = 0,
57
+ // BLOCKS_AND_POOL = 1,
58
+ // POOL_ONLY = 2
59
+ // };
60
+ let requested_info = 0;
61
+ if (params.requested_info === "BLOCKS_AND_POOL") {
62
+ requested_info = 1;
63
+ }
64
+ else if (params.requested_info === "POOL_ONLY") {
65
+ requested_info = 2;
66
+ }
67
+ let prune_num = 1;
68
+ if (params.prune === false) {
69
+ prune_num = 0;
70
+ }
71
+ let no_miner_tx_num = 0;
72
+ if (params.no_miner_tx) {
73
+ no_miner_tx_num = 1;
74
+ }
75
+ let getBlocksArray;
76
+ processor.readFromWasmMemory = (ptr, len) => {
77
+ getBlocksArray = processor.readArray(ptr, len);
78
+ };
79
+ //@ts-ignore
80
+ processor.tinywasi.instance.exports.build_getblocksbin_request(requested_info, BigInt(params.start_height), prune_num, no_miner_tx_num, BigInt(params.pool_info_since || 0));
81
+ const getBlocksBinResponseBuffer = await binaryFetchRequest(processor.node_url + "/getblocks.bin", getBlocksArray // written in build_getblocksbin_request call to readFromWasmMemory
82
+ );
83
+ processor.writeToWasmMemory = (ptr, len) => {
84
+ processor.writeArray(ptr, len, getBlocksBinResponseBuffer);
85
+ };
86
+ let resultMeta;
87
+ let result;
88
+ processor.readFromWasmMemory = (ptr, len) => {
89
+ resultMeta = JSON.parse(processor.readString(ptr, len));
90
+ processor.readFromWasmMemory = (ptr, len) => {
91
+ result = JSON.parse(processor.readString(ptr, len));
92
+ if (!("error" in result)) {
93
+ result.new_height = resultMeta.new_height;
94
+ }
95
+ };
96
+ };
97
+ //@ts-ignore
98
+ processor.tinywasi.instance.exports.convert_get_blocks_bin_response_to_json(getBlocksBinResponseBuffer.length);
99
+ return result; //result written in convert_get_blocks_bin_response_to_json
100
+ }
101
+ export async function binaryFetchRequest(url, body) {
102
+ const response = await fetch(url, {
103
+ body,
104
+ method: "POST",
105
+ })
106
+ .then((result) => result.blob())
107
+ .then((blob) => blob.arrayBuffer());
108
+ return new Uint8Array(response);
109
+ }