saito-js 0.0.5 → 0.0.6
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/package.json +1 -1
- package/.github/workflows/publish.yml +0 -50
- package/.prettierrc.json +0 -3
- package/babel.config.json +0 -13
- package/configs.ts +0 -26
- package/index.node.ts +0 -51
- package/index.web.ts +0 -47
- package/lib/block.ts +0 -42
- package/lib/blockchain.ts +0 -14
- package/lib/custom/custom_shared_methods.ts +0 -92
- package/lib/custom/shared_methods.web.ts +0 -123
- package/lib/factory.ts +0 -32
- package/lib/peer.ts +0 -50
- package/lib/saito_factory.ts +0 -18
- package/lib/slip.ts +0 -119
- package/lib/transaction.ts +0 -179
- package/lib/wallet.ts +0 -61
- package/saito.ts +0 -404
- package/shared_methods.ts +0 -40
- package/tests/index.test.ts +0 -35
- package/tsconfig.json +0 -111
- package/tsconfig.testing.json +0 -19
- package/webpack.config.js +0 -136
- package/webpack.prod.config.js +0 -136
- /package/{dist/browser → browser}/index.js +0 -0
- /package/{dist/configs.d.ts → configs.d.ts} +0 -0
- /package/{dist/configs.d.ts.map → configs.d.ts.map} +0 -0
- /package/{dist/index.node.d.ts → index.node.d.ts} +0 -0
- /package/{dist/index.node.d.ts.map → index.node.d.ts.map} +0 -0
- /package/{dist/index.web.d.ts → index.web.d.ts} +0 -0
- /package/{dist/index.web.d.ts.map → index.web.d.ts.map} +0 -0
- /package/{dist/lib → lib}/block.d.ts +0 -0
- /package/{dist/lib → lib}/block.d.ts.map +0 -0
- /package/{dist/lib → lib}/blockchain.d.ts +0 -0
- /package/{dist/lib → lib}/blockchain.d.ts.map +0 -0
- /package/{dist/lib → lib}/custom/custom_shared_methods.d.ts +0 -0
- /package/{dist/lib → lib}/custom/custom_shared_methods.d.ts.map +0 -0
- /package/{dist/lib → lib}/custom/shared_methods.web.d.ts +0 -0
- /package/{dist/lib → lib}/custom/shared_methods.web.d.ts.map +0 -0
- /package/{dist/lib → lib}/factory.d.ts +0 -0
- /package/{dist/lib → lib}/factory.d.ts.map +0 -0
- /package/{dist/lib → lib}/peer.d.ts +0 -0
- /package/{dist/lib → lib}/peer.d.ts.map +0 -0
- /package/{dist/lib → lib}/saito_factory.d.ts +0 -0
- /package/{dist/lib → lib}/saito_factory.d.ts.map +0 -0
- /package/{dist/lib → lib}/slip.d.ts +0 -0
- /package/{dist/lib → lib}/slip.d.ts.map +0 -0
- /package/{dist/lib → lib}/transaction.d.ts +0 -0
- /package/{dist/lib → lib}/transaction.d.ts.map +0 -0
- /package/{dist/lib → lib}/wallet.d.ts +0 -0
- /package/{dist/lib → lib}/wallet.d.ts.map +0 -0
- /package/{dist/saito.d.ts → saito.d.ts} +0 -0
- /package/{dist/saito.d.ts.map → saito.d.ts.map} +0 -0
- /package/{dist/server → server}/index.js +0 -0
- /package/{dist/shared_methods.d.ts → shared_methods.d.ts} +0 -0
- /package/{dist/shared_methods.d.ts.map → shared_methods.d.ts.map} +0 -0
- /package/{dist/tests → tests}/index.test.d.ts +0 -0
- /package/{dist/tests → tests}/index.test.d.ts.map +0 -0
package/lib/transaction.ts
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import type {WasmTransaction} from "saito-wasm/dist/types/pkg/node/index_bg";
|
|
2
|
-
import Slip from "./slip";
|
|
3
|
-
import Saito from "../saito";
|
|
4
|
-
import Factory from "./factory";
|
|
5
|
-
|
|
6
|
-
export enum TransactionType {
|
|
7
|
-
Normal = 0,
|
|
8
|
-
Fee = 1,
|
|
9
|
-
GoldenTicket = 2,
|
|
10
|
-
ATR = 3,
|
|
11
|
-
Vip = 4,
|
|
12
|
-
SPV = 5,
|
|
13
|
-
Issuance = 6,
|
|
14
|
-
Other = 7,
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export default class Transaction {
|
|
18
|
-
protected tx: WasmTransaction;
|
|
19
|
-
public static Type: any;
|
|
20
|
-
public msg: any = {};
|
|
21
|
-
|
|
22
|
-
// TODO : factory pattern might be useful here to remove unnecessary wrappings
|
|
23
|
-
constructor(tx?: WasmTransaction, json?: any) {
|
|
24
|
-
if (tx) {
|
|
25
|
-
this.tx = tx;
|
|
26
|
-
} else {
|
|
27
|
-
this.tx = new Transaction.Type();
|
|
28
|
-
}
|
|
29
|
-
if (json) {
|
|
30
|
-
for (let slip of json.to) {
|
|
31
|
-
let s = new Slip(undefined, slip);
|
|
32
|
-
this.addToSlip(s);
|
|
33
|
-
}
|
|
34
|
-
for (let slip of json.from) {
|
|
35
|
-
let s = new Slip(undefined, slip);
|
|
36
|
-
this.addFromSlip(s);
|
|
37
|
-
}
|
|
38
|
-
this.timestamp = json.timestamp;
|
|
39
|
-
this.type = json.type;
|
|
40
|
-
this.signature = json.signature;
|
|
41
|
-
this.data = new Uint8Array(Buffer.from(json.buffer, "base64"));
|
|
42
|
-
this.txs_replacements = json.txs_replacements;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public get wasmTransaction(): WasmTransaction {
|
|
47
|
-
return this.tx;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
public addFromSlip(slip: Slip) {
|
|
51
|
-
this.tx.add_from_slip(slip.wasmSlip);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
public addToSlip(slip: Slip) {
|
|
55
|
-
this.tx.add_to_slip(slip.wasmSlip);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public get to(): Array<Slip> {
|
|
59
|
-
return this.tx.to.map((slip) => {
|
|
60
|
-
return Saito.getInstance().factory.createSlip(slip);
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public get from(): Array<Slip> {
|
|
65
|
-
return this.tx.from.map((slip) => {
|
|
66
|
-
return Saito.getInstance().factory.createSlip(slip);
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
public get type(): TransactionType {
|
|
71
|
-
return this.tx.type as TransactionType;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
public set type(type: TransactionType) {
|
|
75
|
-
this.tx.type = type as number;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
public get timestamp(): number {
|
|
79
|
-
return Number(this.tx.timestamp);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
public set timestamp(timestamp: bigint | number) {
|
|
83
|
-
this.tx.timestamp = BigInt(timestamp);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
public set signature(sig: string) {
|
|
87
|
-
this.tx.signature = sig;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
public get signature(): string {
|
|
91
|
-
return this.tx.signature;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
public set data(buffer: Uint8Array) {
|
|
95
|
-
this.tx.data = buffer;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
public get data(): Uint8Array {
|
|
99
|
-
return this.tx.data;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
public set txs_replacements(r: number) {
|
|
103
|
-
this.tx.txs_replacements = r;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
public get txs_replacements(): number {
|
|
107
|
-
return this.tx.txs_replacements;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
public get total_fees(): bigint {
|
|
111
|
-
return this.tx.total_fees;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
public async sign() {
|
|
115
|
-
return this.tx.sign();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
public async signAndEncrypt() {
|
|
119
|
-
return this.tx.sign_and_encrypt();
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
public isFrom(key: string): boolean {
|
|
123
|
-
return this.tx.is_from(key);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
public isTo(key: string): boolean {
|
|
127
|
-
return this.tx.is_to(key);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
public toJson() {
|
|
131
|
-
this.packData();
|
|
132
|
-
return {
|
|
133
|
-
to: this.to.map((slip) => slip.toJson()),
|
|
134
|
-
from: this.from.map((slip) => slip.toJson()),
|
|
135
|
-
type: this.type,
|
|
136
|
-
timestamp: this.timestamp,
|
|
137
|
-
signature: this.signature,
|
|
138
|
-
buffer: Buffer.from(this.data).toString("base64"),
|
|
139
|
-
txs_replacements: this.txs_replacements,
|
|
140
|
-
total_fees: this.total_fees,
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
public static deserialize(buffer: Uint8Array, factory: Factory): Transaction | null {
|
|
145
|
-
try {
|
|
146
|
-
let wasmTx = Transaction.Type.deserialize(buffer);
|
|
147
|
-
return factory.createTransaction(wasmTx);
|
|
148
|
-
} catch (e) {
|
|
149
|
-
console.debug(e);
|
|
150
|
-
return null;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
public serialize(): Uint8Array {
|
|
155
|
-
return this.tx.serialize();
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
public packData() {
|
|
159
|
-
if (Object.keys(this.msg).length === 0) {
|
|
160
|
-
this.data = new Uint8Array(Buffer.alloc(0));
|
|
161
|
-
} else {
|
|
162
|
-
this.data = new Uint8Array(Buffer.from(JSON.stringify(this.msg), "utf-8"));
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
public unpackData() {
|
|
167
|
-
if (this.data.byteLength === 0) {
|
|
168
|
-
this.msg = {};
|
|
169
|
-
} else {
|
|
170
|
-
try {
|
|
171
|
-
this.msg = JSON.parse(Buffer.from(this.data).toString("utf-8"));
|
|
172
|
-
} catch (error) {
|
|
173
|
-
console.log("failed parsing tx buffer into msg");
|
|
174
|
-
console.error(error);
|
|
175
|
-
this.msg = {};
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
}
|
package/lib/wallet.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import type { WasmWallet } from "saito-wasm/dist/types/pkg/node/index_bg";
|
|
2
|
-
import Saito from "../saito";
|
|
3
|
-
|
|
4
|
-
export const DefaultEmptyPrivateKey =
|
|
5
|
-
"0000000000000000000000000000000000000000000000000000000000000000";
|
|
6
|
-
export const DefaultEmptyPublicKey =
|
|
7
|
-
"000000000000000000000000000000000000000000000000000000000000000000";
|
|
8
|
-
|
|
9
|
-
export default class Wallet {
|
|
10
|
-
protected wallet: WasmWallet;
|
|
11
|
-
public static Type: any;
|
|
12
|
-
|
|
13
|
-
constructor(wallet: WasmWallet) {
|
|
14
|
-
this.wallet = wallet;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
public async save() {
|
|
18
|
-
return this.wallet.save();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public async load() {
|
|
22
|
-
return this.wallet.load();
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public async reset() {
|
|
26
|
-
return this.wallet.reset();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public async getPublicKey() {
|
|
30
|
-
let key = await this.wallet.get_public_key();
|
|
31
|
-
return key === DefaultEmptyPublicKey ? "" : key;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public async setPublicKey(key: string) {
|
|
35
|
-
if (key === "") {
|
|
36
|
-
key = DefaultEmptyPublicKey;
|
|
37
|
-
}
|
|
38
|
-
return this.wallet.set_public_key(key);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public async getPrivateKey() {
|
|
42
|
-
let key = await this.wallet.get_private_key();
|
|
43
|
-
return key === DefaultEmptyPrivateKey ? "" : key;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public async setPrivateKey(key: string) {
|
|
47
|
-
if (key === "") {
|
|
48
|
-
key = DefaultEmptyPrivateKey;
|
|
49
|
-
}
|
|
50
|
-
return this.wallet.set_private_key(key);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
public async getBalance() {
|
|
54
|
-
return this.wallet.get_balance();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
public async getPendingTxs() {
|
|
58
|
-
let txs = await this.wallet.get_pending_txs();
|
|
59
|
-
return txs.map((tx: any) => Saito.getInstance().factory.createTransaction(tx));
|
|
60
|
-
}
|
|
61
|
-
}
|
package/saito.ts
DELETED
|
@@ -1,404 +0,0 @@
|
|
|
1
|
-
import SharedMethods from "./shared_methods";
|
|
2
|
-
import Transaction from "./lib/transaction";
|
|
3
|
-
import Block from "./lib/block";
|
|
4
|
-
import Factory from "./lib/factory";
|
|
5
|
-
import Peer from "./lib/peer";
|
|
6
|
-
import Wallet, {DefaultEmptyPrivateKey} from "./lib/wallet";
|
|
7
|
-
import Blockchain from "./lib/blockchain";
|
|
8
|
-
|
|
9
|
-
// export enum MessageType {
|
|
10
|
-
// HandshakeChallenge = 1,
|
|
11
|
-
// HandshakeResponse,
|
|
12
|
-
// //HandshakeCompletion,
|
|
13
|
-
// ApplicationMessage = 4,
|
|
14
|
-
// Block,
|
|
15
|
-
// Transaction,
|
|
16
|
-
// BlockchainRequest,
|
|
17
|
-
// BlockHeaderHash,
|
|
18
|
-
// Ping,
|
|
19
|
-
// SPVChain,
|
|
20
|
-
// Services,
|
|
21
|
-
// GhostChain,
|
|
22
|
-
// GhostChainRequest,
|
|
23
|
-
// Result,
|
|
24
|
-
// Error,
|
|
25
|
-
// ApplicationTransaction,
|
|
26
|
-
// }
|
|
27
|
-
|
|
28
|
-
export default class Saito {
|
|
29
|
-
private static instance: Saito;
|
|
30
|
-
private static libInstance: any;
|
|
31
|
-
sockets: Map<bigint, any> = new Map<bigint, any>();
|
|
32
|
-
nextIndex: bigint = BigInt(0);
|
|
33
|
-
factory = new Factory();
|
|
34
|
-
promises = new Map<number, any>();
|
|
35
|
-
private callbackIndex: number = 1;
|
|
36
|
-
private wallet: Wallet | null = null;
|
|
37
|
-
private blockchain: Blockchain | null = null;
|
|
38
|
-
|
|
39
|
-
public static async initialize(
|
|
40
|
-
configs: any,
|
|
41
|
-
sharedMethods: SharedMethods,
|
|
42
|
-
factory = new Factory(),
|
|
43
|
-
privateKey: string
|
|
44
|
-
) {
|
|
45
|
-
this.instance = new Saito(factory);
|
|
46
|
-
|
|
47
|
-
// @ts-ignore
|
|
48
|
-
globalThis.shared_methods = {
|
|
49
|
-
send_message: (peer_index: bigint, buffer: Uint8Array) => {
|
|
50
|
-
sharedMethods.sendMessage(peer_index, buffer);
|
|
51
|
-
},
|
|
52
|
-
send_message_to_all: (buffer: Uint8Array, exceptions: Array<bigint>) => {
|
|
53
|
-
sharedMethods.sendMessageToAll(buffer, exceptions);
|
|
54
|
-
},
|
|
55
|
-
connect_to_peer: (peer_data: any) => {
|
|
56
|
-
sharedMethods.connectToPeer(peer_data);
|
|
57
|
-
},
|
|
58
|
-
write_value: (key: string, value: Uint8Array) => {
|
|
59
|
-
return sharedMethods.writeValue(key, value);
|
|
60
|
-
},
|
|
61
|
-
read_value: (key: string) => {
|
|
62
|
-
return sharedMethods.readValue(key);
|
|
63
|
-
},
|
|
64
|
-
load_block_file_list: () => {
|
|
65
|
-
return sharedMethods.loadBlockFileList();
|
|
66
|
-
},
|
|
67
|
-
is_existing_file: (key: string) => {
|
|
68
|
-
return sharedMethods.isExistingFile(key);
|
|
69
|
-
},
|
|
70
|
-
remove_value: (key: string) => {
|
|
71
|
-
return sharedMethods.removeValue(key);
|
|
72
|
-
},
|
|
73
|
-
disconnect_from_peer: (peer_index: bigint) => {
|
|
74
|
-
return sharedMethods.disconnectFromPeer(peer_index);
|
|
75
|
-
},
|
|
76
|
-
fetch_block_from_peer: (hash: Uint8Array, peer_index: bigint, url: string) => {
|
|
77
|
-
console.log("fetching block : " + url);
|
|
78
|
-
sharedMethods.fetchBlockFromPeer(url).then((buffer: Uint8Array) => {
|
|
79
|
-
Saito.getLibInstance().process_fetched_block(buffer, hash, peer_index);
|
|
80
|
-
});
|
|
81
|
-
},
|
|
82
|
-
process_api_call: (buffer: Uint8Array, msgIndex: number, peerIndex: bigint) => {
|
|
83
|
-
return sharedMethods.processApiCall(buffer, msgIndex, peerIndex).then(() => {
|
|
84
|
-
});
|
|
85
|
-
},
|
|
86
|
-
process_api_success: (buffer: Uint8Array, msgIndex: number, peerIndex: bigint) => {
|
|
87
|
-
return sharedMethods.processApiSuccess(buffer, msgIndex, peerIndex);
|
|
88
|
-
},
|
|
89
|
-
process_api_error: (buffer: Uint8Array, msgIndex: number, peerIndex: bigint) => {
|
|
90
|
-
return sharedMethods.processApiError(buffer, msgIndex, peerIndex);
|
|
91
|
-
},
|
|
92
|
-
send_interface_event: (event: string, peerIndex: bigint) => {
|
|
93
|
-
return sharedMethods.sendInterfaceEvent(event, peerIndex);
|
|
94
|
-
},
|
|
95
|
-
save_wallet: (wallet: any) => {
|
|
96
|
-
return sharedMethods.saveWallet(wallet);
|
|
97
|
-
},
|
|
98
|
-
load_wallet: (wallet: any) => {
|
|
99
|
-
return sharedMethods.loadWallet(wallet);
|
|
100
|
-
},
|
|
101
|
-
save_blockchain: (blockchain: any) => {
|
|
102
|
-
return sharedMethods.saveBlockchain(blockchain);
|
|
103
|
-
},
|
|
104
|
-
load_blockchain: (blockchain: any) => {
|
|
105
|
-
return sharedMethods.loadBlockchain(blockchain);
|
|
106
|
-
},
|
|
107
|
-
};
|
|
108
|
-
if (privateKey === "") {
|
|
109
|
-
privateKey = DefaultEmptyPrivateKey;
|
|
110
|
-
}
|
|
111
|
-
await Saito.getLibInstance().initialize(JSON.stringify(configs), privateKey);
|
|
112
|
-
|
|
113
|
-
console.log("saito initialized");
|
|
114
|
-
|
|
115
|
-
setInterval(() => {
|
|
116
|
-
Saito.getLibInstance().process_timer_event(BigInt(100));
|
|
117
|
-
// console.log(`WASM memory usage is ${wasm.memory.buffer.byteLength} bytes`);
|
|
118
|
-
}, 100);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
constructor(factory: Factory) {
|
|
122
|
-
this.factory = factory;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
public static getInstance(): Saito {
|
|
126
|
-
return Saito.instance;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
public static getLibInstance(): any {
|
|
130
|
-
return Saito.libInstance;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
public static setLibInstance(instance: any) {
|
|
134
|
-
Saito.libInstance = instance;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
public addNewSocket(socket: any): bigint {
|
|
138
|
-
this.nextIndex++;
|
|
139
|
-
this.sockets.set(this.nextIndex, socket);
|
|
140
|
-
return this.nextIndex;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
public getSocket(index: bigint): any | null {
|
|
144
|
-
return this.sockets.get(index);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
public removeSocket(index: bigint) {
|
|
148
|
-
let socket = this.sockets.get(index);
|
|
149
|
-
this.sockets.delete(index);
|
|
150
|
-
socket.close();
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
public async initialize(configs: any): Promise<any> {
|
|
154
|
-
return Saito.getLibInstance().initialize(configs);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
public async getLatestBlockHash(): Promise<string> {
|
|
158
|
-
return Saito.getLibInstance().get_latest_block_hash();
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
public async getBlock<B extends Block>(blockHash: string): Promise<B> {
|
|
162
|
-
let block = await Saito.getLibInstance().get_block(blockHash);
|
|
163
|
-
return Saito.getInstance().factory.createBlock(block) as B;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// public async getPublicKey(): Promise<string> {
|
|
167
|
-
// return Saito.getLibInstance().get_public_key();
|
|
168
|
-
// }
|
|
169
|
-
//
|
|
170
|
-
// public async getPrivateKey(): Promise<string> {
|
|
171
|
-
// return Saito.getLibInstance().get_private_key();
|
|
172
|
-
// }
|
|
173
|
-
|
|
174
|
-
public async processNewPeer(index: bigint, peer_config: any): Promise<void> {
|
|
175
|
-
return Saito.getLibInstance().process_new_peer(index, peer_config);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
public async processPeerDisconnection(peer_index: bigint): Promise<void> {
|
|
179
|
-
return Saito.getLibInstance().process_peer_disconnection(peer_index);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
public async processMsgBufferFromPeer(buffer: Uint8Array, peer_index: bigint): Promise<void> {
|
|
183
|
-
return Saito.getLibInstance().process_msg_buffer_from_peer(buffer, peer_index);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
public async processFetchedBlock(
|
|
187
|
-
buffer: Uint8Array,
|
|
188
|
-
hash: Uint8Array,
|
|
189
|
-
peer_index: bigint
|
|
190
|
-
): Promise<void> {
|
|
191
|
-
return Saito.getLibInstance().process_fetched_block(buffer, hash, peer_index);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
public async processTimerEvent(duration_in_ms: bigint): Promise<void> {
|
|
195
|
-
return Saito.getLibInstance().process_timer_event(duration_in_ms);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
public hash(buffer: Uint8Array): string {
|
|
199
|
-
return Saito.getLibInstance().hash(buffer);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
public signBuffer(buffer: Uint8Array, privateKey: String): string {
|
|
203
|
-
return Saito.getLibInstance().sign_buffer(buffer, privateKey);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
public verifySignature(buffer: Uint8Array, signature: string, publicKey: string): boolean {
|
|
207
|
-
return Saito.getLibInstance().verify_signature(buffer, signature, publicKey);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
public async createTransaction<T extends Transaction>(
|
|
211
|
-
publickey = "",
|
|
212
|
-
amount = BigInt(0),
|
|
213
|
-
fee = BigInt(0),
|
|
214
|
-
force_merge = false
|
|
215
|
-
): Promise<T> {
|
|
216
|
-
let wasmTx = await Saito.getLibInstance().create_transaction(
|
|
217
|
-
publickey,
|
|
218
|
-
amount,
|
|
219
|
-
fee,
|
|
220
|
-
force_merge
|
|
221
|
-
);
|
|
222
|
-
let tx = Saito.getInstance().factory.createTransaction(wasmTx) as T;
|
|
223
|
-
tx.timestamp = new Date().getTime();
|
|
224
|
-
return tx;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// public async signTransaction(tx: Transaction): Promise<Transaction> {
|
|
228
|
-
// tx.packData();
|
|
229
|
-
// await tx.wasmTransaction.sign();
|
|
230
|
-
// return tx;
|
|
231
|
-
// }
|
|
232
|
-
|
|
233
|
-
// public async getPendingTransactions<Tx extends Transaction>(): Promise<Array<Tx>> {
|
|
234
|
-
// let txs = await Saito.getLibInstance().get_pending_txs();
|
|
235
|
-
// return txs.map((tx: any) => Saito.getInstance().factory.createTransaction(tx));
|
|
236
|
-
// }
|
|
237
|
-
|
|
238
|
-
// public async signAndEncryptTransaction(tx: Transaction) {
|
|
239
|
-
// return tx.wasmTransaction.sign_and_encrypt();
|
|
240
|
-
// }
|
|
241
|
-
|
|
242
|
-
// public async getBalance(): Promise<bigint> {
|
|
243
|
-
// return Saito.getLibInstance().get_balance();
|
|
244
|
-
// }
|
|
245
|
-
|
|
246
|
-
public async getPeers(): Promise<Array<Peer>> {
|
|
247
|
-
let peers = await Saito.getLibInstance().get_peers();
|
|
248
|
-
return peers.map((peer: any) => {
|
|
249
|
-
return this.factory.createPeer(peer);
|
|
250
|
-
});
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
public async getPeer(index: bigint): Promise<Peer | null> {
|
|
254
|
-
let peer = await Saito.getLibInstance().get_peer(index);
|
|
255
|
-
if (!peer) {
|
|
256
|
-
return null;
|
|
257
|
-
}
|
|
258
|
-
return this.factory.createPeer(peer);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
public generatePrivateKey(): string {
|
|
262
|
-
return Saito.getLibInstance().generate_private_key();
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
public generatePublicKey(privateKey: string): string {
|
|
266
|
-
return Saito.getLibInstance().generate_public_key(privateKey);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
public async propagateTransaction(tx: Transaction) {
|
|
270
|
-
return Saito.getLibInstance().propagate_transaction(tx.wasmTransaction);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
public async sendApiCall(
|
|
274
|
-
buffer: Uint8Array,
|
|
275
|
-
peerIndex: bigint,
|
|
276
|
-
waitForReply: boolean
|
|
277
|
-
): Promise<Uint8Array> {
|
|
278
|
-
console.log("saito.sendApiCall : peer = " + peerIndex + " wait for reply = " + waitForReply);
|
|
279
|
-
let callbackIndex = this.callbackIndex++;
|
|
280
|
-
if (waitForReply) {
|
|
281
|
-
return new Promise((resolve, reject) => {
|
|
282
|
-
this.promises.set(callbackIndex, {
|
|
283
|
-
resolve,
|
|
284
|
-
reject,
|
|
285
|
-
});
|
|
286
|
-
Saito.getLibInstance().send_api_call(buffer, callbackIndex, peerIndex);
|
|
287
|
-
});
|
|
288
|
-
} else {
|
|
289
|
-
return Saito.getLibInstance().send_api_call(buffer, callbackIndex, peerIndex);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
public async sendApiSuccess(msgId: number, buffer: Uint8Array, peerIndex: bigint) {
|
|
294
|
-
await Saito.getLibInstance().send_api_success(buffer, msgId, peerIndex);
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
public async sendApiError(msgId: number, buffer: Uint8Array, peerIndex: bigint) {
|
|
298
|
-
await Saito.getLibInstance().send_api_error(buffer, msgId, peerIndex);
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
public async sendTransactionWithCallback(
|
|
302
|
-
transaction: Transaction,
|
|
303
|
-
callback?: any,
|
|
304
|
-
peerIndex?: bigint
|
|
305
|
-
): Promise<any> {
|
|
306
|
-
// TODO : implement retry on fail
|
|
307
|
-
// TODO : stun code goes here probably???
|
|
308
|
-
console.log(
|
|
309
|
-
"saito.sendTransactionWithCallback : peer = " + peerIndex + " sig = " + transaction.signature
|
|
310
|
-
);
|
|
311
|
-
let buffer = transaction.wasmTransaction.serialize();
|
|
312
|
-
console.log(
|
|
313
|
-
"sendTransactionWithCallback : " +
|
|
314
|
-
peerIndex +
|
|
315
|
-
" with length : " +
|
|
316
|
-
buffer.byteLength +
|
|
317
|
-
" sent : ",
|
|
318
|
-
transaction.msg
|
|
319
|
-
);
|
|
320
|
-
await this.sendApiCall(buffer, peerIndex || BigInt(0), !!callback)
|
|
321
|
-
.then((buffer: Uint8Array) => {
|
|
322
|
-
if (callback) {
|
|
323
|
-
// console.log("deserializing tx. buffer length = " + buffer.byteLength);
|
|
324
|
-
console.log("sendTransactionWithCallback. buffer length = " + buffer.byteLength);
|
|
325
|
-
|
|
326
|
-
let tx = this.factory.createTransaction();
|
|
327
|
-
tx.data = buffer;
|
|
328
|
-
tx.unpackData();
|
|
329
|
-
// let tx = Transaction.deserialize(buffer, this.factory);
|
|
330
|
-
// if (tx) {
|
|
331
|
-
// console.log("sendTransactionWithCallback received : ", tx);
|
|
332
|
-
// return callback(tx.data);
|
|
333
|
-
// } else {
|
|
334
|
-
// console.log("sendTransactionWithCallback sending direct buffer since tx deserialization failed");
|
|
335
|
-
return callback(tx);
|
|
336
|
-
// }
|
|
337
|
-
}
|
|
338
|
-
})
|
|
339
|
-
.catch((error) => {
|
|
340
|
-
console.error(error);
|
|
341
|
-
if (callback) {
|
|
342
|
-
return callback({err: error.toString()});
|
|
343
|
-
}
|
|
344
|
-
});
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
public async propagateServices(peerIndex: bigint, services: string[]) {
|
|
348
|
-
return Saito.getLibInstance().propagate_services(peerIndex, services);
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
public async sendRequest(
|
|
352
|
-
message: string,
|
|
353
|
-
data: any = "",
|
|
354
|
-
callback?: any,
|
|
355
|
-
peerIndex?: bigint
|
|
356
|
-
): Promise<any> {
|
|
357
|
-
console.log("saito.sendRequest : peer = " + peerIndex);
|
|
358
|
-
let wallet = await this.getWallet();
|
|
359
|
-
let publicKey = await wallet.getPublicKey();
|
|
360
|
-
let tx = await this.createTransaction(publicKey, BigInt(0), BigInt(0));
|
|
361
|
-
tx.msg = {
|
|
362
|
-
request: message,
|
|
363
|
-
data: data,
|
|
364
|
-
};
|
|
365
|
-
tx.packData();
|
|
366
|
-
return this.sendTransactionWithCallback(
|
|
367
|
-
tx,
|
|
368
|
-
(tx: Transaction) => {
|
|
369
|
-
if (callback) {
|
|
370
|
-
return callback(tx.msg);
|
|
371
|
-
}
|
|
372
|
-
},
|
|
373
|
-
peerIndex
|
|
374
|
-
);
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
public async getWallet() {
|
|
378
|
-
if (!this.wallet) {
|
|
379
|
-
let w = await Saito.getLibInstance().get_wallet();
|
|
380
|
-
this.wallet = this.factory.createWallet(w);
|
|
381
|
-
}
|
|
382
|
-
return this.wallet;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
public async getBlockchain() {
|
|
386
|
-
if (!this.blockchain) {
|
|
387
|
-
let b = await Saito.getLibInstance().get_blockchain();
|
|
388
|
-
this.blockchain = this.factory.createBlockchain(b);
|
|
389
|
-
}
|
|
390
|
-
return this.blockchain;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
// public async loadWallet() {
|
|
394
|
-
// return Saito.getLibInstance().load_wallet();
|
|
395
|
-
// }
|
|
396
|
-
//
|
|
397
|
-
// public async saveWallet() {
|
|
398
|
-
// return Saito.getLibInstance().save_wallet();
|
|
399
|
-
// }
|
|
400
|
-
//
|
|
401
|
-
// public async resetWallet() {
|
|
402
|
-
// return Saito.getLibInstance().reset_wallet();
|
|
403
|
-
// }
|
|
404
|
-
}
|
package/shared_methods.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import Wallet from "./lib/wallet";
|
|
2
|
-
import Blockchain from "./lib/blockchain";
|
|
3
|
-
|
|
4
|
-
export default interface SharedMethods {
|
|
5
|
-
sendMessage(peerIndex: bigint, buffer: Uint8Array): void;
|
|
6
|
-
|
|
7
|
-
sendMessageToAll(buffer: Uint8Array, exceptions: Array<bigint>): void;
|
|
8
|
-
|
|
9
|
-
connectToPeer(peerData: any): void;
|
|
10
|
-
|
|
11
|
-
writeValue(key: string, value: Uint8Array): void;
|
|
12
|
-
|
|
13
|
-
readValue(key: string): Uint8Array | null;
|
|
14
|
-
|
|
15
|
-
loadBlockFileList(): Array<string>;
|
|
16
|
-
|
|
17
|
-
isExistingFile(key: string): boolean;
|
|
18
|
-
|
|
19
|
-
removeValue(key: string): void;
|
|
20
|
-
|
|
21
|
-
disconnectFromPeer(peerIndex: bigint): void;
|
|
22
|
-
|
|
23
|
-
fetchBlockFromPeer(url: string): Promise<Uint8Array>;
|
|
24
|
-
|
|
25
|
-
processApiCall(buffer: Uint8Array, msgIndex: number, peerIndex: bigint): Promise<void>;
|
|
26
|
-
|
|
27
|
-
processApiSuccess(buffer: Uint8Array, msgIndex: number, peerIndex: bigint): void;
|
|
28
|
-
|
|
29
|
-
processApiError(buffer: Uint8Array, msgIndex: number, peerIndex: bigint): void;
|
|
30
|
-
|
|
31
|
-
sendInterfaceEvent(event: String, peerIndex: bigint): void;
|
|
32
|
-
|
|
33
|
-
saveWallet(wallet: Wallet): void;
|
|
34
|
-
|
|
35
|
-
loadWallet(wallet: Wallet): void;
|
|
36
|
-
|
|
37
|
-
saveBlockchain(blockchain: Blockchain): void;
|
|
38
|
-
|
|
39
|
-
loadBlockchain(blockchain: Blockchain): void;
|
|
40
|
-
}
|