-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
283 lines (135 loc) · 11.3 KB
/
index.js
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
██╗ ██╗███████╗██████╗ ██╗██████╗ ██████╗ ███████╗
██║ ██║██╔════╝██╔══██╗ ███║╚════██╗╚════██╗╚════██║
██║ █╗ ██║█████╗ ██████╔╝█████╗╚██║ █████╔╝ █████╔╝ ██╔╝
██║███╗██║██╔══╝ ██╔══██╗╚════╝ ██║ ╚═══██╗ ╚═══██╗ ██╔╝
╚███╔███╔╝███████╗██████╔╝ ██║██████╔╝██████╔╝ ██║
╚══╝╚══╝ ╚══════╝╚═════╝ ╚═╝╚═════╝ ╚═════╝ ╚═╝
██████╗██████╗ ███████╗ █████╗ ████████╗███████╗██████╗ ███████╗ ██████╗ ██████╗ ██╗ ██╗██╗ ██╗ ██╗
██╔════╝██╔══██╗██╔════╝██╔══██╗╚══██╔══╝██╔════╝██╔══██╗ ██╔════╝██╔═══██╗██╔══██╗ ██║ ██╔╝██║ ╚██╗ ██╔╝
██║ ██████╔╝█████╗ ███████║ ██║ █████╗ ██║ ██║ █████╗ ██║ ██║██████╔╝ █████╔╝ ██║ ╚████╔╝
██║ ██╔══██╗██╔══╝ ██╔══██║ ██║ ██╔══╝ ██║ ██║ ██╔══╝ ██║ ██║██╔══██╗ ██╔═██╗ ██║ ╚██╔╝
╚██████╗██║ ██║███████╗██║ ██║ ██║ ███████╗██████╔╝ ██║ ╚██████╔╝██║ ██║ ██║ ██╗███████╗██║
╚═════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═╝ ╚══════╝╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚═╝
_____________________________________________ INFO _____________________________________________
Only general API & functionality present here. We'll extend abilities via modules & other packages and so on
You can also use web3.js EVM-compatible API with blockchains in KLY ecosystem that supports KLY-EVM
*/
import * as smartContractsApi from './src/smart_contract_api.js'
import * as txsCreation from './src/txs_creation.js'
import crypto from './crypto_primitives/crypto.js'
import {hash} from 'blake3-wasm'
import fetch from 'node-fetch'
// For future support of WSS
// import WS from 'websocket' // https://github.com/theturtle32/WebSocket-Node
// For proxy support
import {SocksProxyAgent} from 'socks-proxy-agent'
import {HttpsProxyAgent} from 'https-proxy-agent'
const TX_TYPES = {
TX:'TX', // default address <=> address tx
WVM_CONTRACT_DEPLOY:'WVM_CONTRACT_DEPLOY', // deployment of WASM contact to KLY-WVM
WVM_CALL:'WVM_CALL', // call the WASM contact to KLY-WVM
}
const SIGNATURES_TYPES = {
DEFAULT:'D', // Default ed25519
TBLS:'T', // TBLS(threshold sig)
POST_QUANTUM_DILITHIUM:'P/D', // Post-quantum Dilithium(2/3/5,2 used by default)
POST_QUANTUM_BLISS:'P/B', // Post-quantum BLISS
MULTISIG:'M' // Multisig BLS
}
export {TX_TYPES,SIGNATURES_TYPES}
export {crypto}
export default class {
/**
*
* @param {String} [options.chainID] identificator of KLY chain to work with
* @param {Number} [options.workflowVersion] identificator of appropriate version of chain's workflow
* @param {String} [options.nodeURL] endpoint of node to interact with
* @param {String} [options.proxyURL] HTTP(s) / SOCKS proxy url
*
*
*/
constructor(options = {chainID,workflowVersion,nodeURL,proxyURL}){
let {chainID,workflowVersion,nodeURL,proxyURL} = options;
if(proxyURL === 'string'){
if(proxyURL.startsWith('http')) this.proxy = new HttpsProxyAgent(proxyURL) // for example => 'http(s)://login:[email protected]:8080'
else if (proxyURL.startsWith('socks')) this.proxy = new SocksProxyAgent(proxyURL) // for TOR/I2P connections. For example => socks5h://Vlad:[email protected]:9150
}
this.chains = new Map() // chainID => {nodeURL,workflowVersion}
// Set the initial values
this.currentChain = chainID
this.chains.set(chainID,{nodeURL,workflowVersion})
}
blake3=(input,length)=>hash(input,{length}).toString('hex')
getRequestToNode=url=>{
let {nodeURL} = this.chains.get(this.currentChain)
return fetch(nodeURL+url,{agent:this.proxy}).then(r=>r.json()).catch(error=>error)
}
postRequestToNode=(url,bodyToSend)=>{
let {nodeURL} = this.chains.get(this.currentChain)
return fetch(nodeURL+url,{
method:'POST',
body:JSON.stringify(bodyToSend),
agent:this.proxy
}).then(r=>r.json()).catch(error=>error)
}
/*
█████╗ ██████╗ ██╗
██╔══██╗██╔══██╗██║
███████║██████╔╝██║
██╔══██║██╔═══╝ ██║
██║ ██║██║ ██║
╚═╝ ╚═╝╚═╝ ╚═╝
*/
//_________________________Block API_________________________
getBlockByBlockID=blockID=>this.getRequestToNode('/block/'+blockID)
getBlockBySID=(shard,blockHeight)=>this.getRequestToNode(`/block_by_sid/${shard}/${blockHeight}`)
getLatestNBlocksOnShard=(shard,startIndex,limit)=>this.getRequestToNode(`/latest_n_blocks/${shard}/${startIndex}/${limit}`)
getVerificationThreadStats=()=>this.getRequestToNode(`/verification_thread_stats`)
//_______________________Epoch data API______________________
getCurrentEpochOnThread=threadID=>this.getRequestToNode('/current_epoch/'+threadID)
getCurrentLeadersOnShards=()=>this.getRequestToNode(`/current_shards_leaders`)
getEpochDataByEpochIndex=epochIndex=>this.getRequestToNode(`/epoch_by_index/${epochIndex}`)
getVerificationThreadStatsPerEpoch=epochIndex=>this.getRequestToNode(`/verification_thread_stats_per_epoch/${epochIndex}`)
//_______________________State data API____________________
getFromStateByCellID=(shard,cellID)=>this.getRequestToNode(`/state/${shard}/${cellID}`)
getTransactionReceiptById=txID=>this.getRequestToNode('/tx_receipt/'+txID)
getPoolStats=poolID=>this.getRequestToNode('/pool_stats/'+poolID)
getTransactionsWithAccount=(shardID,accountID)=>this.getRequestToNode(`/txs_list/${shardID}/${accountID}`)
getAccount=(shardID,accountID)=>this.getRequestToNode(`/account/${shardID}/${accountID}`)
//_______________________Misc data API_____________________
getTargetNodeInfrastructureInfo=()=>this.getRequestToNode('/infrastructure_info')
getChainData=()=>this.getRequestToNode('/chain_info')
getKlyEvmMetadata=()=>this.getRequestToNode('/kly_evm_metadata')
getSynchronizationStatus=()=>this.getRequestToNode('/synchronization_stats')
getQuorumUrlsAndPubkeys=()=>this.getRequestToNode('/quorum_urls_and_pubkeys')
//___________________Consensus-related API___________________
getAggregatedEpochFinalizationProof=(epochIndex,shard)=>this.getRequestToNode(`/aggregated_epoch_finalization_proof/${epochIndex}/${shard}`)
getAggregatedFinalizationProofForBlock=blockID=>this.getRequestToNode('/aggregated_finalization_proof/'+blockID)
//_____________________________ TXS Creation _____________________________
createEd25519Transaction=(originShard,txType,yourAddress,yourPrivateKey,nonce,fee,payload)=>{
return txsCreation.createEd25519Transaction(this,originShard,txType,yourAddress,yourPrivateKey,nonce,fee,payload)
}
signDataForMultisigTransaction=(originShard,txType,blsPrivateKey,nonce,fee,payload)=>{
return txsCreation.signDataForMultisigTransaction(this,originShard,txType,blsPrivateKey,nonce,fee,payload)
}
createMultisigTransaction=(rootPubKey,txType,aggregatedSignatureOfActive,nonce,fee,payload)=>{
return txsCreation.createMultisigTransaction(this,txType,rootPubKey,aggregatedSignatureOfActive,nonce,fee,payload)
}
buildPartialSignatureWithTxData=(originShard,txType,hexID,sharedPayload,nonce,fee,payload)=>{
return txsCreation.buildPartialSignatureWithTxData(this,originShard,txType,hexID,sharedPayload,nonce,fee,payload)
}
createThresholdTransaction=(tblsRootPubkey,txType,partialSignaturesArray,nonce,fee,payload)=>{
return txsCreation.createThresholdTransaction(this,txType,tblsRootPubkey,partialSignaturesArray,nonce,fee,payload)
}
createPostQuantumTransaction=(originShard,txType,pqcAlgorithm,yourAddress,yourPrivateKey,nonce,fee,payload)=>{
return txsCreation.createPostQuantumTransaction(this,originShard,txType,pqcAlgorithm,yourAddress,yourPrivateKey,nonce,fee,payload)
}
sendTransaction=(transaction)=>txsCreation.sendTransaction(this,transaction)
//_________________________ Smart contracts API __________________________
getContractMetadata=(shardID,contractID)=>smartContractsApi.getContractMetadata(this,shardID,contractID)
getContractStorage=(shardID,contractID,storageName)=>smartContractsApi.getContractStorage(this,shardID,contractID,storageName)
//_________________ To work with other chains _______________
addChain=(chainID,workflowVersion,nodeURL)=>this.chains.set(chainID,{nodeURL,workflowVersion})
changeCurrentChain=chainID=>this.currentChain=chainID
}