saito-js 0.0.2

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.
@@ -0,0 +1,13 @@
1
+ {
2
+ "presets": [
3
+ [
4
+ "@babel/preset-env",
5
+ {
6
+ "targets": {
7
+ "node": "current"
8
+ }
9
+ }
10
+ ],
11
+ "@babel/preset-typescript"
12
+ ]
13
+ }
package/configs.ts ADDED
@@ -0,0 +1,26 @@
1
+ type Configs = {
2
+ server: {
3
+ host: string,
4
+ port: number,
5
+ protocol: string,
6
+ endpoint: {
7
+ host: string,
8
+ port: number,
9
+ protocol: string,
10
+ },
11
+ verification_threads: number,
12
+ channel_size: number,
13
+ stat_timer_in_ms: number,
14
+ reconnection_wait_time: number,
15
+ thread_sleep_time_in_ms: number,
16
+ block_fetch_batch_size: number,
17
+ },
18
+ peers: {
19
+ host: string,
20
+ port: number,
21
+ protocol: string,
22
+ synctype: string
23
+ }[]
24
+ };
25
+
26
+ export default Configs;
package/index.node.ts ADDED
@@ -0,0 +1,40 @@
1
+ import Saito from './saito';
2
+ import SharedMethods from "./shared_methods";
3
+ import Configs from "./configs";
4
+ import Transaction from "./lib/transaction";
5
+ import Slip from "./lib/slip";
6
+ import Block from "./lib/block";
7
+ import Peer from "./lib/peer";
8
+ import Factory from "./lib/factory";
9
+
10
+ let cr = require('crypto');
11
+ globalThis.crypto = cr.webcrypto;
12
+
13
+
14
+ /**
15
+ *
16
+ * @param configs
17
+ * @param sharedMethods
18
+ */
19
+ export async function initialize(configs: Configs, sharedMethods: SharedMethods, factory: Factory) {
20
+ if (Saito.getLibInstance()) {
21
+ console.error("saito already initialized");
22
+ return;
23
+ }
24
+ console.log("initializing saito-js");
25
+ let saito = await import("saito-wasm/dist/server");
26
+ console.log("wasm lib loaded");
27
+
28
+ let s = await saito.default;
29
+
30
+ Saito.setLibInstance(s);
31
+
32
+ Transaction.Type = s.WasmTransaction;
33
+ Slip.Type = s.WasmSlip;
34
+ Block.Type = s.WasmBlock;
35
+ Peer.Type = s.WasmPeer;
36
+
37
+ return Saito.initialize(configs, sharedMethods, factory);
38
+ }
39
+
40
+ export default Saito;
package/index.web.ts ADDED
@@ -0,0 +1,41 @@
1
+ import Saito from './saito';
2
+ import SharedMethods from "./shared_methods";
3
+ import Configs from "./configs";
4
+ import Transaction from "./lib/transaction";
5
+ import Slip from "./lib/slip";
6
+ import Block from "./lib/block";
7
+ import Peer from "./lib/peer";
8
+ import Factory from "./lib/factory";
9
+
10
+ /**
11
+ *
12
+ * @param configs
13
+ * @param sharedMethods
14
+ */
15
+ export async function initialize(configs: Configs, sharedMethods: SharedMethods, factory: Factory) {
16
+ if (Saito.getLibInstance()) {
17
+ console.error("saito already initialized");
18
+ return;
19
+ }
20
+ console.log("initializing saito-js");
21
+
22
+ return import("saito-wasm/dist/browser")
23
+ .then((s: any) => {
24
+ return s.default;
25
+ })
26
+ .then((s) => {
27
+ Saito.setLibInstance(s);
28
+ return s.default()
29
+ .then(() => {
30
+
31
+ Transaction.Type = s.WasmTransaction;
32
+ Slip.Type = s.WasmSlip;
33
+ Block.Type = s.WasmBlock;
34
+ Peer.Type = s.WasmPeer;
35
+ return Saito.initialize(configs, sharedMethods, factory);
36
+ });
37
+ });
38
+ }
39
+
40
+
41
+ export default Saito;
package/lib/block.ts ADDED
@@ -0,0 +1,42 @@
1
+ import type {WasmBlock} from 'saito-wasm/dist/types/pkg/node/index_bg';
2
+ import Transaction from "./transaction";
3
+ import Saito from "../saito";
4
+
5
+ export enum BlockType {
6
+ Ghost = 0,
7
+ Header = 1,
8
+ Pruned = 2,
9
+ Full = 3,
10
+ }
11
+
12
+
13
+ export default class Block {
14
+ protected block: WasmBlock;
15
+ public static Type: any;
16
+
17
+ constructor(block?: WasmBlock) {
18
+ if (block) {
19
+ this.block = block;
20
+ } else {
21
+ this.block = Block.Type();
22
+ }
23
+ }
24
+
25
+ public get transactions(): Array<Transaction> {
26
+ return this.block.transactions.map(tx => {
27
+ return Saito.getInstance().factory.createTransaction(tx);
28
+ });
29
+ }
30
+
31
+ public get id(): bigint {
32
+ return this.block.id;
33
+ }
34
+
35
+ public get hash(): string {
36
+ return this.block.hash;
37
+ }
38
+
39
+ public serialize(): Uint8Array {
40
+ return this.block.serialize();
41
+ }
42
+ }
@@ -0,0 +1,72 @@
1
+ import SharedMethods from "../../shared_methods";
2
+ import Saito from "../../saito";
3
+
4
+ export default class CustomSharedMethods implements SharedMethods {
5
+ processApiCall(buffer: Uint8Array, msgIndex: number, peerIndex: bigint): Promise<void> {
6
+ throw new Error("Method not implemented.");
7
+ }
8
+
9
+ connectToPeer(peerData: any): void {
10
+ throw new Error("Method not implemented.");
11
+ }
12
+
13
+ disconnectFromPeer(peerIndex: bigint): void {
14
+ throw new Error("Method not implemented.");
15
+ }
16
+
17
+ fetchBlockFromPeer(url: string): Promise<Uint8Array> {
18
+ throw new Error("Method not implemented.");
19
+ }
20
+
21
+ isExistingFile(key: string): boolean {
22
+ throw new Error("Method not implemented.");
23
+ }
24
+
25
+ loadBlockFileList(): Array<string> {
26
+ throw new Error("Method not implemented.");
27
+ }
28
+
29
+
30
+ processApiError(buffer: Uint8Array, msgIndex: number, peerIndex: bigint): void {
31
+ let promise = Saito.getInstance().promises.get(msgIndex);
32
+ if (promise) {
33
+ promise.reject(buffer);
34
+ } else {
35
+ console.error("callback not found for callback index : " + msgIndex + " from peer : " + peerIndex);
36
+ }
37
+ }
38
+
39
+ processApiSuccess(buffer: Uint8Array, msgIndex: number, peerIndex: bigint): void {
40
+ let promise = Saito.getInstance().promises.get(msgIndex);
41
+ if (promise) {
42
+ promise.resolve(buffer);
43
+ } else {
44
+ console.error("callback not found for callback index : " + msgIndex + " from peer : " + peerIndex);
45
+ }
46
+ }
47
+
48
+ readValue(key: string): Uint8Array | null {
49
+ throw new Error("Method not implemented.");
50
+ }
51
+
52
+ removeValue(key: string): void {
53
+ throw new Error("Method not implemented.");
54
+ }
55
+
56
+ sendMessage(peerIndex: bigint, buffer: Uint8Array): void {
57
+ throw new Error("Method not implemented.");
58
+ }
59
+
60
+ sendMessageToAll(buffer: Uint8Array, exceptions: Array<bigint>): void {
61
+ throw new Error("Method not implemented.");
62
+ }
63
+
64
+ writeValue(key: string, value: Uint8Array): void {
65
+ throw new Error("Method not implemented.");
66
+ }
67
+
68
+ sendInterfaceEvent(event: String, peerIndex: bigint): void {
69
+ throw new Error("Method not implemented.");
70
+ }
71
+
72
+ }
@@ -0,0 +1,122 @@
1
+ import Saito from "../../saito";
2
+ import CustomSharedMethods from "./shared_methods.custom";
3
+
4
+ export default class WebSharedMethods extends CustomSharedMethods {
5
+ connectToPeer(peerData: any): void {
6
+ let protocol = "ws";
7
+ if (peerData.protocol === "https") {
8
+ protocol = "wss";
9
+ }
10
+ let url = protocol + "://" + peerData.host + ":" + peerData.port + "/wsopen";
11
+
12
+ try {
13
+ console.log("connecting to " + url + "....");
14
+ let socket = new WebSocket(url);
15
+ socket.binaryType = "arraybuffer";
16
+ let index = Saito.getInstance().addNewSocket(socket);
17
+
18
+ socket.onmessage = (event: MessageEvent) => {
19
+ // console.log("buffer : ", event.data);
20
+ Saito.getLibInstance().process_msg_buffer_from_peer(new Uint8Array(event.data), index);
21
+ };
22
+
23
+ socket.onopen = () => {
24
+ Saito.getLibInstance().process_new_peer(index, peerData);
25
+ };
26
+ socket.onclose = () => {
27
+ Saito.getLibInstance().process_peer_disconnection(index);
28
+ };
29
+
30
+ console.log("connected to : " + url + " with peer index : " + index);
31
+ } catch (e) {
32
+ console.error(e);
33
+ }
34
+ }
35
+
36
+ disconnectFromPeer(peerIndex: bigint): void {
37
+ Saito.getInstance().removeSocket(peerIndex);
38
+ }
39
+
40
+ fetchBlockFromPeer(url: string): Promise<Uint8Array> {
41
+ return fetch(url)
42
+ .then((res: any) => {
43
+ return res.arrayBuffer();
44
+ })
45
+ .then((buffer: ArrayBuffer) => {
46
+ return new Uint8Array(buffer);
47
+ });
48
+ }
49
+
50
+ isExistingFile(key: string): boolean {
51
+ try {
52
+ return !!localStorage.getItem(key);
53
+ } catch (error) {
54
+ console.error(error);
55
+ return false;
56
+ }
57
+ }
58
+
59
+ loadBlockFileList(): Array<string> {
60
+ try {
61
+ // console.log("loading block file list...");
62
+ // let files = Object.keys(localStorage);
63
+ // console.log("files : ", files);
64
+ // return files;
65
+ return [];
66
+ } catch (e) {
67
+ console.error(e);
68
+ return [];
69
+ }
70
+ }
71
+
72
+ readValue(key: string): Uint8Array | null {
73
+ try {
74
+ let data = localStorage.getItem(key);
75
+ if (!data) {
76
+ console.log("item not found for key : " + key);
77
+ return null;
78
+ }
79
+ let buffer = Buffer.from(data, "base64");
80
+ return new Uint8Array(buffer);
81
+ } catch (error) {
82
+ console.error(error);
83
+ return null;
84
+ }
85
+ }
86
+
87
+ removeValue(key: string): void {
88
+ try {
89
+ localStorage.removeItem(key);
90
+ } catch (e) {
91
+ console.error(e);
92
+ }
93
+ }
94
+
95
+ sendMessage(peerIndex: bigint, buffer: Uint8Array): void {
96
+ console.debug("sending message to peer : " + peerIndex);
97
+ let socket = Saito.getInstance().getSocket(peerIndex);
98
+ socket.send(buffer);
99
+ }
100
+
101
+ sendMessageToAll(buffer: Uint8Array, exceptions: Array<bigint>): void {
102
+ console.debug("sending message to all");
103
+ Saito.getInstance().sockets.forEach((socket, key) => {
104
+ if (exceptions.includes(key)) {
105
+ return;
106
+ }
107
+ socket.send(buffer);
108
+ });
109
+ }
110
+
111
+ writeValue(key: string, value: Uint8Array): void {
112
+ try {
113
+ localStorage.setItem(key, Buffer.from(value).toString("base64"));
114
+ } catch (error) {
115
+ console.error(error);
116
+ }
117
+ }
118
+
119
+ sendInterfaceEvent(event: String, peerIndex: bigint) {
120
+ throw new Error("Method not implemented.");
121
+ }
122
+ }
package/lib/factory.ts ADDED
@@ -0,0 +1,25 @@
1
+ import Block from "./block";
2
+ import Transaction from "./transaction";
3
+ import Slip from "./slip";
4
+ import Peer from "./peer";
5
+
6
+ export default class Factory {
7
+ constructor() {
8
+ }
9
+
10
+ public createBlock(data?: any): Block {
11
+ return new Block(data);
12
+ }
13
+
14
+ public createTransaction<T extends Transaction>(data?: any): Transaction {
15
+ return new Transaction(data);
16
+ }
17
+
18
+ public createSlip(data?: any): Slip {
19
+ return new Slip(data);
20
+ }
21
+
22
+ public createPeer(data?: any): Peer {
23
+ return new Peer(data);
24
+ }
25
+ }
package/lib/peer.ts ADDED
@@ -0,0 +1,50 @@
1
+ import type {WasmPeer} from "saito-wasm/dist/types/pkg/node/index_bg";
2
+
3
+ export default class Peer {
4
+ protected peer: WasmPeer;
5
+ public static Type: any;
6
+
7
+ constructor(peer?: WasmPeer, peerIndex?: bigint) {
8
+ if (peer) {
9
+ this.peer = peer;
10
+ } else {
11
+ this.peer = new Peer.Type(peerIndex);
12
+ }
13
+ }
14
+
15
+ public get publicKey(): string {
16
+ return this.peer.public_key;
17
+ }
18
+
19
+ // public set publicKey(key: string) {
20
+ // this.peer.public_key = key;
21
+ // }
22
+
23
+ public get keyList(): Array<string> {
24
+ return this.peer.key_list;
25
+ }
26
+
27
+ public get peerIndex(): bigint {
28
+ return this.peer.peer_index;
29
+ }
30
+
31
+ public get synctype(): string {
32
+ return this.peer.sync_type;
33
+ }
34
+
35
+ public get services(): string[] {
36
+ return this.peer.services;
37
+ }
38
+
39
+ public set services(s: string[]) {
40
+ this.peer.services = s;
41
+ }
42
+
43
+ public hasService(service: string): boolean {
44
+ return this.peer.has_service(service);
45
+ }
46
+
47
+ public isMainPeer(): boolean {
48
+ return this.peer.is_main_peer();
49
+ }
50
+ }
@@ -0,0 +1,18 @@
1
+ // import Factory from "./factory";
2
+ // import Block from "./block";
3
+ // import Transaction from "./transaction";
4
+ // import Slip from "./slip";
5
+ //
6
+ // export default class SaitoFactory extends Factory {
7
+ // public createBlock(data: any): Block {
8
+ // return new Block(data);
9
+ // }
10
+ //
11
+ // public createTransaction(data: any): Transaction {
12
+ // return new Transaction(data);
13
+ // }
14
+ //
15
+ // public createSlip(data: any): Slip {
16
+ // return new Slip(data);
17
+ // }
18
+ // }
package/lib/slip.ts ADDED
@@ -0,0 +1,119 @@
1
+ import type {WasmSlip} from 'saito-wasm/dist/types/pkg/node/index_bg';
2
+
3
+ export enum SlipType {
4
+ Normal = 0,
5
+ ATR = 1,
6
+ VipInput = 2,
7
+ VipOutput = 3,
8
+ MinerInput = 4,
9
+ MinerOutput = 5,
10
+ RouterInput = 6,
11
+ RouterOutput = 7,
12
+ Other = 8,
13
+ }
14
+
15
+ export default class Slip {
16
+ private slip: WasmSlip;
17
+ public static Type: any;
18
+
19
+ public constructor(slip?: WasmSlip, json?: any) {
20
+ if (!slip) {
21
+ this.slip = new Slip.Type();
22
+ } else {
23
+ this.slip = slip;
24
+ }
25
+ if (json) {
26
+ this.publicKey = json.publicKey;
27
+ this.type = json.type;
28
+ this.amount = json.amount;
29
+ this.index = json.index;
30
+ this.blockId = json.blockId;
31
+ this.txOrdinal = json.txOrdinal;
32
+ this.utxoKey = json.utxoKey;
33
+ }
34
+ }
35
+
36
+ public get wasmSlip(): WasmSlip {
37
+ return this.slip;
38
+ }
39
+
40
+ public get type(): SlipType {
41
+ return this.slip.slip_type as SlipType;
42
+ }
43
+
44
+ public set type(type: SlipType) {
45
+ this.slip.slip_type = type as number;
46
+ }
47
+
48
+ public get amount(): bigint {
49
+ return this.slip.amount;
50
+ }
51
+
52
+ public set amount(amount: bigint) {
53
+ this.slip.amount = amount;
54
+ }
55
+
56
+ public get publicKey(): string {
57
+ return this.slip.public_key;
58
+ }
59
+
60
+ public set publicKey(key: string) {
61
+ this.slip.public_key = key;
62
+ }
63
+
64
+ public set index(index: number) {
65
+ this.slip.slip_index = index;
66
+ }
67
+
68
+ public get index(): number {
69
+ return this.slip.slip_index;
70
+ }
71
+
72
+ public set blockId(id: bigint) {
73
+ this.slip.block_id = id;
74
+ }
75
+
76
+ public get blockId(): bigint {
77
+ return this.slip.block_id;
78
+ }
79
+
80
+ public set txOrdinal(ordinal: bigint) {
81
+ this.slip.tx_ordinal = ordinal;
82
+ }
83
+
84
+ public get txOrdinal(): bigint {
85
+ return this.slip.tx_ordinal;
86
+ }
87
+
88
+ public set utxoKey(key: string) {
89
+ this.slip.utxo_key = key;
90
+ }
91
+
92
+ public get utxoKey(): string {
93
+ return this.slip.utxo_key;
94
+ }
95
+
96
+ public toJson(): {
97
+ blockId: bigint;
98
+ utxoKey: string;
99
+ amount: bigint;
100
+ index: number;
101
+ publicKey: string;
102
+ txOrdinal: bigint;
103
+ type: any;
104
+ } {
105
+ return {
106
+ publicKey: this.publicKey,
107
+ type: this.type,
108
+ amount: this.amount,
109
+ index: this.index,
110
+ blockId: this.blockId,
111
+ txOrdinal: this.txOrdinal,
112
+ utxoKey: this.utxoKey,
113
+ };
114
+ }
115
+
116
+ public clone() {
117
+ return new Slip(undefined, this.toJson());
118
+ }
119
+ }