simple-nano-wallet
Version:
Benskalz' simple-nano-wallet rewritten in TypeScript with some additional features.
101 lines (100 loc) • 3.99 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RPC = void 0;
const DEFAULT_HEADERS = {
"Content-Type": "application/json",
};
/**
* RPC client for Nano node with fallback server support
*/
class RPC {
constructor(rpcURL, workURL, customHeaders = {}) {
this.rpcURLs = Array.isArray(rpcURL) ? rpcURL : [rpcURL];
this.workURLs = Array.isArray(workURL) ? workURL : [workURL];
this.customHeaders = customHeaders;
}
account_info(account) {
return __awaiter(this, void 0, void 0, function* () {
const params = {
action: "account_info",
account,
representative: "true"
};
return yield this.execute(params);
});
}
work_generate(hash) {
return __awaiter(this, void 0, void 0, function* () {
const params = {
action: "work_generate",
hash
};
const r = yield this.execute(params);
if (r.work === undefined) {
throw new Error(`Work generation failed: ${JSON.stringify(r)}`);
}
return r.work;
});
}
receivable(account) {
return __awaiter(this, void 0, void 0, function* () {
const params = {
action: "pending",
account,
threshold: "1"
};
const r = yield this.execute(params);
return r.blocks;
});
}
process(block, subtype) {
return __awaiter(this, void 0, void 0, function* () {
const params = {
action: "process",
json_block: "true",
subtype,
block
};
return yield this.execute(params);
});
}
execute(params) {
return __awaiter(this, void 0, void 0, function* () {
const isWorkRequest = params.action === "work_generate";
const urls = isWorkRequest ? this.workURLs : this.rpcURLs;
let lastError = null;
for (const url of urls) {
try {
const response = yield fetch(url, {
method: "POST",
headers: Object.assign(Object.assign({}, DEFAULT_HEADERS), this.customHeaders),
body: JSON.stringify(params)
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data = yield response.json();
// Immediately return if we get a successful response
return data;
}
catch (error) {
lastError = error instanceof Error ? error : new Error(String(error));
console.error(`Request to ${url} failed: ${lastError.message}`);
// Continue to next URL on error
}
}
throw new Error(`All ${isWorkRequest ? "work" : "RPC"} servers failed. ` +
`Last error: ${(lastError === null || lastError === void 0 ? void 0 : lastError.message) || "Unknown error"}`);
});
}
}
exports.RPC = RPC;