bsv2 2.0.15 → 2.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bsv.bundle.js +1 -1
- package/dist/bsv.bundle.js.map +1 -1
- package/dist/bsv.cjs.js +1 -1
- package/dist/bsv.cjs.js.map +1 -1
- package/dist/bsv.module.js +1 -1
- package/dist/bsv.module.js.map +1 -1
- package/dist/bsv2.bundle.js +3 -0
- package/dist/bsv2.bundle.js.LICENSE.txt +10 -0
- package/dist/bsv2.bundle.js.map +1 -0
- package/dist/bsv2.cjs.js +3 -0
- package/dist/bsv2.cjs.js.LICENSE.txt +1 -0
- package/dist/bsv2.cjs.js.map +1 -0
- package/dist/bsv2.mjs +3 -0
- package/dist/bsv2.mjs.LICENSE.txt +10 -0
- package/dist/bsv2.mjs.map +1 -0
- package/dist/bsv2.module.js +3 -0
- package/dist/bsv2.module.js.LICENSE.txt +1 -0
- package/dist/bsv2.module.js.map +1 -0
- package/lib/bip-39.js +15 -5
- package/lib/tx-builder.js +4 -9
- package/mtest/test.html +15 -0
- package/mtest/test.js +2 -0
- package/mtest/test.mjs +2 -0
- package/package.json +7 -5
- package/webpack.config.js +20 -5
- package/build/bsv.js +0 -3
- package/dist/bsv.min.js +0 -2
- package/jsdoc.json +0 -5
- package/lib/rabin.js +0 -300
- package/mkdocs.yml +0 -37
- package/webpack.config.js_entry +0 -57
- package/yarn-error.log +0 -6199
package/jsdoc.json
DELETED
package/lib/rabin.js
DELETED
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
//https://github.com/oraclesv/oracledb/blob/master/src/rabin/utils.js
|
|
2
|
-
|
|
3
|
-
//https://github.com/oraclesv/oracledb/blob/master/src/rabin/rabin.js
|
|
4
|
-
|
|
5
|
-
// utils.js
|
|
6
|
-
// Helper functions
|
|
7
|
-
function checkIfValidHexString(hexString) {
|
|
8
|
-
if(typeof hexString !== 'string')
|
|
9
|
-
return false;
|
|
10
|
-
let re = new RegExp('^(0x|0X)?[a-fA-F0-9]+$');
|
|
11
|
-
return re.test(hexString);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function decimalToHexString(number) {
|
|
15
|
-
if(typeof(number) !== 'bigint' && isNaN(number))
|
|
16
|
-
throw ("Error: Argument %s should be a Number or BigInt", number);
|
|
17
|
-
|
|
18
|
-
if (number < 0) {
|
|
19
|
-
number = 0xFFFFFFFF + number + 1;
|
|
20
|
-
}
|
|
21
|
-
return '0x'+number.toString(16);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function hexStringToDecimal(hexString) {
|
|
25
|
-
if (!checkIfValidHexString(hexString))
|
|
26
|
-
throw ("Error: Hex %s should be hexadecimal with or without '0x' at the beginning.", hexString);
|
|
27
|
-
// Remove 0x from string if necessary
|
|
28
|
-
hexString = hexString.replace('0x', '');
|
|
29
|
-
|
|
30
|
-
var i, j, digits = [0],
|
|
31
|
-
carry;
|
|
32
|
-
for (i = 0; i < hexString.length; i += 1) {
|
|
33
|
-
carry = parseInt(hexString.charAt(i), 16);
|
|
34
|
-
for (j = 0; j < digits.length; j += 1) {
|
|
35
|
-
digits[j] = digits[j] * 16 + carry;
|
|
36
|
-
carry = digits[j] / 10 | 0;
|
|
37
|
-
digits[j] %= 10;
|
|
38
|
-
}
|
|
39
|
-
while (carry > 0) {
|
|
40
|
-
digits.push(carry % 10);
|
|
41
|
-
carry = carry / 10 | 0;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return digits.reverse().join('');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function hexStringToBigInt(hexString){
|
|
48
|
-
return BigInt(hexStringToDecimal(hexString));
|
|
49
|
-
}
|
|
50
|
-
// Test functions
|
|
51
|
-
/**
|
|
52
|
-
* Returns a random integer between min (inclusive) and max (inclusive).
|
|
53
|
-
* The value is no lower than min (or the next integer greater than min
|
|
54
|
-
* if min isn't an integer) and no greater than max (or the next integer
|
|
55
|
-
* lower than max if max isn't an integer).
|
|
56
|
-
* Using Math.round() will give you a non-uniform distribution!
|
|
57
|
-
*/
|
|
58
|
-
function getRandomInt(min, max) {
|
|
59
|
-
min = Math.ceil(min);
|
|
60
|
-
max = Math.floor(max);
|
|
61
|
-
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// Random hex string generator
|
|
65
|
-
function getRandomHex(len) {
|
|
66
|
-
let output = '';
|
|
67
|
-
for (let i = 0; i < len; ++i) {
|
|
68
|
-
output += (Math.floor(Math.random() * 16)).toString(16);
|
|
69
|
-
}
|
|
70
|
-
return output;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function sign(value) {
|
|
74
|
-
if (value > 0n) {
|
|
75
|
-
return 1n;
|
|
76
|
-
}
|
|
77
|
-
if (value < 0n) {
|
|
78
|
-
return -1n;
|
|
79
|
-
}
|
|
80
|
-
return 0n;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function bigIntAbsoluteValue(value) {
|
|
84
|
-
if (sign(value) === -1n) {
|
|
85
|
-
return -value;
|
|
86
|
-
}
|
|
87
|
-
else return value;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
/*
|
|
92
|
-
A Rabin Signature JavaScript module adapted
|
|
93
|
-
from: https://github.com/scrypt-sv/rabin/blob/master/rabin.py
|
|
94
|
-
*/
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const { toBigIntLE } = require('bigint-buffer');
|
|
100
|
-
const { checkIfValidHexString,
|
|
101
|
-
hexStringToBigInt,
|
|
102
|
-
bigIntAbsoluteValue } = require('./utils');
|
|
103
|
-
let crypto;
|
|
104
|
-
try {
|
|
105
|
-
crypto = require('crypto');
|
|
106
|
-
} catch (err) {
|
|
107
|
-
throw('Error: crypto support is disabled!');
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function greatestCommonDivisor(a, b) {
|
|
111
|
-
if ((typeof a !== 'bigint') || (typeof b !== 'bigint'))
|
|
112
|
-
throw "Error: Incorrect argument(s) to greatestCommonDivisor()";
|
|
113
|
-
|
|
114
|
-
a = bigIntAbsoluteValue(a);
|
|
115
|
-
b = bigIntAbsoluteValue(b);
|
|
116
|
-
if (b > a) {
|
|
117
|
-
let t = a;
|
|
118
|
-
a = b;
|
|
119
|
-
b = t;
|
|
120
|
-
}
|
|
121
|
-
while (b > 0) {
|
|
122
|
-
let t = b;
|
|
123
|
-
b = a % b;
|
|
124
|
-
a = t;
|
|
125
|
-
}
|
|
126
|
-
return a;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// Calculates: base^exponent % modulus
|
|
130
|
-
function powerMod(base, exponent, modulus) {
|
|
131
|
-
if (modulus === 1n) return 0n;
|
|
132
|
-
let result = 1n;
|
|
133
|
-
base = base % modulus;
|
|
134
|
-
while (exponent > 0n) {
|
|
135
|
-
if (exponent % 2n === 1n) //odd number
|
|
136
|
-
result = (result * base) % modulus;
|
|
137
|
-
exponent = exponent >> 1n; //divide by 2
|
|
138
|
-
base = (base * base) % modulus;
|
|
139
|
-
}
|
|
140
|
-
return result;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function rabinHashBytes(bytes) {
|
|
144
|
-
hBytes = crypto.createHash('sha256').update(bytes).digest();
|
|
145
|
-
let idx = hBytes.byteLength / 2;
|
|
146
|
-
let hl = crypto.createHash('sha256').update(hBytes.slice(0, idx)).digest();
|
|
147
|
-
let hr = crypto.createHash('sha256').update(hBytes.slice(idx, hBytes.byteLength)).digest();
|
|
148
|
-
return toBigIntLE(Buffer.concat([hl, hr]));
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function calculateNextPrime(p) {
|
|
152
|
-
smallPrimesProduct = 3n * 5n * 7n * 11n * 13n * 17n * 19n * 23n * 29n;
|
|
153
|
-
while (greatestCommonDivisor(p, smallPrimesProduct) != 1) {
|
|
154
|
-
p = p + 4n;
|
|
155
|
-
}
|
|
156
|
-
if (powerMod(2n, p - 1n, p) != 1n) {
|
|
157
|
-
return calculateNextPrime(p + 4n);
|
|
158
|
-
}
|
|
159
|
-
if (powerMod(3n, p - 1n, p) != 1n) {
|
|
160
|
-
return calculateNextPrime(p + 4n);
|
|
161
|
-
}
|
|
162
|
-
if (powerMod(5n, p - 1n, p) != 1n) {
|
|
163
|
-
return calculateNextPrime(p + 4n);
|
|
164
|
-
}
|
|
165
|
-
if (powerMod(17n, p - 1n, p) != 1n) {
|
|
166
|
-
return calculateNextPrime(p + 4n);
|
|
167
|
-
}
|
|
168
|
-
return p;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
function getPrimeNumber(p) {
|
|
172
|
-
while (p % 4n != 3n) {
|
|
173
|
-
p = p + 1n;
|
|
174
|
-
}
|
|
175
|
-
return calculateNextPrime(p);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const paddingBuffer = Buffer.from('00', 'hex');
|
|
179
|
-
|
|
180
|
-
function root(dataBuffer, p, q, nRabin) {
|
|
181
|
-
let sig, x, paddingByteCount = 0;
|
|
182
|
-
while (true) {
|
|
183
|
-
x = rabinHashBytes(dataBuffer) % nRabin;
|
|
184
|
-
sig = powerMod(p, q - 2n, q) * p * powerMod(x, (q + 1n) / 4n, q);
|
|
185
|
-
sig = (powerMod(q, p - 2n, p) * q * powerMod(x, (p + 1n) / 4n, p) + sig) % (nRabin);
|
|
186
|
-
if (((sig * sig) % nRabin) === x) {
|
|
187
|
-
break;
|
|
188
|
-
}
|
|
189
|
-
dataBuffer = Buffer.concat([dataBuffer, paddingBuffer]);
|
|
190
|
-
paddingByteCount++;
|
|
191
|
-
}
|
|
192
|
-
return {
|
|
193
|
-
"signature": sig,
|
|
194
|
-
"paddingByteCount": paddingByteCount
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Calculates Key nRabin (public key) from private key parts p & q
|
|
200
|
-
* @param {BigInt} p Key private key 'p' part
|
|
201
|
-
* @param {BigInt} q Key private key 'q' part
|
|
202
|
-
* @returns {BigInt} Key nRabin (public key) = p * q
|
|
203
|
-
*/
|
|
204
|
-
function privKeyToPubKey(p,q){
|
|
205
|
-
if(typeof(p) !== 'bigint' || typeof(q) !== 'bigint')
|
|
206
|
-
throw("Error: Key parts (p,q) should be BigInts (denoted by trailing 'n').")
|
|
207
|
-
return p * q;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Generates Private Key p & q parts from a PRNG seed
|
|
212
|
-
* @returns {JSON} {'p': BigInt,'q': BigInt}
|
|
213
|
-
*/
|
|
214
|
-
function generatePrivKey() {
|
|
215
|
-
// Get a seed value from a random buffer and convert it to a BigInt
|
|
216
|
-
let seed = crypto.randomBytes(2048);
|
|
217
|
-
return generatePrivKeyFromSeed(seed);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Generates Private Key p & q parts from Seed
|
|
222
|
-
* @param {Buffer} seed
|
|
223
|
-
* @returns {JSON} {'p': BigInt,'q': BigInt}
|
|
224
|
-
*/
|
|
225
|
-
function generatePrivKeyFromSeed(seed) {
|
|
226
|
-
let p = getPrimeNumber(rabinHashBytes(Buffer.from(seed, 'hex')) % ((2n ** 501n) + 1n));
|
|
227
|
-
let q = getPrimeNumber(rabinHashBytes(Buffer.from(seed + '00', 'hex')) % ((2n ** 501n) + 1n));
|
|
228
|
-
return {
|
|
229
|
-
"p": p,
|
|
230
|
-
"q": q
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Creates a Rabin signature of hexadecimal data with a given key's values
|
|
236
|
-
* @param {String} dataHex Hexadecimal data string value
|
|
237
|
-
* @param {BigInt} p Key 'p' value
|
|
238
|
-
* @param {BigInt} q Key 'q' value
|
|
239
|
-
* @param {BigInt} nRabin Key nRabin value
|
|
240
|
-
* @returns {JSON} {"signature": BigInt, "paddingByteCount": Number} Signature and padding count
|
|
241
|
-
*/
|
|
242
|
-
function sign(dataHex, p, q, nRabin) {
|
|
243
|
-
// Check if data is valid hex
|
|
244
|
-
if (!checkIfValidHexString(dataHex))
|
|
245
|
-
throw ("Error: dataHex %s should be a hexadecimal String with or without '0x' at the beginning.", dataHex);
|
|
246
|
-
// Remove 0x from data if necessary
|
|
247
|
-
dataHex = dataHex.replace('0x', '');
|
|
248
|
-
// Check key parts are correct values
|
|
249
|
-
if(typeof(p) !== 'bigint' || typeof(q) !== 'bigint' || typeof(nRabin) !== 'bigint')
|
|
250
|
-
throw("Error: Key parts (p,q) should be BigInts (denoted by trailing 'n').")
|
|
251
|
-
return root(Buffer.from(dataHex, 'hex'), p, q, nRabin);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Verifies a Rabin signature of hexadecimal data with given padding count, signature and key nRabin value
|
|
256
|
-
* @param {String} dataHex Hexadecimal data string value
|
|
257
|
-
* @param {Number} paddingByteCount Padding byte count
|
|
258
|
-
* @param {BigInt} signature Rabin signature value
|
|
259
|
-
* @param {BigInt} nRabin Public Key nRabin value
|
|
260
|
-
* @returns {Boolean} If signature is valid or not
|
|
261
|
-
*/
|
|
262
|
-
function verify(dataHex, paddingByteCount, signature, nRabin) {
|
|
263
|
-
// Check if data is valid hex
|
|
264
|
-
if (!checkIfValidHexString(dataHex))
|
|
265
|
-
throw ("Error: Data %s should be a hexadecimal String with or without '0x' at the beginning.", dataHex);
|
|
266
|
-
// Remove 0x from data if necessary
|
|
267
|
-
dataHex = dataHex.replace('0x', '');
|
|
268
|
-
// Ensure padding count is a number
|
|
269
|
-
if(typeof paddingByteCount !== 'number')
|
|
270
|
-
throw ("Error: paddingByteCount should be a number");
|
|
271
|
-
// Check if signature is a BigInt
|
|
272
|
-
if(typeof(signature) !== 'bigint')
|
|
273
|
-
throw("Error: Signature should be a BigInt (denoted by trailing 'n').");
|
|
274
|
-
// Check if nRabin is a BigInt
|
|
275
|
-
if(typeof(nRabin) !== 'bigint')
|
|
276
|
-
throw("Error: Public Key nRabin should be a BigInt (denoted by trailing 'n').");
|
|
277
|
-
|
|
278
|
-
let dataBuffer = Buffer.from(dataHex, 'hex');
|
|
279
|
-
let paddingBuffer = Buffer.from('00'.repeat(paddingByteCount), 'hex');
|
|
280
|
-
let paddedDataBuffer = Buffer.concat([dataBuffer, paddingBuffer]);
|
|
281
|
-
let dataHash = rabinHashBytes(paddedDataBuffer);
|
|
282
|
-
let hashMod = dataHash % nRabin;
|
|
283
|
-
return hashMod === (signature ** 2n % nRabin);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
module.exports = {
|
|
287
|
-
|
|
288
|
-
checkIfValidHexString,
|
|
289
|
-
decimalToHexString,
|
|
290
|
-
hexStringToDecimal,
|
|
291
|
-
hexStringToBigInt,
|
|
292
|
-
bigIntAbsoluteValue,
|
|
293
|
-
getRandomInt,
|
|
294
|
-
getRandomHex,
|
|
295
|
-
generatePrivKey,
|
|
296
|
-
generatePrivKeyFromSeed,
|
|
297
|
-
privKeyToPubKey,
|
|
298
|
-
sign,
|
|
299
|
-
verify,
|
|
300
|
-
}
|
package/mkdocs.yml
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
site_name: bsv 1.5.6
|
|
2
|
-
site_url: https://bsv.direct/docs/1.5.6
|
|
3
|
-
theme:
|
|
4
|
-
name: material
|
|
5
|
-
palette:
|
|
6
|
-
primary: blue
|
|
7
|
-
# logo: assets/logo.png
|
|
8
|
-
# favicon: images/favicon.png
|
|
9
|
-
features:
|
|
10
|
-
- navigation.tabs
|
|
11
|
-
- navigation.tabs.sticky
|
|
12
|
-
- navigation.tracking
|
|
13
|
-
repo_url: https://github.com/iden3/circom
|
|
14
|
-
markdown_extensions:
|
|
15
|
-
- pymdownx.arithmatex:
|
|
16
|
-
generic: true
|
|
17
|
-
extra_javascript:
|
|
18
|
-
- javascripts/mathjax.js
|
|
19
|
-
- https://polyfill.io/v3/polyfill.min.js?features=es6
|
|
20
|
-
- https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js
|
|
21
|
-
extra:
|
|
22
|
-
social:
|
|
23
|
-
# - icon: fontawesome/brands/telegram
|
|
24
|
-
# link: https://t.me/iden3io
|
|
25
|
-
# - icon: fontawesome/brands/github
|
|
26
|
-
# link: https://github.com/iden3/circom
|
|
27
|
-
# - icon: octicons/browser-24
|
|
28
|
-
# link: https://iden3.io/circom
|
|
29
|
-
|
|
30
|
-
nav:
|
|
31
|
-
- Getting started:
|
|
32
|
-
- Block: 'block.md'
|
|
33
|
-
- Ecies: 'ecies.md'
|
|
34
|
-
- Networks: 'networks.md'
|
|
35
|
-
- Script: 'script.md'
|
|
36
|
-
- Transaction: 'transaction.md'
|
|
37
|
-
- Unspent Output: 'unspentoutput.md'
|
package/webpack.config.js_entry
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
const path = require('path')
|
|
2
|
-
const webpack = require('webpack')
|
|
3
|
-
// const pkg = require('package.json')
|
|
4
|
-
|
|
5
|
-
module.exports = [
|
|
6
|
-
{
|
|
7
|
-
entry: './entry.js',
|
|
8
|
-
target: 'web',
|
|
9
|
-
output: {
|
|
10
|
-
path: path.resolve(__dirname, 'dist'),
|
|
11
|
-
filename: 'bsv.bundle.js',
|
|
12
|
-
library: {
|
|
13
|
-
name: 'bsvjs',
|
|
14
|
-
type: 'umd2'
|
|
15
|
-
}
|
|
16
|
-
},
|
|
17
|
-
resolve: {
|
|
18
|
-
alias: {
|
|
19
|
-
process: 'process/browser'
|
|
20
|
-
}
|
|
21
|
-
},
|
|
22
|
-
plugins: [
|
|
23
|
-
new webpack.ProvidePlugin({
|
|
24
|
-
process: 'process/browser',
|
|
25
|
-
Buffer: ['buffer', 'Buffer']
|
|
26
|
-
})
|
|
27
|
-
],
|
|
28
|
-
devtool: 'source-map',
|
|
29
|
-
mode: 'production'
|
|
30
|
-
},
|
|
31
|
-
{
|
|
32
|
-
entry: './entry.js',
|
|
33
|
-
target: 'node',
|
|
34
|
-
output: {
|
|
35
|
-
path: path.resolve(__dirname, 'dist'),
|
|
36
|
-
filename: 'bsv.module.js',
|
|
37
|
-
library: {
|
|
38
|
-
type: 'commonjs-module'
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
devtool: 'source-map',
|
|
42
|
-
mode: 'production'
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
entry: './entry.js',
|
|
46
|
-
target: 'node',
|
|
47
|
-
output: {
|
|
48
|
-
path: path.resolve(__dirname, 'dist'),
|
|
49
|
-
filename: 'bsv.cjs.js',
|
|
50
|
-
library: {
|
|
51
|
-
type: 'commonjs2'
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
|
-
devtool: 'source-map',
|
|
55
|
-
mode: 'production'
|
|
56
|
-
}
|
|
57
|
-
]
|