Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #31 from SaitoTech/develop
Browse files Browse the repository at this point in the history
wasm bug fixing
  • Loading branch information
SankaD authored Aug 1, 2023
2 parents 68a9425 + 096eac2 commit 8505037
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 107 deletions.
7 changes: 5 additions & 2 deletions lib/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { WasmBlock } from "saito-wasm/pkg/node/index";
import Transaction from "./transaction";
import Saito from "../saito";
import WasmWrapper from "./wasm_wrapper";
import { fromBase58 } from "./util";

export enum BlockType {
Ghost = 0,
Expand Down Expand Up @@ -47,11 +48,13 @@ export default class Block extends WasmWrapper<WasmBlock> {
}

public hasKeylistTxs(keylist: Array<string>): boolean {
return this.instance.has_keylist_txs(keylist);
let keys = keylist.map((key) => fromBase58(key));
return this.instance.has_keylist_txs(keys);
}

public generateLiteBlock(keylist: Array<string>): Block {
let block = this.instance.generate_lite_block(keylist);
let keys = keylist.map((key) => fromBase58(key));
let block = this.instance.generate_lite_block(keys);
return Saito.getInstance().factory.createBlock(block);
}

Expand Down
49 changes: 0 additions & 49 deletions lib/config.ts

This file was deleted.

5 changes: 2 additions & 3 deletions lib/custom/shared_methods.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export default class WebSharedMethods extends CustomSharedMethods {
console.log("connecting to " + url + "....");
let socket = new WebSocket(url);
socket.binaryType = "arraybuffer";
console.log("ssss : ", Saito.getInstance());
let index = Saito.getInstance().addNewSocket(socket);

socket.onmessage = (event: MessageEvent) => {
Expand Down Expand Up @@ -95,13 +94,13 @@ export default class WebSharedMethods extends CustomSharedMethods {
}

sendMessage(peerIndex: bigint, buffer: Uint8Array): void {
console.debug("sending message to peer : " + peerIndex + " with size : " + buffer.byteLength);
// console.debug("sending message to peer : " + peerIndex + " with size : " + buffer.byteLength);
let socket = Saito.getInstance().getSocket(peerIndex);
socket.send(buffer);
}

sendMessageToAll(buffer: Uint8Array, exceptions: Array<bigint>): void {
console.debug("sending message to all with size : " + buffer.byteLength);
// console.debug("sending message to all with size : " + buffer.byteLength);
Saito.getInstance().sockets.forEach((socket, key) => {
if (exceptions.includes(key)) {
return;
Expand Down
5 changes: 3 additions & 2 deletions lib/peer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { WasmPeer, WasmPeerService } from "saito-wasm/pkg/node/index";
import WasmWrapper from "./wasm_wrapper";
import { toBase58 } from "./util";

export default class Peer extends WasmWrapper<WasmPeer> {
public static Type: any;
Expand All @@ -12,15 +13,15 @@ export default class Peer extends WasmWrapper<WasmPeer> {
}

public get publicKey(): string {
return this.instance.public_key;
return toBase58(this.instance.public_key);
}

// public set publicKey(key: string) {
// this.peer.public_key = key;
// }

public get keyList(): Array<string> {
return this.instance.key_list;
return this.instance.key_list.map((key) => toBase58(key));
}

public get peerIndex(): bigint {
Expand Down
18 changes: 0 additions & 18 deletions lib/saito_factory.ts

This file was deleted.

11 changes: 9 additions & 2 deletions lib/slip.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { WasmSlip } from "saito-wasm/pkg/node/index";
import WasmWrapper from "./wasm_wrapper";
import { fromBase58, toBase58 } from "./util";

export enum SlipType {
Normal = 0,
Expand Down Expand Up @@ -55,11 +56,17 @@ export default class Slip extends WasmWrapper<WasmSlip> {
}

public get publicKey(): string {
return this.instance.public_key;
if (this.instance.public_key == "") {
return "";
}
return toBase58(this.instance.public_key);
}

public set publicKey(key: string) {
this.instance.public_key = key;
if (key === "") {
this.instance.public_key = "";
}
this.instance.public_key = fromBase58(key);
}

public set index(index: number) {
Expand Down
9 changes: 7 additions & 2 deletions lib/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { WasmTransaction } from "saito-wasm/pkg/node/index";
import Slip from "./slip";
import Saito from "../saito";
import WasmWrapper from "./wasm_wrapper";
import { fromBase58 } from "./util";

export enum TransactionType {
Normal = 0,
Expand Down Expand Up @@ -112,11 +113,11 @@ export default class Transaction extends WasmWrapper<WasmTransaction> {
}

public isFrom(key: string): boolean {
return this.instance.is_from(key);
return this.instance.is_from(fromBase58(key));
}

public isTo(key: string): boolean {
return this.instance.is_to(key);
return this.instance.is_to(fromBase58(key));
}

public toJson() {
Expand Down Expand Up @@ -172,4 +173,8 @@ export default class Transaction extends WasmWrapper<WasmTransaction> {
}
}
}

public clone() {
return new Transaction.Type(undefined, this.toJson());
}
}
10 changes: 10 additions & 0 deletions lib/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @ts-ignore
import * as base58 from "base-58";

export function toBase58(input: string): string {
return base58.encode(Buffer.from(input, "hex"));
}

export function fromBase58(input: string): string {
return Buffer.from(base58.decode(input)).toString("hex");
}
5 changes: 3 additions & 2 deletions lib/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { WasmWallet } from "saito-wasm/pkg/node/index";
import Saito from "../saito";
import WasmWrapper from "./wasm_wrapper";
import { fromBase58, toBase58 } from "./util";

export const DefaultEmptyPrivateKey =
"0000000000000000000000000000000000000000000000000000000000000000";
Expand Down Expand Up @@ -28,14 +29,14 @@ export default class Wallet extends WasmWrapper<WasmWallet> {

public async getPublicKey() {
let key = await this.instance.get_public_key();
return key === DefaultEmptyPublicKey ? "" : key;
return key === DefaultEmptyPublicKey ? "" : toBase58(key);
}

public async setPublicKey(key: string) {
if (key === "") {
key = DefaultEmptyPublicKey;
}
return this.instance.set_public_key(key);
return this.instance.set_public_key(fromBase58(key));
}

public async getPrivateKey() {
Expand Down
15 changes: 13 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "saito-js",
"version": "0.0.28",
"version": "0.0.29",
"description": "js wrappings around saito-core using wasm",
"scripts": {
"test": "env TS_NODE_PROJECT=\"tsconfig.testing.json\" mocha --require ts-node/register 'tests/**/*.ts'",
Expand All @@ -27,15 +27,16 @@
"typescript": "^4.9.4"
},
"dependencies": {
"base-58": "^0.0.1",
"buffer": "^6.0.3",
"cookie-parser": "~1.4.6",
"saito-wasm": "^0.0.17",
"cors": "^2.8.5",
"debug": "^4.3.4",
"express": "~4.18.2",
"morgan": "~1.10.0",
"node-fetch": "^2.6.1",
"process": "^0.11.10",
"saito-wasm": "^0.0.17",
"url": "^0.11.0",
"util": "^0.12.5",
"ws": "^8.13.0"
Expand Down
29 changes: 6 additions & 23 deletions saito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Factory from "./lib/factory";
import Peer from "./lib/peer";
import Wallet, {DefaultEmptyPrivateKey} from "./lib/wallet";
import Blockchain from "./lib/blockchain";
import {fromBase58, toBase58} from "./lib/util";

// export enum MessageType {
// HandshakeChallenge = 1,
Expand Down Expand Up @@ -244,7 +245,7 @@ export default class Saito {
}

public verifySignature(buffer: Uint8Array, signature: string, publicKey: string): boolean {
return Saito.getLibInstance().verify_signature(buffer, signature, publicKey);
return Saito.getLibInstance().verify_signature(buffer, signature, fromBase58(publicKey));
}

public async createTransaction<T extends Transaction>(
Expand All @@ -254,7 +255,7 @@ export default class Saito {
force_merge = false
): Promise<T> {
let wasmTx = await Saito.getLibInstance().create_transaction(
publickey,
fromBase58(publickey),
amount,
fee,
force_merge
Expand Down Expand Up @@ -284,7 +285,8 @@ export default class Saito {
}

public generatePublicKey(privateKey: string): string {
return Saito.getLibInstance().generate_public_key(privateKey);
let key = Saito.getLibInstance().generate_public_key(privateKey);
return toBase58(key);
}

public async propagateTransaction(tx: Transaction) {
Expand Down Expand Up @@ -330,31 +332,16 @@ export default class Saito {
// "saito.sendTransactionWithCallback : peer = " + peerIndex + " sig = " + transaction.signature
// );
let buffer = transaction.wasmTransaction.serialize();
// console.log(
// "sendTransactionWithCallback : " +
// peerIndex +
// " with length : " +
// buffer.byteLength +
// " sent : ",
// transaction.msg
// );

await this.sendApiCall(buffer, peerIndex || BigInt(0), !!callback)
.then((buffer: Uint8Array) => {
if (callback) {
// console.log("deserializing tx. buffer length = " + buffer.byteLength);
console.log("sendTransactionWithCallback. buffer length = " + buffer.byteLength);

let tx = this.factory.createTransaction();
tx.data = buffer;
tx.unpackData();
// let tx = Transaction.deserialize(buffer, this.factory);
// if (tx) {
// console.log("sendTransactionWithCallback received : ", tx);
// return callback(tx.data);
// } else {
// console.log("sendTransactionWithCallback sending direct buffer since tx deserialization failed");
return callback(tx);
// }
}
})
.catch((error) => {
Expand All @@ -365,10 +352,6 @@ export default class Saito {
});
}

// public async propagateServices(peerIndex: bigint, services: string[]) {
// return Saito.getLibInstance().propagate_services(peerIndex, services);
// }

public async sendRequest(
message: string,
data: any = "",
Expand Down

0 comments on commit 8505037

Please sign in to comment.