add private key account, fix rpcwithbackup

This commit is contained in:
stjet
2024-07-20 21:06:22 +00:00
parent 9086c544aa
commit 2b0dd01f24
68 changed files with 212 additions and 150 deletions

View File

@@ -4,21 +4,20 @@ import type { RPCInterface, RPC } from "./rpc";
export type WorkFunction = (block_hash: BlockHash) => Promise<string>;
/** Wallets are created from seeds, so they can have multiple addresses by changing the index. Use Wallets to "write" (send, receive, change rep) to the network */
/** wallets are created from seeds, so they can have multiple addresses by changing the index. use wallets to "write" (send, receive, change rep) to the network */
export class Wallet {
readonly seed: string;
readonly rpc: RPCInterface;
/** Seed index. Seeds can have multiple private keys and addresses */
index: number;
try_work: boolean;
add_do_work: boolean = true;
work_function?: WorkFunction;
/**
* @param {string} [seed] Seed for the wallet from which private keys are derived. 64 character hex string (32 bytes)
*/
constructor(rpc: RPCInterface, seed: string, index: number = 0, try_work: boolean = false, work_function?: WorkFunction) {
constructor(rpc: RPCInterface, seed: string, index: number = 0, work_function?: WorkFunction) {
this.rpc = rpc;
if (typeof seed !== "string" || seed?.length !== 64) throw Error("Seed needs to be 64 character (hex) string");
this.seed = seed;
@@ -224,3 +223,19 @@ export class Wallet {
return util.sign_message(this.private_key, message);
}
}
/** Does everything a `Wallet` can do, except a private key is put in instead of a seed, and so limited to one address. Means changing `.index` will not do anything obviously. */
export class PrivateKeyAccount extends Wallet {
_private_key: string;
/**
* @param {string} [private_key] Private key. 64 character hex string (32 bytes)
*/
constructor(rpc: RPCInterface, private_key: string, work_function?: WorkFunction) {
if (typeof private_key !== "string" || private_key?.length !== 64) throw Error("Priv key needs to be 64 character (hex) string");
super(rpc, private_key, 0, work_function);
this._private_key = private_key;
}
get private_key(): string {
return this._private_key;
}
}