-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
170 lines (150 loc) · 4.52 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { getRPC, methods } from "@ravenrebels/ravencoin-rpc";
const ONE_FULL_COIN = 1e8;
const URL_MAINNET = "https://rvn-rpc-mainnet.ting.finance/rpc";
const URL_TESTNET = "https://rvn-rpc-testnet.ting.finance/rpc";
let username = "anonymous";
let password = "anonymous";
let url = URL_MAINNET;
let rpc = getRPC(username, password, url);
function resetRPC() {
rpc = getRPC(username, password, url);
return rpc;
}
function setURL(newURL: string) {
url = newURL;
resetRPC();
}
function setUsername(newUsername: string) {
username = newUsername;
resetRPC();
}
function setPassword(newPassword: string) {
password = newPassword;
resetRPC();
}
/**
*
* @param assetName mandatory
* @param onlytotal otional, when false result is just a list of addresses with balances -- when true the result is just a single number representing the number of addresses
* @param count (integer, optional, default=50000, MAX=50000) truncates results to include only the first _count_ assets found
* @param start (integer, optional, default=0) results skip over the first _start_ assets found (if negative it skips back from the end)
*/
function getAddressesByAsset(
assetName: string,
onlytotal?: boolean,
count?: number,
start?: boolean
): Promise<any> {
const _onlytotal = onlytotal === undefined ? false : onlytotal;
let _count = count === undefined ? 5000 : count;
let _start = start === undefined ? 0 : start;
if (_count > 50000) {
_count = 50000;
}
return rpc(methods.listaddressesbyasset, [
assetName,
_onlytotal,
_count,
_start,
]);
}
function getAddressDeltas(address: string | string[]): Promise<any[]> {
const addresses = turnIntoStringArray(address);
const assetName = ""; //Must be empty string, NOT "*"
const deltas = rpc(methods.getaddressdeltas, [{ addresses, assetName }]);
return deltas;
}
function getAddressMempool(address: string | string[]): Promise<any> {
const addresses = turnIntoStringArray(address); //Support both string and string array
const includeAssets = true;
return rpc(methods.getaddressmempool, [
{ addresses: addresses },
includeAssets,
]);
}
function getAddressUTXOs(address: string | string[]): Promise<any> {
const addresses = turnIntoStringArray(address); //Support both string and string array
return rpc(methods.getaddressutxos, [{ addresses: addresses }]);
}
function getAllAssets(
prefix: string = "*",
includeAllMetaData: boolean = false
): Promise<any> {
return rpc(methods.listassets, [prefix, includeAllMetaData]);
}
function getAssetBalance(address: string | string[]): Promise<any> {
const addresses = turnIntoStringArray(address);
const includeAssets = true;
return rpc(methods.getaddressbalance, [
{ addresses: addresses },
includeAssets,
]);
}
function getAsset(name: string): Promise<any> {
return rpc(methods.getassetdata, [name]);
}
function getBestBlockHash(): Promise<string> {
return rpc(methods.getbestblockhash, []);
}
function getBlockByHash(hash: string): Promise<any> {
return rpc(methods.getblock, [hash]);
}
function getBlockByHeight(height: number): Promise<any> {
return rpc(methods.getblockhash, [height]).then((hash) => {
const verbosity = 3; //include transactions
const block = rpc(methods.getblock, [hash, verbosity]);
return block;
});
}
function getMempool() {
return rpc(methods.getrawmempool, [true]);
}
function getRavencoinBalance(address: string | string[]): Promise<any> {
const addresses = turnIntoStringArray(address);
if (!addresses || addresses.length < 1) {
const emptyObject = {};
return Promise.resolve(emptyObject);
}
const includeAssets = false;
const params = [{ addresses: addresses }, includeAssets];
return rpc(methods.getaddressbalance, params);
}
function getTransaction(id: string): Promise<any> {
const verbose = true;
return rpc(methods.getrawtransaction, [id, verbose]);
}
function verifyMessage(
address: string,
signature: string,
message: string
): Promise<boolean> {
const params = [address, signature, message];
return rpc(methods.verifymessage, params);
}
function turnIntoStringArray(str: string | string[]): string[] {
if (typeof str === "string") {
return [str];
}
return str;
}
export default {
getAddressesByAsset,
getAddressDeltas,
getAddressMempool,
getAddressUTXOs,
getAllAssets,
getAsset,
getAssetBalance,
getBestBlockHash,
getBlockByHash,
getBlockByHeight,
getMempool,
getRavencoinBalance,
getTransaction,
setUsername,
setPassword,
setURL,
verifyMessage,
URL_MAINNET,
URL_TESTNET,
};