forked from karlsen-network/node-karlsen-core-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (112 loc) · 4.11 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
'use strict';
const secp256k1 = require('secp256k1-wasm');
const blake2b = require('blake2b-wasm');
var kaspacore = module.exports;
kaspacore.secp256k1 = secp256k1;
// module information
kaspacore.version = 'v' + require('./package.json').version;
kaspacore.versionGuard = function(version) {
if (version !== undefined) {
var message = 'More than one instance of kaspacore-lib found. ' +
'Please make sure to require kaspacore-lib and check that submodules do' +
' not also include their own kaspacore-lib dependency.';
throw new Error(message);
}
};
kaspacore.versionGuard(global._kaspacoreLibVersion);
global._kaspacoreLibVersion = kaspacore.version;
const wasmModulesLoadStatus = new Map();
kaspacore.wasmModulesLoadStatus = wasmModulesLoadStatus;
wasmModulesLoadStatus.set("blake2b", false);
wasmModulesLoadStatus.set("secp256k1", false);
const setWasmLoadStatus = (mod, loaded) => {
//console.log("setWasmLoadStatus:", mod, loaded)
wasmModulesLoadStatus.set(mod, loaded);
let allLoaded = true;
wasmModulesLoadStatus.forEach((loaded, mod) => {
//console.log("wasmModulesLoadStatus:", mod, loaded)
if (!loaded)
allLoaded = false;
})
if (allLoaded)
kaspacore.ready();
}
blake2b.ready(() => {
setWasmLoadStatus("blake2b", true);
})
secp256k1.onRuntimeInitialized = () => {
//console.log("onRuntimeInitialized")
setTimeout(() => {
setWasmLoadStatus("secp256k1", true);
}, 1);
}
secp256k1.onAbort = (error) => {
console.log("secp256k1:onAbort:", error)
}
const deferred = ()=>{
let methods = {};
let promise = new Promise((resolve, reject)=>{
methods = {resolve, reject};
})
Object.assign(promise, methods);
return promise;
}
const readySignal = deferred();
kaspacore.ready = ()=>{
readySignal.resolve(true);
}
kaspacore.initRuntime = ()=>{
return readySignal;
}
// crypto
kaspacore.crypto = {};
kaspacore.crypto.BN = require('./lib/crypto/bn');
kaspacore.crypto.ECDSA = require('./lib/crypto/ecdsa');
kaspacore.crypto.Schnorr = require('./lib/crypto/schnorr');
kaspacore.crypto.Hash = require('./lib/crypto/hash');
kaspacore.crypto.Random = require('./lib/crypto/random');
kaspacore.crypto.Point = require('./lib/crypto/point');
kaspacore.crypto.Signature = require('./lib/crypto/signature');
// encoding
kaspacore.encoding = {};
kaspacore.encoding.Base58 = require('./lib/encoding/base58');
kaspacore.encoding.Base58Check = require('./lib/encoding/base58check');
kaspacore.encoding.BufferReader = require('./lib/encoding/bufferreader');
kaspacore.encoding.BufferWriter = require('./lib/encoding/bufferwriter');
kaspacore.encoding.Varint = require('./lib/encoding/varint');
// utilities
kaspacore.util = {};
kaspacore.util.buffer = require('./lib/util/buffer');
kaspacore.util.js = require('./lib/util/js');
kaspacore.util.preconditions = require('./lib/util/preconditions');
kaspacore.util.base32 = require('./lib/util/base32');
kaspacore.util.convertBits = require('./lib/util/convertBits');
kaspacore.setDebugLevel = (level)=>{
kaspacore.util.js.debugLevel = level;
}
// errors thrown by the library
kaspacore.errors = require('./lib/errors');
// main bitcoin library
kaspacore.Address = require('./lib/address');
kaspacore.Block = require('./lib/block');
kaspacore.MerkleBlock = require('./lib/block/merkleblock');
kaspacore.BlockHeader = require('./lib/block/blockheader');
kaspacore.HDPrivateKey = require('./lib/hdprivatekey.js');
kaspacore.HDPublicKey = require('./lib/hdpublickey.js');
kaspacore.Networks = require('./lib/networks');
kaspacore.Opcode = require('./lib/opcode');
kaspacore.PrivateKey = require('./lib/privatekey');
kaspacore.PublicKey = require('./lib/publickey');
kaspacore.Script = require('./lib/script');
kaspacore.Transaction = require('./lib/transaction');
kaspacore.URI = require('./lib/uri');
kaspacore.Unit = require('./lib/unit');
// dependencies, subject to change
kaspacore.deps = {};
kaspacore.deps.bnjs = require('bn.js');
kaspacore.deps.bs58 = require('bs58');
kaspacore.deps.Buffer = Buffer;
kaspacore.deps.elliptic = require('elliptic');
kaspacore.deps._ = require('lodash');
// Internal usage, exposed for testing/advanced tweaking
kaspacore.Transaction.sighash = require('./lib/transaction/sighash');