Skip to content

Commit

Permalink
ts strict: true and noUnusedLocals: true - v1.0.5
Browse files Browse the repository at this point in the history
had to do one @ts-ignore because .d.ts file for tweetnacl_mod.js are not going to happen
  • Loading branch information
stjet committed Dec 14, 2024
1 parent 9dc8328 commit 295e180
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 17 deletions.
12 changes: 10 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "banani",
"version": "1.0.4",
"version": "1.0.5",
"description": "JS/TS library for the Banano cryptocurrency in the style of bananopie",
"main": "main.js",
"scripts": {
Expand All @@ -27,6 +27,7 @@
},
"homepage": "https://github.com/stjet/banani#readme",
"devDependencies": {
"@types/blake2b": "^2.1.3",
"esbuild": "^0.21.4",
"prettier": "3.3.2",
"typedoc": "^0.25.13",
Expand Down
3 changes: 2 additions & 1 deletion rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ export class RPCWithBackup extends RPC {
return resp_json;
} catch (e) {
//increment (so try next RPC in provided list), if all RPCs exhausted (all failed), throw error
if (!this.rpc_urls[++i]) throw Error(e);
//typescript says e might not inherit from Error which is technically true, but in this case it always will be
if (!this.rpc_urls[++i]) throw Error(e instanceof Error ? e.toString() : "RPC call error");
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"typeRoots": ["./node_modules/@types"],
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
"forceConsistentCasingInFileNames": true,
"strict": true,
"noUnusedLocals": true
},
"lib": ["ES2020"],
"exclude": ["node_modules", ".build", "browser-main.ts"]
Expand Down
5 changes: 3 additions & 2 deletions util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-ignore
import * as nacl from "./tweetnacl_mod";
import blake2b from "blake2b";
import type { AddressPrefix, Address, BlockNoSignature, BlockHash } from "./rpc_types";
Expand Down Expand Up @@ -122,7 +123,7 @@ export function utf8_to_uint8array(utf8: string): Uint8Array {

const BANANO_DECIMALS: number = 29;
/** Do `rpc.DECIMALS = banani.NANO_DECIMALS` if using Nano. Putting the wrong amount of decimals in may result in LOSS OF FUNDS. */
const NANO_DECIMALS: number = 30;
export const NANO_DECIMALS: number = 30;

/** Does NOT mean whole number, can be decimal like "4.2001". Use instead of regular number since those lose precision when decimal */
export type Whole = `${number}`; //number can include non-base-10 formats... but whatever, we can assume users will pass in only base-10 because they are normal for the most part
Expand Down Expand Up @@ -175,7 +176,7 @@ export function get_address_from_public_key(public_key: string, prefix: AddressP
//the previously mentioned padding the front with 4 bits
const encoded = uint8array_to_base32(hex_to_uint8array(`0${public_key}`));
//skip byte length assertions
const hashed = uint8array_to_base32(blake2b(5, null, null, null, true).update(hex_to_uint8array(public_key)).digest().reverse());
const hashed = uint8array_to_base32(blake2b(5, undefined, undefined, undefined, true).update(hex_to_uint8array(public_key)).digest().reverse());
return `ban_${encoded}${hashed}` as Address; //fix for old versions of typescript or something
}

Expand Down
23 changes: 13 additions & 10 deletions wallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as util from "./util";
import type { AccountInfoRPC, AccountReceivableRPC, AccountReceivableThresholdRPC, AccountReceivableSourceRPC, Address, Block, BlockNoSignature, BlockSubtype, BlockHash } from "./rpc_types";
import type { RPCInterface, RPC } from "./rpc";
import type { RPCInterface } from "./rpc";

export type WorkFunction = (block_hash: BlockHash) => Promise<string>;

Expand Down Expand Up @@ -68,7 +68,10 @@ export class Wallet {
const raw_send = util.whole_to_raw(amount, this.rpc.DECIMALS);
const info = cached_account_info ?? (await this.get_account_info(undefined, true)); //this should be lazy. the true makes sure representative is included
const pub_receive = util.get_public_key_from_address(to);
if (!representative) representative = info.representative;
if (representative === undefined) {
if (info.representative === undefined) throw Error("Missing field 'representative' in `cached_account_info`");
representative = info.representative;
}
const before_balance = BigInt(info.balance);
const new_balance = before_balance - raw_send;
if (new_balance < 0n) {
Expand All @@ -86,7 +89,7 @@ export class Wallet {
};
const s_block_hash = util.hash_block(block_ns); //block hash of the send block
let work = undefined;
if (gen_work) work = await this.work_function(s_block_hash);
if (gen_work && this.work_function) work = await this.work_function(s_block_hash);
const signature = util.sign_block_hash(this.private_key, s_block_hash);
const block = { ...block_ns, signature, work };
return await this.send_process(block, "send");
Expand All @@ -106,31 +109,31 @@ export class Wallet {
//doesn't matter if open or not, I think?
const block_info = await this.rpc.get_block_info(block_hash);
let before_balance = 0n;
if (!representative) representative = this.address;
let previous;
try {
const info = await this.get_account_info(undefined, true);
previous = info.frontier;
representative = info.representative;
if (!representative) representative = info.representative;
before_balance = BigInt(info.balance);
} catch (e) {
//todo, check if error message is "Account not found"
//console.log(e)
//unopened account probably
previous = "0".repeat(64);
}
if (representative === undefined) representative = this.address;
const block_ns: BlockNoSignature = {
type: "state",
account: this.address,
previous,
representative,
balance: (before_balance + BigInt(block_info.amount)).toString() as `${number}`,
balance: ((before_balance + BigInt(block_info.amount)).toString() as `${number}`),
//link is hash of send block
link: block_hash,
};
const r_block_hash = util.hash_block(block_ns); //block hash of the receive block
let work = undefined;
if (gen_work) work = await this.work_function(r_block_hash);
if (gen_work && this.work_function) work = await this.work_function(r_block_hash);
const signature = util.sign_block_hash(this.private_key, r_block_hash);
const block = { ...block_ns, signature, work };
return await this.send_process(block, "receive");
Expand All @@ -156,9 +159,9 @@ export class Wallet {
//console.log(e)
//unopened account probably
previous = "0".repeat(64);
representative = this.address;
before_balance = BigInt(0);
}
if (representative === undefined) representative = this.address;
let receive_block_hashes: BlockHash[] = [];
for (const receive_hash of Object.keys(to_receive)) {
const new_balance = (before_balance + BigInt(to_receive[receive_hash].amount)).toString() as `${number}`;
Expand All @@ -173,7 +176,7 @@ export class Wallet {
};
const r_block_hash = util.hash_block(block_ns); //block hash of the receive block
let work = undefined;
if (gen_work) work = await this.work_function(r_block_hash);
if (gen_work && this.work_function) work = await this.work_function(r_block_hash);
const signature = util.sign_block_hash(this.private_key, r_block_hash);
const block = { ...block_ns, signature, work };
await this.send_process(block, "receive");
Expand All @@ -200,7 +203,7 @@ export class Wallet {
};
const c_block_hash = util.hash_block(block_ns); //block hash of the change block
let work = undefined;
if (gen_work) work = await this.work_function(c_block_hash);
if (gen_work && this.work_function) work = await this.work_function(c_block_hash);
const signature = util.sign_block_hash(this.private_key, c_block_hash);
const block = { ...block_ns, signature, work };
return await this.send_process(block, "change");
Expand Down

0 comments on commit 295e180

Please sign in to comment.