Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nanode

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nanode - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

api.d.ts

1078

dist/index.d.ts

@@ -0,838 +1,100 @@

import { API, SendBlock, ReceiveBlock, OpenBlock, ChangeBlock } from './api';
export declare const NanodeRepresentative = "xrb_1nanode8ngaakzbck8smq6ru9bethqwyehomf79sae1k7xd47dkidjqzffeg";
export declare const BounceAddress = "";
export declare type RPCClient = (params: any) => Promise<any>;
export declare function createAxiosClient(apiKey: string, baseURL?: string): RPCClient;
export interface NanoConstructorOptions {
apiKey?: string;
url?: string;
rpcClient?: RPCClient;
debug?: boolean;
}
export default class Nano {
rpc: <Action extends "receive" | "account_balance" | "account_block_count" | "account_get" | "account_history" | "account_info" | "account_key" | "account_representative" | "account_weight" | "accounts_balances" | "accounts_frontiers" | "accounts_pending" | "available_supply" | "block" | "blocks" | "block_account" | "block_count" | "block_count_type" | "block_create" | "blocks_info" | "chain" | "delegators" | "delegators_count" | "deterministic_key" | "frontiers" | "frontier_count" | "key_create" | "key_expand" | "krai_to_raw" | "history" | "ledger" | "pending" | "pending_exists" | "process" | "receive_minimum" | "receive_minimum_set" | "representatives" | "successors" | "work_generate" | "work_cancel" | "work_get">(action: Action, body: {
account_balance: {
body: {
account: string;
};
response: {
balance: string;
pending: string;
};
};
account_block_count: {
body: {
account: string;
};
response: {
block_count: string;
};
};
account_get: {
body: {
key: string;
};
response: {
account: string;
};
};
account_history: {
body: {
account: string;
count?: string;
};
response: any;
};
account_info: {
body: {
account: string;
};
response: {
frontier: string;
open_block: string;
representative_block: string;
balance: string;
modified_timestamp: string;
block_count: string;
};
};
account_key: {
body: {
account: string;
};
response: {
key: string;
};
};
account_representative: {
body: {
account: string;
};
response: {
representative: string;
};
};
account_weight: {
body: {
account: string;
};
response: {
weight: string;
};
};
accounts_balances: {
body: {
accounts: string[];
};
response: {
rpc: <Action extends "receive" | "work_generate" | "account_balance" | "account_block_count" | "account_get" | "account_history" | "account_info" | "account_key" | "account_representative" | "account_weight" | "accounts_balances" | "accounts_frontiers" | "accounts_pending" | "available_supply" | "block" | "blocks" | "block_account" | "block_count" | "block_count_type" | "block_create" | "blocks_info" | "chain" | "delegators" | "delegators_count" | "deterministic_key" | "frontiers" | "frontier_count" | "key_create" | "key_expand" | "krai_to_raw" | "history" | "ledger" | "pending" | "pending_exists" | "process" | "receive_minimum" | "receive_minimum_set" | "representatives" | "successors" | "work_validate">(action: Action, body?: API[Action]["body"]) => Promise<API[Action]["response"]>;
debug: boolean;
constructor(options: NanoConstructorOptions);
_log(message: string): void;
account(private_key: string): {
open: (respresentative?: string, hash?: string) => Promise<{
hash: string;
}>;
send: (amount: string | number, address: string) => Promise<string>;
receive: (hash?: string) => Promise<{
hash: string;
}>;
change: (representative: string) => Promise<{
hash: string;
}>;
rawBalance: () => Promise<{
balance: string;
pending: string;
}>;
nanoBalance: () => Promise<string>;
blockCount: () => Promise<{
block_count: string;
}>;
history: (count?: number) => Promise<{
type: string;
account: string;
hash: string;
amount: string;
}[]>;
info: () => Promise<{
frontier: string;
open_block: string;
representative_block: string;
balance: string;
modified_timestamp: string;
block_count: string;
}>;
publicKey: () => Promise<{
key: string;
}>;
ledger: (count?: number, details?: boolean) => Promise<{
accounts: {
[account: string]: {
frontier: string;
open_block: string;
representative_block: string;
balance: string;
pending: string;
modified_timestamp: string;
block_count: string;
representative?: string;
weight?: string;
pending?: string;
};
};
};
accounts_frontiers: {
body: {
accounts: string[];
};
response: {
frontiers: {
[account: string]: string;
};
};
};
accounts_pending: {
body: {
accounts: string[];
count?: string;
};
response: {
blocks: {
[account: string]: string;
};
};
};
available_supply: {
body: {};
response: {
available: string;
};
};
block: {
body: {
hash: string;
};
response: {
contents: {
type: string;
account: string;
representative: string;
source: string;
work: string;
signature: string;
};
};
};
blocks: {
body: {
hashes: string[];
};
response: {
blocks: {
[account: string]: {
type: string;
account: string;
representative: string;
source: string;
work: string;
signature: string;
};
};
};
};
block_account: {
body: {
hash: string;
};
response: {
account: string;
};
};
block_count: {
body: {};
response: {
count: string;
unchecked: string;
};
};
block_count_type: {
body: {};
response: {
send: string;
receive: string;
open: string;
change: string;
};
};
block_create: {
body: {
type: "open" | "send" | "receive" | "change";
key: string;
previous?: string;
work: string;
account?: string;
representative?: string;
source?: string;
signature?: string;
destination?: string;
balance?: string;
amount?: string;
};
response: {
hash: string;
block: string;
};
};
blocks_info: {
body: {
hashes: string[];
};
response: {
blocks: {
[account: string]: {
contents: {
type: string;
account: string;
representative: string;
source: string;
work: string;
signature: string;
};
block_account: string;
amount: string;
};
};
};
};
chain: {
body: {
block: string;
count: string;
};
};
delegators: {
body: {
account: string;
};
response: {
delegators: {
[account: string]: string;
};
};
};
delegators_count: {
body: {
account: string;
};
response: {
count: string;
};
};
deterministic_key: {
body: {
seed: string;
index: string;
};
response: any;
};
frontiers: {
body: {
account: string;
};
response: {
frontiers: {
[account: string]: string;
};
};
};
frontier_count: {
body: {
account: string;
};
response: {
count: string;
};
};
key_create: {
body: any;
response: {
public: string;
private: string;
account: string;
};
};
key_expand: {
body: any;
response: {
public: string;
private: string;
account: string;
};
};
krai_to_raw: {
body: {
amount: string | number;
};
response: {
amount: string;
};
};
history: {
body: {
hash: string;
count: string;
};
response: {
type: string;
account: string;
hash: string;
amount: string;
}[];
};
ledger: {
body: {
account: string;
count?: string;
representative?: string;
weight?: string;
pending?: string;
};
response: {
accounts: {
[account: string]: {
frontier: string;
open_block: string;
representative_block: string;
balance: string;
modified_timestamp: string;
block_count: string;
representative?: string;
weight?: string;
pending?: string;
};
};
};
};
pending: {
body: {
account: string;
count: string;
};
response: {
blocks: string[];
};
};
pending_exists: {
body: {
hash: string;
};
response: {
exists: "1" | "0";
};
};
process: {
body: {
block: string;
};
response: {
hash: string;
};
};
receive: {
body: {
wallet: string;
account: string;
block: string;
};
response: {
block: string;
};
};
receive_minimum: {
body: {};
response: {
amount: string;
};
};
receive_minimum_set: {
body: {
amount: string;
};
response: {
success: string;
};
};
representatives: {
body: {};
response: {
representatives: {
[account: string]: string;
};
};
};
successors: {
body: {
block: string;
count: string;
};
response: {
blocks: string[];
};
};
work_generate: {
body: {
hash: string;
};
response: {
work: string;
};
};
work_cancel: {
body: {
hash: string;
};
response: {};
};
work_get: {
body: {
wallet: string;
account: string;
};
response: {
work: string;
};
};
}[Action]["body"]) => Promise<{
account_balance: {
body: {
account: string;
};
response: {
balance: string;
pending: string;
};
};
account_block_count: {
body: {
account: string;
};
response: {
block_count: string;
};
};
account_get: {
body: {
key: string;
};
response: {
account: string;
};
};
account_history: {
body: {
account: string;
count?: string;
};
response: any;
};
account_info: {
body: {
account: string;
};
response: {
frontier: string;
open_block: string;
representative_block: string;
balance: string;
modified_timestamp: string;
block_count: string;
};
};
account_key: {
body: {
account: string;
};
response: {
key: string;
};
};
account_representative: {
body: {
account: string;
};
response: {
representative: string;
};
};
account_weight: {
body: {
account: string;
};
response: {
weight: string;
};
};
accounts_balances: {
body: {
accounts: string[];
};
response: {
[account: string]: {
balance: string;
pending: string;
};
};
};
accounts_frontiers: {
body: {
accounts: string[];
};
response: {
frontiers: {
[account: string]: string;
};
};
};
accounts_pending: {
body: {
accounts: string[];
count?: string;
};
response: {
blocks: {
[account: string]: string;
};
};
};
available_supply: {
body: {};
response: {
available: string;
};
};
block: {
body: {
hash: string;
};
response: {
contents: {
type: string;
account: string;
representative: string;
source: string;
work: string;
signature: string;
};
};
};
blocks: {
body: {
hashes: string[];
};
response: {
blocks: {
[account: string]: {
type: string;
account: string;
representative: string;
source: string;
work: string;
signature: string;
};
};
};
};
block_account: {
body: {
hash: string;
};
response: {
account: string;
};
};
block_count: {
body: {};
response: {
count: string;
unchecked: string;
};
};
block_count_type: {
body: {};
response: {
send: string;
receive: string;
open: string;
change: string;
};
};
block_create: {
body: {
type: "open" | "send" | "receive" | "change";
key: string;
previous?: string;
work: string;
account?: string;
representative?: string;
source?: string;
signature?: string;
destination?: string;
balance?: string;
amount?: string;
};
response: {
hash: string;
block: string;
};
};
blocks_info: {
body: {
hashes: string[];
};
response: {
blocks: {
[account: string]: {
contents: {
type: string;
account: string;
representative: string;
source: string;
work: string;
signature: string;
};
block_account: string;
amount: string;
};
};
};
};
chain: {
body: {
block: string;
count: string;
};
};
delegators: {
body: {
account: string;
};
response: {
delegators: {
[account: string]: string;
};
};
};
delegators_count: {
body: {
account: string;
};
response: {
count: string;
};
};
deterministic_key: {
body: {
seed: string;
index: string;
};
response: any;
};
frontiers: {
body: {
account: string;
};
response: {
frontiers: {
[account: string]: string;
};
};
};
frontier_count: {
body: {
account: string;
};
response: {
count: string;
};
};
key_create: {
body: any;
response: {
public: string;
private: string;
account: string;
};
};
key_expand: {
body: any;
response: {
public: string;
private: string;
account: string;
};
};
krai_to_raw: {
body: {
amount: string | number;
};
response: {
amount: string;
};
};
history: {
body: {
hash: string;
count: string;
};
response: {
type: string;
account: string;
hash: string;
amount: string;
}[];
};
ledger: {
body: {
account: string;
count?: string;
representative?: string;
weight?: string;
pending?: string;
};
response: {
accounts: {
[account: string]: {
frontier: string;
open_block: string;
representative_block: string;
balance: string;
modified_timestamp: string;
block_count: string;
representative?: string;
weight?: string;
pending?: string;
};
};
};
};
pending: {
body: {
account: string;
count: string;
};
response: {
blocks: string[];
};
};
pending_exists: {
body: {
hash: string;
};
response: {
exists: "1" | "0";
};
};
process: {
body: {
block: string;
};
response: {
hash: string;
};
};
receive: {
body: {
wallet: string;
account: string;
block: string;
};
response: {
block: string;
};
};
receive_minimum: {
body: {};
response: {
amount: string;
};
};
receive_minimum_set: {
body: {
amount: string;
};
response: {
success: string;
};
};
representatives: {
body: {};
response: {
representatives: {
[account: string]: string;
};
};
};
successors: {
body: {
block: string;
count: string;
};
response: {
blocks: string[];
};
};
work_generate: {
body: {
hash: string;
};
response: {
work: string;
};
};
work_cancel: {
body: {
hash: string;
};
response: {};
};
work_get: {
body: {
wallet: string;
account: string;
};
response: {
work: string;
};
};
}[Action]["response"]>;
debug: boolean;
origin_address?: string;
origin_key?: string;
constructor(options: {
api_key: string;
url: string;
origin_address?: string;
origin_key?: string;
debug?: boolean;
});
log(message: string): void;
call(action: string, body: any): Promise<any>;
open(send_block_hash: string, representative: string, target_private_key?: string, target_public_key?: string): Promise<{
}>;
pending: (count?: number, threshold?: string) => Promise<{
blocks: string[];
}>;
representative: () => Promise<string>;
weight: () => Promise<string>;
};
open(privateKey: string, representative?: string, sendBlockHash?: string): Promise<{
hash: string;
}>;
send(amount: string, recipient_wallet_address: string, origin_private_key?: string, origin_account_address?: string): Promise<string>;
receive(send_block_hash: string, recipient_private_key?: string, recipient_wallet_address?: string): Promise<{
send(privateKey: string, amount: string | number, toAddress: string): Promise<string>;
receive(privateKey: string, sendBlockHash?: string): Promise<{
hash: string;
}>;
change(previous: string, representative: string, target_private_key?: string, target_public_key?: string): Promise<{
change(privateKey: string, representative: string): Promise<{
hash: string;
}>;
readonly account: {
get(key: string): Promise<{
generateLatestWork(privateKey: string): Promise<{
address: any;
balance: string;
frontier: string;
work: string;
}>;
readonly accounts: {
get(publicKey: string): Promise<{
account: string;
}>;
balance(account?: string): Promise<{
rawBalance(account: string): Promise<{
balance: string;
pending: string;
}>;
balances(accounts: string[], count?: string): Promise<{
nanoBalance(account: string): Promise<string>;
balances(accounts: string[]): Promise<{
[account: string]: {

@@ -843,6 +105,6 @@ balance: string;

}>;
block_count(account?: string): Promise<{
blockCount(account: string): Promise<{
block_count: string;
}>;
frontiers(accounts: string[], count?: string): Promise<{
frontiers(accounts: string[]): Promise<{
frontiers: {

@@ -852,4 +114,9 @@ [account: string]: string;

}>;
history(account?: string, count?: string): Promise<any>;
info(account?: string): Promise<{
history(account: string, count?: number): Promise<{
type: string;
account: string;
hash: string;
amount: string;
}[]>;
info(account: string): Promise<{
frontier: string;

@@ -865,3 +132,3 @@ open_block: string;

}>;
ledger(account: string, count?: number, representative?: boolean, weight?: boolean, pending?: boolean): Promise<{
ledger(account: string, count?: number, details?: boolean): Promise<{
accounts: {

@@ -881,18 +148,16 @@ [account: string]: {

}>;
pending(accountOrAccounts: string | string[], count?: number, threshold?: string): Promise<{
pending(account: string, count?: number, minNanoThreshold?: string | number): Promise<{
blocks: string[];
}>;
pendingMulti(accounts: string[], count?: number, minNanoThreshold?: string | number): Promise<{
blocks: {
[account: string]: string;
};
} | {
blocks: string[];
}>;
representative(account: string): Promise<string>;
wieght(account: string): Promise<string>;
weight(account: string): Promise<string>;
};
readonly block: {
readonly blocks: {
account(hash: string): Promise<string>;
count(by_type?: string): Promise<{
count: string;
unchecked: string;
} | {
count(byType?: boolean): Promise<{
send: string;

@@ -902,14 +167,12 @@ receive: string;

change: string;
}> | Promise<{
count: number;
unchecked: string;
}>;
chain(block: string, count?: string): Promise<any>;
change(block: {
previous: string;
key: string;
work: string;
representative: string;
}): Promise<{
chain(block: string, count?: number): Promise<any>;
createChange(block: ChangeBlock): Promise<{
hash: string;
block: string;
}>;
history(hash: string, count?: string): Promise<{
history(hash: string, count?: number): Promise<{
type: string;

@@ -920,9 +183,33 @@ account: string;

}[]>;
open(block: {
key: string;
info(hashOrHashes: string | string[], details?: boolean): Promise<{
[account: string]: {
contents: {
type: string;
account: string;
representative: string;
source: string;
work: string;
signature: string;
};
block_account: string;
amount: string;
};
}> | Promise<{
[account: string]: {
type: string;
account: string;
representative: string;
source: string;
work: string;
signature: string;
};
}> | Promise<{
type: string;
account: string;
representative: string;
source: string;
previous?: string;
representative: string;
work: string;
}): Promise<{
signature: string;
}>;
createOpen(block: OpenBlock): Promise<{
hash: string;

@@ -935,64 +222,21 @@ block: string;

}>;
receive(block: {
key: string;
account: string;
previous: string;
work: string;
source: string;
}): Promise<{
createReceive(block: ReceiveBlock): Promise<{
hash: string;
block: string;
}>;
send(block: {
key: string;
account: string;
destination: string;
balance: string;
amount: string;
previous: string;
work: string;
}): Promise<{
createSend(block: SendBlock): Promise<{
hash: string;
block: string;
}>;
successors(block: string, count?: number): Promise<{
blocks: string[];
}>;
};
blocks(hashOrHashes: string | string[], details: boolean): Promise<{
type: string;
account: string;
representative: string;
source: string;
work: string;
signature: string;
} | {
[account: string]: {
type: string;
account: string;
representative: string;
source: string;
work: string;
signature: string;
};
} | {
[account: string]: {
contents: {
type: string;
account: string;
representative: string;
source: string;
work: string;
signature: string;
};
block_account: string;
amount: string;
};
}>;
readonly convert: {
toRaw(amount: number, denomination: "krai" | "mrai" | "rai"): Promise<any>;
fromRaw(amount: number, denomination: "krai" | "mrai" | "rai"): Promise<any>;
toRaw(amount: string | number, denomination: "mrai" | "krai" | "rai"): string;
fromRaw(amount: string, denomination: "mrai" | "krai" | "rai"): string;
};
readonly delegators: {
get(account: string): Promise<{
delegators: {
[account: string]: string;
};
[account: string]: string;
}>;

@@ -1002,3 +246,3 @@ count(account: string): Promise<string>;

readonly frontiers: {
get(account: string, count?: string): Promise<{
get(account: string, count?: number): Promise<{
frontiers: {

@@ -1008,14 +252,14 @@ [account: string]: string;

}>;
count(account: string): Promise<string>;
count(): Promise<string>;
};
readonly key: {
create(): Promise<{
public: string;
private: string;
account: string;
privateKey: string;
publicKey: string;
address: string;
}>;
expand(key: string): Promise<{
public: string;
private: string;
account: string;
expand(privateKey: string): Promise<{
privateKey: string;
publicKey: string;
address: string;
}>;

@@ -1027,5 +271,4 @@ };

}>;
cancel(hash: string): Promise<{}>;
get(wallet: string, account: string): Promise<{
work: string;
validate(work: string, hash: string): Promise<{
valid: string;
}>;

@@ -1037,8 +280,13 @@ };

}>;
get_deterministic_key(seed: string): Promise<any>;
deterministicKey(seed: string, index?: number): Promise<{
private: string;
public: string;
account: string;
}>;
readonly minimumReceive: {
get(): Promise<string>;
set(amount: string): Promise<boolean>;
get(): Promise<{
amount: string;
}>;
set(nanoAmount: string | number): Promise<boolean>;
};
successors(block: string, count?: number): Promise<string[]>;
}

@@ -47,3 +47,19 @@ "use strict";

var axios_1 = require("axios");
function createAPI(baseURL, apiKey) {
var accountPair = require('./util/util.js').accountPair;
var converter_1 = require("./util/converter");
exports.NanodeRepresentative = 'xrb_1nanode8ngaakzbck8smq6ru9bethqwyehomf79sae1k7xd47dkidjqzffeg';
exports.BounceAddress = '';
function createAPI(rpcClient) {
return function callRPC(action, body) {
return __awaiter(this, void 0, void 0, function () {
var params;
return __generator(this, function (_a) {
params = Object.assign({}, body || {}, { action: action });
return [2 /*return*/, rpcClient(params)];
});
});
};
}
function createAxiosClient(apiKey, baseURL) {
if (baseURL === void 0) { baseURL = 'https://api.nanode.co/'; }
var rpc = axios_1.default.create({

@@ -55,23 +71,11 @@ baseURL: baseURL,

});
return function callRPC(action, body) {
return function (params) {
return __awaiter(this, void 0, void 0, function () {
var request, result, err_1;
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
request = Object.assign({}, body, { action: action });
_a.label = 1;
case 0: return [4 /*yield*/, rpc.post('/', params)];
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, rpc.post('/', request)];
case 2:
result = _a.sent();
if (result && result.data) {
return [2 /*return*/, result.data];
}
throw new Error(result.statusText);
case 3:
err_1 = _a.sent();
throw new Error(err_1.message);
case 4: return [2 /*return*/];
data = (_a.sent()).data;
return [2 /*return*/, data];
}

@@ -82,17 +86,17 @@ });

}
exports.createAxiosClient = createAxiosClient;
var Nano = /** @class */ (function () {
function Nano(options) {
this.rpc = createAPI(null, null);
if (!options.api_key) {
throw new Error('Must pass api_key to constructor');
this.rpc = createAPI(null);
this.debug = !!options.debug;
if (options.rpcClient) {
this.rpc = createAPI(options.rpcClient);
}
if (!options.url) {
throw new Error('Must past RPC URL to constructor');
else {
var rpcClient = createAxiosClient(options.apiKey, options.url);
this.rpc = createAPI(rpcClient);
}
this.debug = !!options.debug;
this.rpc = createAPI(options.url, options.api_key);
this.origin_address = options.origin_address;
this.origin_key = options.origin_key;
this._log = this._log.bind(this);
}
Nano.prototype.log = function (message) {
Nano.prototype._log = function (message) {
if (this.debug) {

@@ -102,45 +106,91 @@ console.log(message);

};
Nano.prototype.call = function (action, body) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.rpc(action, body)];
});
});
Nano.prototype.account = function (private_key) {
var _this = this;
var address = accountPair(private_key).address;
return {
open: function (respresentative, hash) {
return _this.open(private_key, respresentative, hash);
},
send: function (amount, address) {
return _this.send(private_key, amount, address);
},
receive: function (hash) {
return _this.receive(private_key, hash);
},
change: function (representative) {
return _this.change(private_key, representative);
},
rawBalance: function () {
return _this.accounts.rawBalance(address);
},
nanoBalance: function () {
return _this.accounts.nanoBalance(address);
},
blockCount: function () {
return _this.accounts.blockCount(address);
},
history: function (count) {
return _this.accounts.history(address, count);
},
info: function () {
return _this.accounts.info(address);
},
publicKey: function () {
return _this.accounts.key(address);
},
ledger: function (count, details) {
return _this.accounts.ledger(address, count, details);
},
pending: function (count, threshold) {
return _this.accounts.pending(address, count, threshold);
},
representative: function () {
return _this.accounts.representative(address);
},
weight: function () {
return _this.accounts.weight(address);
}
};
};
//Top-level call: open block
Nano.prototype.open = function (send_block_hash, representative, target_private_key, target_public_key) {
Nano.prototype.open = function (privateKey, representative, sendBlockHash) {
return __awaiter(this, void 0, void 0, function () {
var log, private_key, work, block, result, err_2;
return __generator(this, function (_a) {
switch (_a.label) {
var _log, _a, address, publicKey, work, res, block, result;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
log = this.log;
private_key = this.origin_key || target_private_key;
if (!private_key) {
throw new Error('Must pass origin_key in either open call or constructor');
_log = this._log;
if (!privateKey) {
throw new Error('Must pass privateKey argument');
}
_a.label = 1;
if (!representative) {
representative = exports.NanodeRepresentative;
}
_a = accountPair(privateKey), address = _a.address, publicKey = _a.publicKey;
return [4 /*yield*/, this.work.generate(publicKey)];
case 1:
_a.trys.push([1, 5, , 6]);
return [4 /*yield*/, this.work.generate(target_public_key)];
work = (_b.sent()).work;
if (!!sendBlockHash) return [3 /*break*/, 3];
return [4 /*yield*/, this.accounts.pending(address, 1)];
case 2:
work = _a.sent();
return [4 /*yield*/, this.block.open({
previous: target_public_key,
key: target_private_key,
source: send_block_hash,
work: work.work,
representative: representative
})];
case 3:
block = _a.sent();
return [4 /*yield*/, this.block.publish(block.block)];
res = _b.sent();
if (!res.blocks || res.blocks.length === 0) {
throw new Error('This account has no pending blocks to receive');
}
sendBlockHash = res.blocks[0];
_b.label = 3;
case 3: return [4 /*yield*/, this.blocks.createOpen({
previous: publicKey,
key: privateKey,
source: sendBlockHash,
work: work,
representative: representative
})];
case 4:
result = _a.sent();
log("Opened NANO block " + result.hash + " with rep. " + representative + "!");
block = _b.sent();
return [4 /*yield*/, this.blocks.publish(block.block)];
case 5:
result = _b.sent();
_log("Opened NANO account " + address + " with block " + result.hash + " and representative " + representative + "!");
return [2 /*return*/, result];
case 5:
err_2 = _a.sent();
throw new Error("open failed: " + err_2.message);
case 6: return [2 /*return*/];
}

@@ -151,49 +201,31 @@ });

//Top-level call: send block
Nano.prototype.send = function (amount, recipient_wallet_address, origin_private_key, origin_account_address) {
Nano.prototype.send = function (privateKey, amount, toAddress) {
return __awaiter(this, void 0, void 0, function () {
var log, origin_wallet, private_key, account, work, rai_to_send, block, result, err_3;
return __generator(this, function (_a) {
switch (_a.label) {
var _log, _a, balance, frontier, work, amountRaw, block, result;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
log = this.log;
_a.label = 1;
_log = this._log;
if (!privateKey) {
throw new Error('Must pass private_key argument');
}
return [4 /*yield*/, this.generateLatestWork(privateKey)];
case 1:
_a.trys.push([1, 7, , 8]);
origin_wallet = this.origin_address || origin_account_address;
if (!origin_wallet) {
throw new Error('Must pass origin_account_address in either send or constructor');
}
private_key = this.origin_key || origin_private_key;
if (!private_key) {
throw new Error('Must pass origin_account_address in either send or constructor');
}
return [4 /*yield*/, this.account.info(origin_wallet)];
_a = _b.sent(), balance = _a.balance, frontier = _a.frontier, work = _a.work;
amountRaw = converter_1.default.unit(amount, 'NANO', 'raw');
return [4 /*yield*/, this.blocks.createSend({
key: privateKey,
destination: toAddress,
balance: balance,
amount: amountRaw,
previous: frontier,
work: work
})];
case 2:
account = _a.sent();
return [4 /*yield*/, this.work.generate(account.frontier)];
block = _b.sent();
return [4 /*yield*/, this.blocks.publish(block.block)];
case 3:
work = _a.sent();
return [4 /*yield*/, this.convert.toRaw(+amount * 1000, 'krai')];
case 4:
rai_to_send = _a.sent();
return [4 /*yield*/, this.block.send({
key: private_key,
account: origin_wallet,
destination: recipient_wallet_address,
balance: account.balance,
amount: rai_to_send.amount,
previous: account.frontier,
work: work.work
})];
case 5:
block = _a.sent();
return [4 /*yield*/, this.block.publish(block.block)];
case 6:
result = _a.sent();
log("Sent " + account.balance + " NANO to " + recipient_wallet_address + "!");
result = _b.sent();
_log("Sent " + amountRaw + " NANO to " + toAddress + "!");
return [2 /*return*/, result.hash];
case 7:
err_3 = _a.sent();
throw new Error("Nano.send failed: " + err_3.message);
case 8: return [2 /*return*/];
}

@@ -204,44 +236,37 @@ });

//Top-level call: receive block
Nano.prototype.receive = function (send_block_hash, recipient_private_key, recipient_wallet_address) {
Nano.prototype.receive = function (privateKey, sendBlockHash) {
return __awaiter(this, void 0, void 0, function () {
var log, receiving_wallet, private_key, account, work, block, result, err_4;
return __generator(this, function (_a) {
switch (_a.label) {
var _log, _a, address, frontier, work, res, block, result;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
log = this.log;
_a.label = 1;
_log = this._log;
if (!privateKey) {
throw new Error('Must pass privateKey argument');
}
return [4 /*yield*/, this.generateLatestWork(privateKey)];
case 1:
_a.trys.push([1, 6, , 7]);
receiving_wallet = this.origin_address || recipient_wallet_address;
if (!receiving_wallet) {
throw new Error('Must pass recipient_wallet_address in either receive call or constructor');
_a = _b.sent(), address = _a.address, frontier = _a.frontier, work = _a.work;
if (!!sendBlockHash) return [3 /*break*/, 3];
return [4 /*yield*/, this.accounts.pending(address, 1)];
case 2:
res = _b.sent();
if (!res.blocks || res.blocks.length === 0) {
throw new Error('This account has no pending blocks to receive');
}
private_key = this.origin_key || recipient_private_key;
if (!private_key) {
throw new Error('Must pass origin_key in either receive call or constructor');
}
return [4 /*yield*/, this.account.info(receiving_wallet)];
case 2:
account = _a.sent();
return [4 /*yield*/, this.work.generate(account.frontier)];
case 3:
work = _a.sent();
return [4 /*yield*/, this.block.receive({
key: private_key,
account: receiving_wallet,
previous: account.frontier,
work: work.work,
source: send_block_hash
})];
sendBlockHash = res.blocks[0];
_b.label = 3;
case 3: return [4 /*yield*/, this.blocks.createReceive({
key: privateKey,
previous: frontier,
work: work,
source: sendBlockHash
})];
case 4:
block = _a.sent();
return [4 /*yield*/, this.block.publish(block.block)];
block = _b.sent();
return [4 /*yield*/, this.blocks.publish(block.block)];
case 5:
result = _a.sent();
log("Received " + account.balance + " NANO block " + send_block_hash + " to wallet " + receiving_wallet + "!");
result = _b.sent();
_log("Received block " + sendBlockHash + " to wallet " + address + "!");
return [2 /*return*/, result];
case 6:
err_4 = _a.sent();
throw new Error("Nano.send failed: " + err_4.message);
case 7: return [2 /*return*/];
}

@@ -252,32 +277,28 @@ });

//Top-level call: change block
Nano.prototype.change = function (previous, representative, target_private_key, target_public_key) {
Nano.prototype.change = function (privateKey, representative) {
return __awaiter(this, void 0, void 0, function () {
var log, work, block, result, err_5;
return __generator(this, function (_a) {
switch (_a.label) {
var _log, _a, frontier, work, block, result;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
log = this.log;
_a.label = 1;
_log = this._log;
if (!privateKey) {
throw new Error('Must pass privateKey argument');
}
return [4 /*yield*/, this.generateLatestWork(privateKey)];
case 1:
_a.trys.push([1, 5, , 6]);
return [4 /*yield*/, this.work.generate(previous)];
case 2:
work = _a.sent();
return [4 /*yield*/, this.block.change({
previous: previous,
_a = _b.sent(), frontier = _a.frontier, work = _a.work;
return [4 /*yield*/, this.blocks.createChange({
previous: frontier,
representative: representative,
work: work.work,
key: target_private_key
work: work,
key: privateKey
})];
case 2:
block = _b.sent();
return [4 /*yield*/, this.blocks.publish(block.block)];
case 3:
block = _a.sent();
return [4 /*yield*/, this.block.publish(block.block)];
case 4:
result = _a.sent();
log("Opened NANO block " + result.hash + " with rep. " + representative + "!");
result = _b.sent();
_log("Opened NANO block " + result.hash + " with rep. " + representative + "!");
return [2 /*return*/, result];
case 5:
err_5 = _a.sent();
throw new Error("open failed: " + err_5.message);
case 6: return [2 /*return*/];
}

@@ -287,202 +308,96 @@ });

};
Object.defineProperty(Nano.prototype, "account", {
Nano.prototype.generateLatestWork = function (privateKey) {
return __awaiter(this, void 0, void 0, function () {
var address, _a, balance, frontier, work;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
address = accountPair(privateKey).address;
return [4 /*yield*/, this.accounts.info(address)];
case 1:
_a = _b.sent(), balance = _a.balance, frontier = _a.frontier;
return [4 /*yield*/, this.work.generate(frontier)];
case 2:
work = (_b.sent()).work;
return [2 /*return*/, {
address: address,
balance: balance,
frontier: frontier,
work: work
}];
}
});
});
};
Object.defineProperty(Nano.prototype, "accounts", {
//General account methods
get: function () {
var _a = this, rpc = _a.rpc, log = _a.log;
var _a = this, rpc = _a.rpc, _log = _a._log;
return {
get: function (key) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
if (!key) {
throw new Error("Must pass key to constructor, or account name to this method");
}
return [2 /*return*/, rpc('account_get', { key: key })
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("account.get failed: " + err.message);
})];
});
});
get: function (publicKey) {
return rpc('account_get', { key: publicKey });
},
balance: function (account) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
account = this.origin_address || account;
if (!account) {
throw new Error("Must pass origin_address to constructor, or account name to this method");
}
return [2 /*return*/, rpc('account_balance', { account: account })
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("account.balance failed: " + err.message);
})];
});
});
rawBalance: function (account) {
return rpc('account_balance', { account: account });
},
balances: function (accounts, count) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('accounts_balances', {
accounts: accounts
})
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("account.balances failed: " + err.message);
})];
});
nanoBalance: function (account) {
return rpc('account_balance', { account: account }).then(function (balance) {
return converter_1.default.unit(balance.balance, 'raw', 'NANO');
});
},
block_count: function (account) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
account = this.origin_address || account;
if (!account) {
throw new Error("Must pass origin_address to constructor, or account name to this method");
}
return [2 /*return*/, rpc('account_block_count', {
account: account
})
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("account.block_count failed: " + err.message);
})];
});
});
balances: function (accounts) {
return rpc('accounts_balances', { accounts: accounts });
},
frontiers: function (accounts, count) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('accounts_frontiers', {
accounts: accounts
})
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("account.frontiers failed: " + err.message);
})];
});
});
blockCount: function (account) {
return rpc('account_block_count', { account: account });
},
frontiers: function (accounts) {
return rpc('accounts_frontiers', { accounts: accounts });
},
history: function (account, count) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
account = this.origin_address || account;
if (!account) {
throw new Error("Must pass origin_address to constructor, or account name to this method");
}
return [2 /*return*/, rpc('account_history', {
account: account,
count: count || '1'
})
.then(function (res) { return res.data; })
.catch(function (err) {
throw new Error("account.pending failed: " + err.message);
})];
});
});
return rpc('account_history', {
account: account,
count: count || 1000
}).then(function (res) { return res.history; });
},
info: function (account) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
account = this.origin_address || account;
if (!account) {
throw new Error("Must pass origin_address to constructor, or account name to this method");
}
return [2 /*return*/, rpc('account_info', { account: account })
.then(function (account) {
log("(ACCOUNT) balance: " + account.balance);
log("(ACCOUNT) latest hash: " + account.frontier);
return account;
})
.catch(function (err) {
throw new Error("account.info failed: " + err.message);
})];
});
return rpc('account_info', { account: account }).then(function (account) {
_log("(ACCOUNT) balance: " + account.balance);
_log("(ACCOUNT) latest hash: " + account.frontier);
return account;
});
},
key: function (account) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('account_key', { account: account })
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("account.key failed: " + err.message);
})];
});
return rpc('account_key', { account: account });
},
ledger: function (account, count, details) {
return rpc('ledger', {
account: account,
count: count || 1000,
representative: details,
weight: details,
pending: details
});
},
ledger: function (account, count, representative, weight, pending) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
account = this.origin_address || account;
if (!account) {
throw new Error("Must pass origin_address to constructor, or account name to this method");
}
return [4 /*yield*/, rpc('ledger', {
account: account,
count: count.toString() || '1',
representative: (!!representative).toString(),
weight: (!!weight).toString(),
pending: (!!pending).toString()
})
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("ledger failed: " + err.message);
})];
case 1: return [2 /*return*/, _a.sent()];
}
});
pending: function (account, count, minNanoThreshold) {
return rpc('pending', {
account: account,
threshold: converter_1.default.unit(minNanoThreshold || 0, 'NANO', 'raw'),
count: count || 1000
});
},
pending: function (accountOrAccounts, count, threshold) {
return __awaiter(this, void 0, void 0, function () {
var getMulti;
return __generator(this, function (_a) {
getMulti = typeof accountOrAccounts === 'array';
return [2 /*return*/, getMulti
? rpc('accounts_pending', {
accounts: accountOrAccounts,
threshold: threshold,
count: count.toString() || '1'
})
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("account.accounts_pending failed: " + err.message);
})
: rpc('pending', {
account: accountOrAccounts,
threshold: threshold,
count: count.toString() || '1'
})
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("account.pending failed: " + err.message);
})];
});
pendingMulti: function (accounts, count, minNanoThreshold) {
return rpc('accounts_pending', {
accounts: accounts,
threshold: converter_1.default.unit(minNanoThreshold || 0, 'NANO', 'raw'),
count: count || 1000
});
},
representative: function (account) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('account_representative', {
account: account
})
.then(function (res) { return res.representative; })
.catch(function (err) {
throw new Error("account.representative failed: " + err.message);
})];
});
});
return rpc('account_representative', {
account: account
}).then(function (res) { return res.representative; });
},
wieght: function (account) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('account_weight', { account: account })
.then(function (res) { return res.weight; })
.catch(function (err) {
throw new Error("account.weight failed: " + err.message);
})];
});
});
weight: function (account) {
return rpc('account_weight', { account: account }).then(function (res) { return res.weight; });
}

@@ -494,145 +409,80 @@ };

});
Object.defineProperty(Nano.prototype, "block", {
//General block related calls
Object.defineProperty(Nano.prototype, "blocks", {
get: function () {
var _a = this, rpc = _a.rpc, log = _a.log;
var _a = this, rpc = _a.rpc, _log = _a._log;
return {
account: function (hash) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('block_account', { hash: hash })
.then(function (res) {
return res.account;
})
.catch(function (err) {
throw new Error("block.account failed: " + err.message);
})];
});
});
return rpc('block_account', { hash: hash }).then(function (res) { return res.account; });
},
count: function (by_type) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, by_type
? rpc('block_count_type', {})
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("block.count_type failed: " + err.message);
})
: rpc('block_count', {})
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("block.count failed: " + err.message);
})];
});
});
count: function (byType) {
return byType ? rpc('block_count_type') : rpc('block_count');
},
chain: function (block, count) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('chain', {
block: block,
count: count || '1'
})
.then(function (res) { return res.blocks; })
.catch(function (err) {
throw new Error("block.chain failed: " + err.message);
})];
});
});
return rpc('chain', {
block: block,
count: count || 1000
}).then(function (res) { return res.blocks; });
},
change: function (block) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('block_create', __assign({ type: 'change' }, block))
.then(function (res) {
log("(BLOCK) Changing " + block.key);
return res;
})
.catch(function (err) {
throw new Error("block.change failed: " + err.message);
})];
});
createChange: function (block) {
return rpc('block_create', __assign({ type: 'change' }, block)).then(function (res) {
_log("(BLOCK) Changing " + block.key);
return res;
});
},
history: function (hash, count) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('history', {
hash: hash,
count: count || '0'
})
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("block.change failed: " + err.message);
})];
});
return rpc('history', {
hash: hash,
count: count || 1000
});
},
open: function (block) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('block_create', __assign({ type: 'open' }, block))
.then(function (res) {
log("(BLOCK) Opening " + block.key);
return res;
})
.catch(function (err) {
throw new Error("block.open failed: " + err.message);
})];
});
//Get one or many block's information
info: function (hashOrHashes, details) {
var getMulti = typeof hashOrHashes === 'array';
if (getMulti) {
return details
? rpc('blocks_info', {
hashes: hashOrHashes
}).then(function (res) { return res.blocks; })
: rpc('blocks', {
hashes: hashOrHashes
}).then(function (res) { return res.blocks; });
}
else {
return rpc('block', {
hash: hashOrHashes
}).then(function (res) { return res.contents; });
}
},
createOpen: function (block) {
return rpc('block_create', __assign({ type: 'open' }, block)).then(function (res) {
_log("(BLOCK) Opening " + block.key);
return res;
});
},
pending: function (hash) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('pending_exists', { hash: hash })
.then(function (res) { return res.exists === '1'; })
.catch(function (err) {
throw new Error("block.change failed: " + err.message);
})];
});
});
return rpc('pending_exists', { hash: hash }).then(function (res) { return res.exists === '1'; });
},
publish: function (block) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('process', { block: block })
.then(function (res) {
log("(BLOCK) Published: " + res.hash);
return res;
})
.catch(function (err) {
throw new Error("block.publish failed: " + err.message);
})];
});
return rpc('process', { block: block }).then(function (res) {
_log("(BLOCK) Published: " + res.hash);
return res;
});
},
receive: function (block) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('block_create', __assign({ type: 'receive' }, block))
.then(function (res) {
log("Received block " + block.source);
return res;
})
.catch(function (err) {
throw new Error("block.receive failed: " + err.message);
})];
});
createReceive: function (block) {
return rpc('block_create', __assign({ type: 'receive' }, block)).then(function (res) {
_log("Received block " + block.source);
return res;
});
},
send: function (block) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('block_create', __assign({ type: 'send' }, block))
.then(function (res) {
log("(BLOCK) Sending " + block.amount + " from " + block.account + " to " + block.destination);
return res;
})
.catch(function (err) {
throw new Error("block.send failed: " + err.message);
})];
});
createSend: function (block) {
return rpc('block_create', __assign({ type: 'send' }, block)).then(function (res) {
_log("(BLOCK) Sending " + block.amount + " to " + block.destination);
return res;
});
},
successors: function (block, count) {
return rpc('successors', {
block: block,
count: count || 1000
});
}

@@ -644,83 +494,10 @@ };

});
//Get one or many block's information
Nano.prototype.blocks = function (hashOrHashes, details) {
return __awaiter(this, void 0, void 0, function () {
var rpc, getMulti;
return __generator(this, function (_a) {
rpc = this.rpc;
getMulti = typeof hashOrHashes === 'array';
if (getMulti) {
return [2 /*return*/, details
? rpc('blocks_info', {
hashes: hashOrHashes
})
.then(function (res) { return res.blocks; })
.catch(function (err) {
throw new Error("blocks.get failed: " + err.message);
})
: rpc('blocks', {
hashes: hashOrHashes
})
.then(function (res) { return res.blocks; })
.catch(function (err) {
throw new Error("blocks.get failed: " + err.message);
})];
}
else {
return [2 /*return*/, rpc('block', {
hash: hashOrHashes
})
.then(function (res) { return res.contents; })
.catch(function (err) {
throw new Error("block.get failed: " + err.message);
})];
}
return [2 /*return*/];
});
});
};
Object.defineProperty(Nano.prototype, "convert", {
//Convert KRAI, MRAI, RAI to and from RAW
get: function () {
var rpc = this.rpc;
return {
toRaw: function (amount, denomination) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!amount) {
throw new Error('Must pass amount to conversion call');
}
return [4 /*yield*/, rpc(denomination + "_to_raw", {
amount: amount.toString()
})
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("convert." + denomination + "_to_rai failed: " + err.message);
})];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
return converter_1.default.unit(amount, denomination, 'raw');
},
fromRaw: function (amount, denomination) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!amount) {
throw new Error('Must pass amount to conversion call');
}
return [4 /*yield*/, rpc(denomination + "_from_raw", {
amount: amount.toString()
})
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("convert." + denomination + "_from_raw failed: " + err.message);
})];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
return converter_1.default.unit(amount, 'raw', denomination);
}

@@ -733,4 +510,2 @@ };

Object.defineProperty(Nano.prototype, "delegators", {
//get, count delegators
//TODO: could be a single method
get: function () {

@@ -740,22 +515,6 @@ var rpc = this.rpc;

get: function (account) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('delegators', { account: account })
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("delegators.get failed: " + err.message);
})];
});
});
return rpc('delegators', { account: account }).then(function (res) { return res.delegators; });
},
count: function (account) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('delegators_count', { account: account })
.then(function (res) { return res.count; })
.catch(function (err) {
throw new Error("delegators.count failed: " + err.message);
})];
});
});
return rpc('delegators_count', { account: account }).then(function (res) { return res.count; });
}

@@ -768,4 +527,2 @@ };

Object.defineProperty(Nano.prototype, "frontiers", {
//Get, count frontiers
//TODO: could be a single method
get: function () {

@@ -775,25 +532,9 @@ var rpc = this.rpc;

get: function (account, count) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('frontiers', {
account: account,
count: count || '1'
})
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("frontiers.get failed: " + err.message);
})];
});
return rpc('frontiers', {
account: account,
count: count || 1000
});
},
count: function (account) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('frontier_count', { account: account })
.then(function (res) { return res.count; })
.catch(function (err) {
throw new Error("frontiers.count failed: " + err.message);
})];
});
});
count: function () {
return rpc('frontier_count').then(function (res) { return res.count; });
}

@@ -806,30 +547,19 @@ };

Object.defineProperty(Nano.prototype, "key", {
//Create and expand keys
get: function () {
var _a = this, rpc = _a.rpc, log = _a.log;
var rpc = this.rpc;
// The word 'private' is reserved in JS so we use this function
// to get around that, and to make 'address' more clear
function convertKeyObj(keyObj) {
return {
privateKey: keyObj.private,
publicKey: keyObj.public,
address: keyObj.account
};
}
return {
create: function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('key_create', {})
.then(function (res) {
log("Created key " + res);
return res;
})
.catch(function (err) {
throw new Error("key.create failed: " + err.message);
})];
});
});
return rpc('key_create').then(convertKeyObj);
},
expand: function (key) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('key_expand', {})
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("key.expand failed: " + err.message);
})];
});
});
expand: function (privateKey) {
return rpc('key_expand', { key: privateKey }).then(convertKeyObj);
}

@@ -842,47 +572,14 @@ };

Object.defineProperty(Nano.prototype, "work", {
//Generate, cancel, and get work
//Generate and get work
get: function () {
var _a = this, rpc = _a.rpc, log = _a.log;
var _a = this, rpc = _a.rpc, _log = _a._log;
return {
generate: function (hash) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('work_generate', { hash: hash })
.then(function (result) {
log("(WORK) generated PoW: " + result.work);
return result;
})
.catch(function (err) {
throw new Error("work.generate failed: " + err.message);
})];
});
return rpc('work_generate', { hash: hash }).then(function (result) {
_log("(WORK) generated PoW: " + result.work);
return result;
});
},
cancel: function (hash) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('work_cancel', { hash: hash })
.then(function (result) {
log("(WORK) cancelled PoW for " + hash);
return result;
})
.catch(function (err) {
throw new Error("work.cancel failed: " + err.message);
})];
});
});
},
get: function (wallet, account) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('work_get', { wallet: wallet, account: account })
.then(function (result) {
log("(WORK) retrieved PoW: " + result.work);
return result;
})
.catch(function (err) {
throw new Error("work.get failed: " + err.message);
})];
});
});
validate: function (work, hash) {
return rpc('work_validate', { work: work, hash: hash });
}

@@ -895,37 +592,11 @@ };

Nano.prototype.available = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.rpc('available_supply', {})
.then(function (res) { return res.available; })
.catch(function (err) {
throw new Error("available failed: " + err.message);
})];
});
});
return this.rpc('available_supply').then(function (res) { return res.available; });
};
Nano.prototype.representatives = function () {
return __awaiter(this, void 0, void 0, function () {
var rpc;
return __generator(this, function (_a) {
rpc = this.rpc;
return [2 /*return*/, rpc('representatives', {})
.then(function (res) { return res.representatives; })
.catch(function (err) {
throw new Error("representatives failed: " + err.message);
})];
});
});
return this.rpc('representatives').then(function (res) { return res.representatives; });
};
Nano.prototype.get_deterministic_key = function (seed) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.rpc('deterministic_key', {
seed: seed,
index: '0'
})
.then(function (res) { return res; })
.catch(function (err) {
throw new Error("get_dertiministic_key failed: " + err.message);
})];
});
Nano.prototype.deterministicKey = function (seed, index) {
return this.rpc('deterministic_key', {
seed: seed,
index: '0'
});

@@ -938,24 +609,8 @@ };

get: function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('receive_minimum', {})
.then(function (res) { return res.amount; })
.catch(function (err) {
throw new Error("available failed: " + err.message);
})];
});
});
return rpc('receive_minimum');
},
set: function (amount) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, rpc('receive_minimum_set', {
amount: amount
})
.then(function (res) { return res.success === ''; })
.catch(function (err) {
throw new Error("available failed: " + err.message);
})];
});
});
set: function (nanoAmount) {
return rpc('receive_minimum_set', {
amount: converter_1.default.unit(nanoAmount, 'NANO', 'raw')
}).then(function (res) { return res.success === ''; });
}

@@ -967,18 +622,2 @@ };

});
Nano.prototype.successors = function (block, count) {
return __awaiter(this, void 0, void 0, function () {
var rpc;
return __generator(this, function (_a) {
rpc = this.rpc;
return [2 /*return*/, rpc('successors', {
block: block,
count: count ? count.toString() : '1'
})
.then(function (res) { return res.blocks; })
.catch(function (err) {
throw new Error("representatives failed: " + err.message);
})];
});
});
};
return Nano;

@@ -985,0 +624,0 @@ }());

{
"name": "nanode",
"version": "1.0.1",
"description":
"Dead simple, promise-based client for interacting and building services on top of the NANO network.",
"version": "2.0.0",
"description": "Dead simple, promise-based client for interacting and building services on top of the NANO network.",
"main": "./dist/index.js",

@@ -24,4 +23,6 @@ "types": "./dist/index.d.ts",

"dependencies": {
"@types/bignumber.js": "5.0.0",
"@types/node": "^9.4.0",
"axios": "^0.16.2",
"bignumber.js": "6.0.0",
"dotenv": "^5.0.0",

@@ -28,0 +29,0 @@ "moment": "^2.20.1",

@@ -1,4 +0,4 @@

# Nano Client
# Nano Client by [Nanode](https://www.nanode.co/)
Dead simple, promise-based client for interacting and building services on top of the NANO network, a next-generation cryptocurrency created by Colin LeMahieu with nearly instant transactions and no fees. [Learn more on the official repo](https://nanode.co/node-api) ⚡️
Dead simple, promise-based client for interacting and building services on top of the NANO network, a next-generation cryptocurrency created by Colin LeMahieu with nearly instant transactions and no fees. [Learn more in the Nanode docs.](https://www.nanode.co/docs) ⚡️

@@ -12,6 +12,9 @@ If you've worked with NANO before, you probably have experienced it's learning curve. The community is amazingly helpful and growing fast,

1. abstracts away some of the idiosyncracies and quirks with the RPC API,
2. exposes some top-level methods like `Nano.send()` and `Nano.recieve()`, so you don't have to know about creating, signing, publishing, etc.
2. exposes some easy top-level account methods like `send()` and `recieve()` that do everything automatically so you don't have to know about Proof of Work, creating, signing, publishing, etc
3. allows you to specify amounts in Nano without dealing with "raw" 128-bit integers
Want to build with NANO, but not running a full node? [Sign up for node access and get your API key at nanode.co](https://nanode.co/node-api)
This library works natively with the [Nanode Node API](https://www.nanode.co/node-api) as well as the [official Nano node software](https://github.com/nanocurrency/raiblocks), so there's no vendor lock-in.
[Sign up for Node API to get 1,000 free API calls per month!](https://www.nanode.co/node-api) It's the easiest way to build with Nano.
## Install

@@ -21,69 +24,52 @@

## Usage
## Getting Started
This library is built with TypeScript, and I highly reccommend you take advantage of your code editor's Intellisense features. All fields on requests and responses for the RPC are strings - and for now, the same is true for this library.
This library is built with TypeScript, and I highly reccommend you take advantage of your code editor's Intellisense features.
The full code for these snippets can be found /examples directory
Note: All "amount" parameters must be passed in as strings. ex: `"0.01"` is equivalent to 0.01 Nano.
### Connect
### Nanode Node API
Initiate the client with your API key and a valid RPC url. Optionally pass your address and private key to the constructor.
```typescript
import Nano from 'nanode'
const nano = new Nano({apiKey: process.env.NANODE_API_KEY})
```
Not sure what your private key is? Call `nano.get_deterministic_key()` with your account's seed to get all relevant account information.
### Your own Nano RPC server
```typescript
//examples/init.ts
import Nano from 'nanode'
const nano = new Nano({
api_key: process.env.API_KEY,
url: 'https://api.nanode.co',
origin_address: process.env.SENDER_WALLET, // wallet for the 'controlling' account
origin_key: process.env.SENDER_WALLET_PRIVATE_KEY // key for the 'controlling' account
})
const nano = new Nano({url: 'http://localhost:7076'})
```
### Open account
## Working with accounts
Now that we're connected, we need to 'open' the account. Opening an account is a bit of a catch-22 - it requires any amount to be sent to the address from a funded account before account is actually open.
### Generate an account
It's easy to generate a new random account. You'll get the account's private and public keys along with its address.
```typescript
//examples/open.ts
const {privateKey, publicKey, address} = await nano.key.create()
```
import Nano from './init'
//create a new key
const target = await Nano.key.create()
### Open account
console.log(`Initializing account ${target.account} with funds..`)
In order to open an account and let the network know it exists, we'll need publish an `open` block. An account can't be opened with zero balance, so we'll first need to send some Nano to our account's address from our own wallet or [NanoFaucet](https://www.nanofaucet.org/), then call `open()`.
//send new account init funds from our walet
const hash = await Nano.send('0.01', target.account)
//open block with send's hash
const hash = await Nano.open(
hash,
process.env.DEFAULT_REP,
target.private,
target.public
)
console.log(`Open block published with hash: ${hash}`)
```typescript
await nano.account(PRIVATE_KEY).open()
```
### Send and receive
### Send funds
Sending and receiving are simple one liners. For receive, we have to pass in the hash of the block we're receiving
```typescript
//examples/send.ts, examples/receive.ts
import Nano from './init'
await nano.account(PRIVATE_KEY).send(0.01, RECIPIENT_ADDRESS)
```
const wallet_addr =
'xrb_3hk1e77fbkr67fwzswc31so7zi76g7poek9fwu1jhqxrn3r9wiohwt5hi4p1'
### Receive funds
const hash = await Nano.send('0.01', wallet_addr)
The `receive()` method will automatically receive the latest pending block for the given account.
return await Nano.receive(
hash,
process.env.RECIPIENT_WALLET_PRIVATE_KEY,
wallet_addr
)
```typescript
await nano.account(PRIVATE_KEY).receive()
```

@@ -93,71 +79,108 @@

If you aren't sure about some of the arguments, they're available as types in your editor, or in the official RPC guide. Full TypeDoc documentation is on the way!
All methods return native or Bluebird promises and are fully compatible with `async/await`.
### Top-level calls
### Working with a specific account
if you only need to send and recieve NANO, **these methods should technically be the only ones you need**, per the examples above:
If you're just looking to transact with Nano, these methods will cover 90% of your use case.
* `Nano.open()`
* `Nano.send()`
* `Nano.receive()`
* `Nano.change()`
```typescript
const account = nano.account(PRIVATE_KEY)
```
### Account
* `account.open(representative?: string, hash?: string)`
* `account.send(nanoAmount: string | number, address: string)`
* `account.receive(hash?: string)`
* `account.change(representative: string)`
* `account.rawBalance()`
* `account.nanoBalance()`
* `account.blockCount()`
* `account.history(count?: number)`
* `account.info()`
* `account.publicKey()`
* `account.ledger(count?: number, details?: boolean)`
* `account.pending(count?: number, minNanoThreshold?: string | number)`
* `account.representative()`
* `account.weight()`
Account methods take a single account string or in some cases, an array of accounts.
### Keys
* `Nano.account.get()`
* `Nano.account.balance()`
* `Nano.account.balances()`
* `Nano.account.block_count()`
* `Nano.account.frontiers()`
* `Nano.account.history()`
* `Nano.account.info()`
* `Nano.account.key()`
* `Nano.account.ledger()`
* `Nano.account.pending()`
* `Nano.account.representative()`
* `Nano.account.weight()`
Used for generating accounts and extrapolating public keys/addresses from private keys.
### Block
* `nano.key.create()`
* `nano.key.expand(privateKey: string)`
Block methods either require a block hash as a single argument, or a stringified block:
### Accounts
* `Nano.block.account(block_hash)`
* `Nano.block.count(block_hash)`
* `Nano.block.chain(block_hash)`
* `Nano.block.history(block_hash)`
* `Nano.block.pending(block_hash)`
* `Nano.block.change(block_string)`
* `Nano.block.open(block_string)`
* `Nano.block.publish(block_string)`
* `Nano.block.receive(block_string)`
* `Nano.block.send(block_string)`
Account methods take a single account string or in some cases, an array of accounts.
* `nano.accounts.get(publicKey: string)`
* `nano.accounts.balance(account: string)`
* `nano.accounts.balances(accounts: string[])`
* `nano.accounts.blockCount(account: string)`
* `nano.accounts.frontiers(accounts: string[])`
* `nano.accounts.history(account: string, count?: number)`
* `nano.accounts.info(account: string)`
* `nano.accounts.key(account: string)`
* `nano.accounts.ledger(account: string, count?: number, details?: boolean)`
* `nano.accounts.pending(account: string, count?: number, minNanoThreshold?: string | number)`
* `nano.accounts.pendingMulti(accounts: string[], count?: number, minNanoThreshold?: string | number)`
* `nano.accounts.representative(account: string)`
* `nano.accounts.weight(account: string)`
### Blocks
This utility method allows a block hash or array of block hashes -
Has methods to get information about blocks:
* `Nano.blocks.find()`
* `nano.blocks.account(hash: string)`
* `nano.blocks.count(byType?: boolean)`
* `nano.blocks.chain(hash: string, count?: number)`
* `nano.blocks.history(hash: string, count?: number)`
* `nano.blocks.info(hashOrHahes: string | string[], details?: boolean)`
* `nano.blocks.pending(hash: string)`
* `nano.blocks.successors(block: string, count?: number)`
Methods to construct blocks:
* `nano.blocks.createOpen(block: OpenBlock)`
* `nano.blocks.createSend(block: SendBlock)`
* `nano.blocks.createReceive(block: ReceiveBlock)`
* `nano.blocks.createChange(block: ChangeBlock)`
And a method to publish a constructed block to the network:
* `nano.blocks.publish(block: string)`
### Convert
The convert method allows you to convert krai, mrai, and rai to and from their raw values. Both take an amount and denomination.
Allows you to convert `rai`, `krai`, and `mrai` amounts to and from their raw values.
* `Nano.convert.toRaw(22, 'mrai')`
* `Nano.convert.fromRaw(22, 'mrai')`
* `nano.convert.toRaw(amount: string | number, denomination: 'rai' | 'krai' | 'mrai')`
* `nano.convert.fromRaw(amount: string, denomination: 'rai' | 'krai' | 'mrai')`
### Work
Exposes work methods to perform on hashes
Allows you to generate and validate Proof of Work for a given block hash.
* `Nano.work.generate(block_hash)`
* `Nano.work.cancel(block_hash)`
* `Nano.work.get(wallet, account)`
* `nano.work.generate(hash: string)`
* `nano.work.validate(work: string, hash: string)`
### Other
* `nano.available()`
* `nano.representatives()`
* `nano.deterministicKey(seed: string, index?: number)`
* `nano.minimumReceive.get()`
* `nano.minimumReceive.set(nanoAmount: string | number)`
## Calling RPC directly
If there's a method missing, or if you prefer to call RPC directly, you can use `nano.rpc`. You'll still get the full benefit of type checking and return types for applicable RPC calls.
```typescript
await nano.rpc('account_info', {account})
```
## Todos
* Use BigNumber, etc. to allow passing numbers
* TypeDoc site + add remaining method documentation
* Better argument checking / error handling
* TypeDoc site + add remaining method types
* Tests!

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc