xpi-ts 0.2.0
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/LICENSE +21 -0
- package/README.md +516 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/lib/bitcore/address.d.ts +66 -0
- package/dist/lib/bitcore/address.d.ts.map +1 -0
- package/dist/lib/bitcore/address.js +407 -0
- package/dist/lib/bitcore/block/block.d.ts +57 -0
- package/dist/lib/bitcore/block/block.d.ts.map +1 -0
- package/dist/lib/bitcore/block/block.js +233 -0
- package/dist/lib/bitcore/block/blockheader.d.ts +82 -0
- package/dist/lib/bitcore/block/blockheader.d.ts.map +1 -0
- package/dist/lib/bitcore/block/blockheader.js +323 -0
- package/dist/lib/bitcore/block/index.d.ts +5 -0
- package/dist/lib/bitcore/block/index.d.ts.map +1 -0
- package/dist/lib/bitcore/block/index.js +2 -0
- package/dist/lib/bitcore/chunk.d.ts +22 -0
- package/dist/lib/bitcore/chunk.d.ts.map +1 -0
- package/dist/lib/bitcore/chunk.js +46 -0
- package/dist/lib/bitcore/crypto/bn.d.ts +53 -0
- package/dist/lib/bitcore/crypto/bn.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/bn.js +238 -0
- package/dist/lib/bitcore/crypto/ecdsa.d.ts +46 -0
- package/dist/lib/bitcore/crypto/ecdsa.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/ecdsa.js +247 -0
- package/dist/lib/bitcore/crypto/hash.d.ts +16 -0
- package/dist/lib/bitcore/crypto/hash.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/hash.js +87 -0
- package/dist/lib/bitcore/crypto/index.d.ts +9 -0
- package/dist/lib/bitcore/crypto/index.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/index.js +8 -0
- package/dist/lib/bitcore/crypto/musig2.d.ts +40 -0
- package/dist/lib/bitcore/crypto/musig2.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/musig2.js +236 -0
- package/dist/lib/bitcore/crypto/point.d.ts +20 -0
- package/dist/lib/bitcore/crypto/point.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/point.js +133 -0
- package/dist/lib/bitcore/crypto/random.d.ts +7 -0
- package/dist/lib/bitcore/crypto/random.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/random.js +30 -0
- package/dist/lib/bitcore/crypto/schnorr.d.ts +40 -0
- package/dist/lib/bitcore/crypto/schnorr.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/schnorr.js +185 -0
- package/dist/lib/bitcore/crypto/signature.d.ts +53 -0
- package/dist/lib/bitcore/crypto/signature.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/signature.js +300 -0
- package/dist/lib/bitcore/crypto/sigtype.d.ts +5 -0
- package/dist/lib/bitcore/crypto/sigtype.d.ts.map +1 -0
- package/dist/lib/bitcore/crypto/sigtype.js +18 -0
- package/dist/lib/bitcore/encoding/base58.d.ts +16 -0
- package/dist/lib/bitcore/encoding/base58.d.ts.map +1 -0
- package/dist/lib/bitcore/encoding/base58.js +55 -0
- package/dist/lib/bitcore/encoding/base58check.d.ts +9 -0
- package/dist/lib/bitcore/encoding/base58check.d.ts.map +1 -0
- package/dist/lib/bitcore/encoding/base58check.js +82 -0
- package/dist/lib/bitcore/encoding/bufferreader.d.ts +34 -0
- package/dist/lib/bitcore/encoding/bufferreader.d.ts.map +1 -0
- package/dist/lib/bitcore/encoding/bufferreader.js +198 -0
- package/dist/lib/bitcore/encoding/bufferwriter.d.ts +36 -0
- package/dist/lib/bitcore/encoding/bufferwriter.d.ts.map +1 -0
- package/dist/lib/bitcore/encoding/bufferwriter.js +189 -0
- package/dist/lib/bitcore/encoding/varint.d.ts +20 -0
- package/dist/lib/bitcore/encoding/varint.d.ts.map +1 -0
- package/dist/lib/bitcore/encoding/varint.js +61 -0
- package/dist/lib/bitcore/errors.d.ts +28 -0
- package/dist/lib/bitcore/errors.d.ts.map +1 -0
- package/dist/lib/bitcore/errors.js +325 -0
- package/dist/lib/bitcore/hdprivatekey.d.ts +78 -0
- package/dist/lib/bitcore/hdprivatekey.d.ts.map +1 -0
- package/dist/lib/bitcore/hdprivatekey.js +381 -0
- package/dist/lib/bitcore/hdpublickey.d.ts +98 -0
- package/dist/lib/bitcore/hdpublickey.d.ts.map +1 -0
- package/dist/lib/bitcore/hdpublickey.js +416 -0
- package/dist/lib/bitcore/index.d.ts +60 -0
- package/dist/lib/bitcore/index.d.ts.map +1 -0
- package/dist/lib/bitcore/index.js +44 -0
- package/dist/lib/bitcore/message.d.ts +23 -0
- package/dist/lib/bitcore/message.d.ts.map +1 -0
- package/dist/lib/bitcore/message.js +112 -0
- package/dist/lib/bitcore/mnemonic/errors.d.ts +7 -0
- package/dist/lib/bitcore/mnemonic/errors.d.ts.map +1 -0
- package/dist/lib/bitcore/mnemonic/errors.js +20 -0
- package/dist/lib/bitcore/mnemonic/index.d.ts +5 -0
- package/dist/lib/bitcore/mnemonic/index.d.ts.map +1 -0
- package/dist/lib/bitcore/mnemonic/index.js +4 -0
- package/dist/lib/bitcore/mnemonic/mnemonic.d.ts +23 -0
- package/dist/lib/bitcore/mnemonic/mnemonic.d.ts.map +1 -0
- package/dist/lib/bitcore/mnemonic/mnemonic.js +164 -0
- package/dist/lib/bitcore/mnemonic/pbkdf2.d.ts +2 -0
- package/dist/lib/bitcore/mnemonic/pbkdf2.d.ts.map +1 -0
- package/dist/lib/bitcore/mnemonic/pbkdf2.js +25 -0
- package/dist/lib/bitcore/mnemonic/words/english.d.ts +2 -0
- package/dist/lib/bitcore/mnemonic/words/english.d.ts.map +1 -0
- package/dist/lib/bitcore/mnemonic/words/english.js +2050 -0
- package/dist/lib/bitcore/mnemonic/words/index.d.ts +4 -0
- package/dist/lib/bitcore/mnemonic/words/index.d.ts.map +1 -0
- package/dist/lib/bitcore/mnemonic/words/index.js +4 -0
- package/dist/lib/bitcore/musig2/index.d.ts +3 -0
- package/dist/lib/bitcore/musig2/index.d.ts.map +1 -0
- package/dist/lib/bitcore/musig2/index.js +2 -0
- package/dist/lib/bitcore/musig2/session.d.ts +79 -0
- package/dist/lib/bitcore/musig2/session.d.ts.map +1 -0
- package/dist/lib/bitcore/musig2/session.js +346 -0
- package/dist/lib/bitcore/musig2/signer.d.ts +61 -0
- package/dist/lib/bitcore/musig2/signer.d.ts.map +1 -0
- package/dist/lib/bitcore/musig2/signer.js +146 -0
- package/dist/lib/bitcore/networks.d.ts +53 -0
- package/dist/lib/bitcore/networks.d.ts.map +1 -0
- package/dist/lib/bitcore/networks.js +150 -0
- package/dist/lib/bitcore/opcode.d.ts +250 -0
- package/dist/lib/bitcore/opcode.d.ts.map +1 -0
- package/dist/lib/bitcore/opcode.js +270 -0
- package/dist/lib/bitcore/privatekey.d.ts +56 -0
- package/dist/lib/bitcore/privatekey.d.ts.map +1 -0
- package/dist/lib/bitcore/privatekey.js +237 -0
- package/dist/lib/bitcore/publickey.d.ts +59 -0
- package/dist/lib/bitcore/publickey.d.ts.map +1 -0
- package/dist/lib/bitcore/publickey.js +263 -0
- package/dist/lib/bitcore/script/interpreter.d.ts +98 -0
- package/dist/lib/bitcore/script/interpreter.d.ts.map +1 -0
- package/dist/lib/bitcore/script/interpreter.js +1704 -0
- package/dist/lib/bitcore/script.d.ts +111 -0
- package/dist/lib/bitcore/script.d.ts.map +1 -0
- package/dist/lib/bitcore/script.js +1112 -0
- package/dist/lib/bitcore/taproot/musig2.d.ts +29 -0
- package/dist/lib/bitcore/taproot/musig2.d.ts.map +1 -0
- package/dist/lib/bitcore/taproot/musig2.js +104 -0
- package/dist/lib/bitcore/taproot/nft.d.ts +164 -0
- package/dist/lib/bitcore/taproot/nft.d.ts.map +1 -0
- package/dist/lib/bitcore/taproot/nft.js +407 -0
- package/dist/lib/bitcore/taproot.d.ts +65 -0
- package/dist/lib/bitcore/taproot.d.ts.map +1 -0
- package/dist/lib/bitcore/taproot.js +288 -0
- package/dist/lib/bitcore/transaction/index.d.ts +12 -0
- package/dist/lib/bitcore/transaction/index.d.ts.map +1 -0
- package/dist/lib/bitcore/transaction/index.js +6 -0
- package/dist/lib/bitcore/transaction/input.d.ts +202 -0
- package/dist/lib/bitcore/transaction/input.d.ts.map +1 -0
- package/dist/lib/bitcore/transaction/input.js +911 -0
- package/dist/lib/bitcore/transaction/output.d.ts +48 -0
- package/dist/lib/bitcore/transaction/output.d.ts.map +1 -0
- package/dist/lib/bitcore/transaction/output.js +231 -0
- package/dist/lib/bitcore/transaction/sighash.d.ts +32 -0
- package/dist/lib/bitcore/transaction/sighash.d.ts.map +1 -0
- package/dist/lib/bitcore/transaction/sighash.js +335 -0
- package/dist/lib/bitcore/transaction/signature.d.ts +36 -0
- package/dist/lib/bitcore/transaction/signature.d.ts.map +1 -0
- package/dist/lib/bitcore/transaction/signature.js +130 -0
- package/dist/lib/bitcore/transaction/transaction.d.ts +164 -0
- package/dist/lib/bitcore/transaction/transaction.d.ts.map +1 -0
- package/dist/lib/bitcore/transaction/transaction.js +1016 -0
- package/dist/lib/bitcore/transaction/unspentoutput.d.ts +58 -0
- package/dist/lib/bitcore/transaction/unspentoutput.d.ts.map +1 -0
- package/dist/lib/bitcore/transaction/unspentoutput.js +167 -0
- package/dist/lib/bitcore/unit.d.ts +44 -0
- package/dist/lib/bitcore/unit.d.ts.map +1 -0
- package/dist/lib/bitcore/unit.js +106 -0
- package/dist/lib/bitcore/uri.d.ts +29 -0
- package/dist/lib/bitcore/uri.d.ts.map +1 -0
- package/dist/lib/bitcore/uri.js +163 -0
- package/dist/lib/bitcore/util/base32.d.ts +5 -0
- package/dist/lib/bitcore/util/base32.d.ts.map +1 -0
- package/dist/lib/bitcore/util/base32.js +58 -0
- package/dist/lib/bitcore/util/buffer.d.ts +18 -0
- package/dist/lib/bitcore/util/buffer.d.ts.map +1 -0
- package/dist/lib/bitcore/util/buffer.js +76 -0
- package/dist/lib/bitcore/util/convertBits.d.ts +2 -0
- package/dist/lib/bitcore/util/convertBits.d.ts.map +1 -0
- package/dist/lib/bitcore/util/convertBits.js +26 -0
- package/dist/lib/bitcore/util/js.d.ts +9 -0
- package/dist/lib/bitcore/util/js.d.ts.map +1 -0
- package/dist/lib/bitcore/util/js.js +45 -0
- package/dist/lib/bitcore/util/preconditions.d.ts +6 -0
- package/dist/lib/bitcore/util/preconditions.d.ts.map +1 -0
- package/dist/lib/bitcore/util/preconditions.js +31 -0
- package/dist/lib/bitcore/util.d.ts +14 -0
- package/dist/lib/bitcore/util.d.ts.map +1 -0
- package/dist/lib/bitcore/util.js +13 -0
- package/dist/lib/bitcore/xaddress.d.ts +45 -0
- package/dist/lib/bitcore/xaddress.d.ts.map +1 -0
- package/dist/lib/bitcore/xaddress.js +279 -0
- package/dist/lib/rank/api.d.ts +75 -0
- package/dist/lib/rank/api.d.ts.map +1 -0
- package/dist/lib/rank/api.js +4 -0
- package/dist/lib/rank/index.d.ts +127 -0
- package/dist/lib/rank/index.d.ts.map +1 -0
- package/dist/lib/rank/index.js +421 -0
- package/dist/lib/rank/opcode.d.ts +23 -0
- package/dist/lib/rank/opcode.d.ts.map +1 -0
- package/dist/lib/rank/opcode.js +23 -0
- package/dist/lib/rank/script.d.ts +2 -0
- package/dist/lib/rank/script.d.ts.map +1 -0
- package/dist/lib/rank/script.js +7 -0
- package/dist/lib/rank/transaction.d.ts +3 -0
- package/dist/lib/rank/transaction.d.ts.map +1 -0
- package/dist/lib/rank/transaction.js +12 -0
- package/dist/lib/rpc.d.ts +136 -0
- package/dist/lib/rpc.d.ts.map +1 -0
- package/dist/lib/rpc.js +62 -0
- package/dist/utils/constants.d.ts +18 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +20 -0
- package/dist/utils/env.d.ts +3 -0
- package/dist/utils/env.d.ts.map +1 -0
- package/dist/utils/env.js +8 -0
- package/dist/utils/string.d.ts +11 -0
- package/dist/utils/string.d.ts.map +1 -0
- package/dist/utils/string.js +47 -0
- package/dist/utils/types.d.ts +2 -0
- package/dist/utils/types.d.ts.map +1 -0
- package/dist/utils/types.js +1 -0
- package/dist/utils/wallet.d.ts +12 -0
- package/dist/utils/wallet.d.ts.map +1 -0
- package/dist/utils/wallet.js +28 -0
- package/package.json +91 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { BN } from './bn.js';
|
|
2
|
+
import { Point } from './point.js';
|
|
3
|
+
import { Signature } from './signature.js';
|
|
4
|
+
import { Hash } from './hash.js';
|
|
5
|
+
export class Schnorr {
|
|
6
|
+
hashbuf;
|
|
7
|
+
endian;
|
|
8
|
+
privkey;
|
|
9
|
+
pubkey;
|
|
10
|
+
sig;
|
|
11
|
+
verified;
|
|
12
|
+
constructor(obj) {
|
|
13
|
+
if (obj) {
|
|
14
|
+
this.set(obj);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
set(obj) {
|
|
18
|
+
this.hashbuf = obj.hashbuf || this.hashbuf;
|
|
19
|
+
this.endian = obj.endian || this.endian;
|
|
20
|
+
this.privkey = obj.privkey || this.privkey;
|
|
21
|
+
this.pubkey =
|
|
22
|
+
obj.pubkey || (this.privkey ? this.privkey.toPublicKey() : this.pubkey);
|
|
23
|
+
this.sig = obj.sig || this.sig;
|
|
24
|
+
this.verified = obj.verified || this.verified;
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
privkey2pubkey() {
|
|
28
|
+
this.pubkey = this.privkey.toPublicKey();
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
toPublicKey() {
|
|
32
|
+
return this.privkey.toPublicKey();
|
|
33
|
+
}
|
|
34
|
+
sign() {
|
|
35
|
+
const hashbuf = this.hashbuf;
|
|
36
|
+
const privkey = this.privkey;
|
|
37
|
+
const d = privkey.bn;
|
|
38
|
+
if (!hashbuf || !privkey || !d) {
|
|
39
|
+
throw new Error('invalid parameters');
|
|
40
|
+
}
|
|
41
|
+
if (!Buffer.isBuffer(hashbuf) || hashbuf.length !== 32) {
|
|
42
|
+
throw new Error('hashbuf must be a 32 byte buffer');
|
|
43
|
+
}
|
|
44
|
+
const e = new BN(hashbuf, 'be');
|
|
45
|
+
const obj = this._findSignature(d, e);
|
|
46
|
+
obj.compressed = this.pubkey.compressed;
|
|
47
|
+
obj.isSchnorr = true;
|
|
48
|
+
this.sig = new Signature(obj);
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
_findSignature(d, e) {
|
|
52
|
+
const n = Point.getN();
|
|
53
|
+
const G = Point.getG();
|
|
54
|
+
if (d.lte(new BN(0))) {
|
|
55
|
+
throw new Error('privkey out of field of curve');
|
|
56
|
+
}
|
|
57
|
+
if (d.gte(n)) {
|
|
58
|
+
throw new Error('privkey out of field of curve');
|
|
59
|
+
}
|
|
60
|
+
let k = this.nonceFunctionRFC6979(d.toArrayLike(Buffer, 'be', 32), e.toArrayLike(Buffer, 'be', 32));
|
|
61
|
+
const P = G.mul(d);
|
|
62
|
+
const R = G.mul(k);
|
|
63
|
+
if (R.hasSquare()) {
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
k = n.sub(k);
|
|
67
|
+
}
|
|
68
|
+
const r = R.getX();
|
|
69
|
+
const rBuffer = this.getrBuffer(r);
|
|
70
|
+
const e0 = new BN(Hash.sha256(Buffer.concat([
|
|
71
|
+
rBuffer,
|
|
72
|
+
Point.pointToCompressed(P),
|
|
73
|
+
e.toArrayLike(Buffer, 'be', 32),
|
|
74
|
+
])), 'be');
|
|
75
|
+
const s = e0.mul(d).add(k).mod(n);
|
|
76
|
+
return { r, s, compressed: this.pubkey.compressed, isSchnorr: true };
|
|
77
|
+
}
|
|
78
|
+
getrBuffer(r) {
|
|
79
|
+
const rNaturalLength = r.toArrayLike(Buffer, 'be').length;
|
|
80
|
+
if (rNaturalLength < 32) {
|
|
81
|
+
return r.toArrayLike(Buffer, 'be', 32);
|
|
82
|
+
}
|
|
83
|
+
return r.toArrayLike(Buffer, 'be');
|
|
84
|
+
}
|
|
85
|
+
getsBuffer(s) {
|
|
86
|
+
const sNaturalLength = s.toArrayLike(Buffer, 'be').length;
|
|
87
|
+
if (sNaturalLength < 32) {
|
|
88
|
+
return s.toArrayLike(Buffer, 'be', 32);
|
|
89
|
+
}
|
|
90
|
+
return s.toArrayLike(Buffer, 'be');
|
|
91
|
+
}
|
|
92
|
+
sigError() {
|
|
93
|
+
if (!Buffer.isBuffer(this.hashbuf) || this.hashbuf.length !== 32) {
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
const sigLength = this.getrBuffer(this.sig.r).length + this.getsBuffer(this.sig.s).length;
|
|
97
|
+
if (!(sigLength === 64 || sigLength === 65)) {
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
const hashbuf = this.endian === 'little' ? this.reverseBuffer(this.hashbuf) : this.hashbuf;
|
|
101
|
+
const P = this.pubkey.point;
|
|
102
|
+
const G = Point.getG();
|
|
103
|
+
if (P.isInfinity()) {
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
const r = this.sig.r;
|
|
107
|
+
const s = this.sig.s;
|
|
108
|
+
const p = new BN('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F', 16);
|
|
109
|
+
const n = Point.getN();
|
|
110
|
+
if (r.gte(p) || s.gte(n)) {
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
const Br = this.getrBuffer(this.sig.r);
|
|
114
|
+
const Bp = Point.pointToCompressed(P);
|
|
115
|
+
const hash = Hash.sha256(Buffer.concat([Br, Bp, hashbuf]));
|
|
116
|
+
const e = new BN(hash, 'be').mod(n);
|
|
117
|
+
const sG = G.mul(s);
|
|
118
|
+
const eP = P.mul(n.sub(e));
|
|
119
|
+
const R = sG.add(eP);
|
|
120
|
+
if (R.isInfinity() || !R.hasSquare() || !R.getX().eq(r)) {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
verify() {
|
|
126
|
+
this.verified = !!!this.sigError();
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
nonceFunctionRFC6979(privkey, msgbuf) {
|
|
130
|
+
let V = Buffer.from('0101010101010101010101010101010101010101010101010101010101010101', 'hex');
|
|
131
|
+
let K = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex');
|
|
132
|
+
const blob = Buffer.concat([
|
|
133
|
+
privkey,
|
|
134
|
+
msgbuf,
|
|
135
|
+
Buffer.from('', 'ascii'),
|
|
136
|
+
Buffer.from('Schnorr+SHA256 ', 'ascii'),
|
|
137
|
+
]);
|
|
138
|
+
K = Hash.sha256hmac(Buffer.concat([V, Buffer.from('00', 'hex'), blob]), K);
|
|
139
|
+
V = Hash.sha256hmac(V, K);
|
|
140
|
+
K = Hash.sha256hmac(Buffer.concat([V, Buffer.from('01', 'hex'), blob]), K);
|
|
141
|
+
V = Hash.sha256hmac(V, K);
|
|
142
|
+
let k = new BN(0);
|
|
143
|
+
let T;
|
|
144
|
+
while (true) {
|
|
145
|
+
V = Hash.sha256hmac(V, K);
|
|
146
|
+
T = new BN(V, 'be');
|
|
147
|
+
k = T;
|
|
148
|
+
if (V.length < 32) {
|
|
149
|
+
throw new Error('V length should be >= 32');
|
|
150
|
+
}
|
|
151
|
+
if (k.gt(new BN(0)) && k.lt(Point.getN())) {
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
K = Hash.sha256hmac(Buffer.concat([V, Buffer.from('00', 'hex')]), K);
|
|
155
|
+
V = Hash.sha256hmac(V, K);
|
|
156
|
+
}
|
|
157
|
+
return k;
|
|
158
|
+
}
|
|
159
|
+
static sign(hashbuf, privkey, endian) {
|
|
160
|
+
return new Schnorr()
|
|
161
|
+
.set({
|
|
162
|
+
hashbuf: hashbuf,
|
|
163
|
+
endian: endian,
|
|
164
|
+
privkey: privkey,
|
|
165
|
+
})
|
|
166
|
+
.sign().sig;
|
|
167
|
+
}
|
|
168
|
+
static verify(hashbuf, sig, pubkey, endian) {
|
|
169
|
+
return new Schnorr()
|
|
170
|
+
.set({
|
|
171
|
+
hashbuf: hashbuf,
|
|
172
|
+
endian: endian,
|
|
173
|
+
sig: sig,
|
|
174
|
+
pubkey: pubkey,
|
|
175
|
+
})
|
|
176
|
+
.verify().verified;
|
|
177
|
+
}
|
|
178
|
+
reverseBuffer(buf) {
|
|
179
|
+
const buf2 = Buffer.alloc(buf.length);
|
|
180
|
+
for (let i = 0; i < buf.length; i++) {
|
|
181
|
+
buf2[i] = buf[buf.length - 1 - i];
|
|
182
|
+
}
|
|
183
|
+
return buf2;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { BN } from './bn.js';
|
|
2
|
+
export interface SignatureData {
|
|
3
|
+
r: BN;
|
|
4
|
+
s: BN;
|
|
5
|
+
i?: number;
|
|
6
|
+
compressed?: boolean;
|
|
7
|
+
isSchnorr?: boolean;
|
|
8
|
+
nhashtype?: number;
|
|
9
|
+
}
|
|
10
|
+
export type SignatureSigningMethod = 'schnorr' | 'ecdsa';
|
|
11
|
+
export declare class Signature {
|
|
12
|
+
r: BN;
|
|
13
|
+
s: BN;
|
|
14
|
+
i?: number;
|
|
15
|
+
compressed?: boolean;
|
|
16
|
+
isSchnorr?: boolean;
|
|
17
|
+
nhashtype?: number;
|
|
18
|
+
static readonly SIGHASH_ALL = 1;
|
|
19
|
+
static readonly SIGHASH_NONE = 2;
|
|
20
|
+
static readonly SIGHASH_SINGLE = 3;
|
|
21
|
+
static readonly SIGHASH_FORKID = 64;
|
|
22
|
+
static readonly SIGHASH_LOTUS = 96;
|
|
23
|
+
static readonly SIGHASH_ANYONECANPAY = 128;
|
|
24
|
+
constructor(r: BN | SignatureData, s?: BN, isSchnorr?: boolean);
|
|
25
|
+
static create(r?: BN | SignatureData, s?: BN, isSchnorr?: boolean): Signature;
|
|
26
|
+
set(obj: SignatureData): Signature;
|
|
27
|
+
static fromCompact(buf: Buffer): Signature;
|
|
28
|
+
static fromDER(buf: Buffer, strict?: boolean): Signature;
|
|
29
|
+
static fromBuffer(buf: Buffer, strict?: boolean): Signature;
|
|
30
|
+
static fromTxFormat(buf: Buffer): Signature;
|
|
31
|
+
static fromDataFormat(buf: Buffer): Signature;
|
|
32
|
+
static fromString(str: string): Signature;
|
|
33
|
+
static fromSchnorr(buf: Buffer): Signature;
|
|
34
|
+
static parseSchnorrEncodedSig(buf: Buffer): {
|
|
35
|
+
r: BN;
|
|
36
|
+
s: BN;
|
|
37
|
+
nhashtype?: Buffer;
|
|
38
|
+
};
|
|
39
|
+
static parseDER(buf: Buffer, strict?: boolean): {
|
|
40
|
+
r: BN;
|
|
41
|
+
s: BN;
|
|
42
|
+
};
|
|
43
|
+
toCompact(i?: number, compressed?: boolean): Buffer;
|
|
44
|
+
toDER(signingMethod?: string): Buffer;
|
|
45
|
+
toBuffer(signingMethod?: string): Buffer;
|
|
46
|
+
toString(): string;
|
|
47
|
+
toTxFormat(signingMethod?: SignatureSigningMethod): Buffer;
|
|
48
|
+
static isDER(buf: Buffer): boolean;
|
|
49
|
+
hasLowS(): boolean;
|
|
50
|
+
hasDefinedHashtype(): boolean;
|
|
51
|
+
static isTxDER(buf: Buffer): boolean;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=signature.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signature.d.ts","sourceRoot":"","sources":["../../../../lib/bitcore/crypto/signature.ts"],"names":[],"mappings":"AAsBA,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAA;AAY5B,MAAM,WAAW,aAAa;IAC5B,CAAC,EAAE,EAAE,CAAA;IACL,CAAC,EAAE,EAAE,CAAA;IACL,CAAC,CAAC,EAAE,MAAM,CAAA;IACV,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED,MAAM,MAAM,sBAAsB,GAAG,SAAS,GAAG,OAAO,CAAA;AAgCxD,qBAAa,SAAS;IAEpB,CAAC,EAAG,EAAE,CAAA;IAGN,CAAC,EAAG,EAAE,CAAA;IAGN,CAAC,CAAC,EAAE,MAAM,CAAA;IAGV,UAAU,CAAC,EAAE,OAAO,CAAA;IAGpB,SAAS,CAAC,EAAE,OAAO,CAAA;IAGnB,SAAS,CAAC,EAAE,MAAM,CAAA;IAclB,MAAM,CAAC,QAAQ,CAAC,WAAW,KAAO;IAclC,MAAM,CAAC,QAAQ,CAAC,YAAY,KAAO;IAenC,MAAM,CAAC,QAAQ,CAAC,cAAc,KAAO;IAoBrC,MAAM,CAAC,QAAQ,CAAC,cAAc,MAAO;IAiCrC,MAAM,CAAC,QAAQ,CAAC,aAAa,MAAO;IAmBpC,MAAM,CAAC,QAAQ,CAAC,oBAAoB,OAAO;gBAyB/B,CAAC,EAAE,EAAE,GAAG,aAAa,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,SAAS,CAAC,EAAE,OAAO;IAoB9D,MAAM,CAAC,MAAM,CACX,CAAC,CAAC,EAAE,EAAE,GAAG,aAAa,EACtB,CAAC,CAAC,EAAE,EAAE,EACN,SAAS,CAAC,EAAE,OAAO,GAClB,SAAS;IAaZ,GAAG,CAAC,GAAG,EAAE,aAAa,GAAG,SAAS;IA4BlC,MAAM,CAAC,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS;IAwD1C,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,OAAc,GAAG,SAAS;IA6B9D,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,OAAc,GAAG,SAAS;IAgBjE,MAAM,CAAC,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS;IAc3C,MAAM,CAAC,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS;IAW7C,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS;IAazC,MAAM,CAAC,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS;IAmB1C,MAAM,CAAC,sBAAsB,CAAC,GAAG,EAAE,MAAM,GAAG;QAC1C,CAAC,EAAE,EAAE,CAAA;QACL,CAAC,EAAE,EAAE,CAAA;QACL,SAAS,CAAC,EAAE,MAAM,CAAA;KACnB;IA4CD,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,OAAc,GAAG;QAAE,CAAC,EAAE,EAAE,CAAC;QAAC,CAAC,EAAE,EAAE,CAAA;KAAE;IAwEtE,SAAS,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,OAAO,GAAG,MAAM;IAqCnD,KAAK,CAAC,aAAa,GAAE,MAAgB,GAAG,MAAM;IAwC9C,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM;IAexC,QAAQ,IAAI,MAAM;IAyBlB,UAAU,CAAC,aAAa,CAAC,EAAE,sBAAsB,GAAG,MAAM;IAsB1D,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IA4ElC,OAAO,IAAI,OAAO;IAqBlB,kBAAkB,IAAI,OAAO;IAqC7B,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;CAGrC"}
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { BN } from './bn.js';
|
|
2
|
+
export class Signature {
|
|
3
|
+
r;
|
|
4
|
+
s;
|
|
5
|
+
i;
|
|
6
|
+
compressed;
|
|
7
|
+
isSchnorr;
|
|
8
|
+
nhashtype;
|
|
9
|
+
static SIGHASH_ALL = 0x01;
|
|
10
|
+
static SIGHASH_NONE = 0x02;
|
|
11
|
+
static SIGHASH_SINGLE = 0x03;
|
|
12
|
+
static SIGHASH_FORKID = 0x40;
|
|
13
|
+
static SIGHASH_LOTUS = 0x60;
|
|
14
|
+
static SIGHASH_ANYONECANPAY = 0x80;
|
|
15
|
+
constructor(r, s, isSchnorr) {
|
|
16
|
+
if (r instanceof BN) {
|
|
17
|
+
this.set({
|
|
18
|
+
r: r,
|
|
19
|
+
s: s,
|
|
20
|
+
isSchnorr: isSchnorr,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
else if (r) {
|
|
24
|
+
this.set(r);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
static create(r, s, isSchnorr) {
|
|
28
|
+
return new Signature(r, s, isSchnorr);
|
|
29
|
+
}
|
|
30
|
+
set(obj) {
|
|
31
|
+
this.r = obj.r || this.r || undefined;
|
|
32
|
+
this.s = obj.s || this.s || undefined;
|
|
33
|
+
this.i = typeof obj.i !== 'undefined' ? obj.i : this.i;
|
|
34
|
+
this.compressed =
|
|
35
|
+
typeof obj.compressed !== 'undefined' ? obj.compressed : this.compressed;
|
|
36
|
+
this.isSchnorr = obj.isSchnorr;
|
|
37
|
+
this.nhashtype = obj.nhashtype || this.nhashtype || undefined;
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
static fromCompact(buf) {
|
|
41
|
+
if (!Buffer.isBuffer(buf)) {
|
|
42
|
+
throw new Error('Argument is expected to be a Buffer');
|
|
43
|
+
}
|
|
44
|
+
const sig = new Signature(new BN(0), new BN(0));
|
|
45
|
+
let compressed = true;
|
|
46
|
+
let i = buf.subarray(0, 1)[0] - 27 - 4;
|
|
47
|
+
if (i < 0) {
|
|
48
|
+
compressed = false;
|
|
49
|
+
i = i + 4;
|
|
50
|
+
}
|
|
51
|
+
const b2 = buf.subarray(1, 33);
|
|
52
|
+
const b3 = buf.subarray(33, 65);
|
|
53
|
+
if (!(i === 0 || i === 1 || i === 2 || i === 3)) {
|
|
54
|
+
throw new Error('i must be 0, 1, 2, or 3');
|
|
55
|
+
}
|
|
56
|
+
if (b2.length !== 32) {
|
|
57
|
+
throw new Error('r must be 32 bytes');
|
|
58
|
+
}
|
|
59
|
+
if (b3.length !== 32) {
|
|
60
|
+
throw new Error('s must be 32 bytes');
|
|
61
|
+
}
|
|
62
|
+
sig.compressed = compressed;
|
|
63
|
+
sig.i = i;
|
|
64
|
+
sig.r = new BN(b2, 'le');
|
|
65
|
+
sig.s = new BN(b3, 'le');
|
|
66
|
+
return sig;
|
|
67
|
+
}
|
|
68
|
+
static fromDER(buf, strict = true) {
|
|
69
|
+
if ((buf.length === 64 || buf.length === 65) && buf[0] !== 0x30) {
|
|
70
|
+
const obj = Signature.parseSchnorrEncodedSig(buf);
|
|
71
|
+
const sig = new Signature(new BN(0), new BN(0));
|
|
72
|
+
sig.r = obj.r;
|
|
73
|
+
sig.s = obj.s;
|
|
74
|
+
sig.isSchnorr = true;
|
|
75
|
+
return sig;
|
|
76
|
+
}
|
|
77
|
+
if (buf.length === 64 && buf[0] === 0x30) {
|
|
78
|
+
throw new Error('64 DER (ecdsa) signatures not allowed');
|
|
79
|
+
}
|
|
80
|
+
const obj = Signature.parseDER(buf, strict);
|
|
81
|
+
const sig = new Signature(new BN(0), new BN(0));
|
|
82
|
+
sig.r = obj.r;
|
|
83
|
+
sig.s = obj.s;
|
|
84
|
+
return sig;
|
|
85
|
+
}
|
|
86
|
+
static fromBuffer(buf, strict = true) {
|
|
87
|
+
return Signature.fromDER(buf, strict);
|
|
88
|
+
}
|
|
89
|
+
static fromTxFormat(buf) {
|
|
90
|
+
const nhashtype = buf.readUInt8(buf.length - 1);
|
|
91
|
+
const derbuf = buf.subarray(0, buf.length - 1);
|
|
92
|
+
const sig = Signature.fromDER(derbuf, false);
|
|
93
|
+
sig.nhashtype = nhashtype;
|
|
94
|
+
return sig;
|
|
95
|
+
}
|
|
96
|
+
static fromDataFormat(buf) {
|
|
97
|
+
const derbuf = buf.subarray(0, buf.length);
|
|
98
|
+
return Signature.fromDER(derbuf, false);
|
|
99
|
+
}
|
|
100
|
+
static fromString(str) {
|
|
101
|
+
const buf = Buffer.from(str, 'hex');
|
|
102
|
+
return Signature.fromDER(buf);
|
|
103
|
+
}
|
|
104
|
+
static fromSchnorr(buf) {
|
|
105
|
+
const parsed = Signature.parseSchnorrEncodedSig(buf);
|
|
106
|
+
const sig = new Signature(parsed.r, parsed.s);
|
|
107
|
+
sig.isSchnorr = true;
|
|
108
|
+
if (parsed.nhashtype) {
|
|
109
|
+
sig.nhashtype = parsed.nhashtype.readUInt8(0);
|
|
110
|
+
}
|
|
111
|
+
return sig;
|
|
112
|
+
}
|
|
113
|
+
static parseSchnorrEncodedSig(buf) {
|
|
114
|
+
if (buf.length !== 64 && buf.length !== 65) {
|
|
115
|
+
throw new Error(`Invalid Schnorr signature length: ${buf.length} bytes (expected 64 or 65)`);
|
|
116
|
+
}
|
|
117
|
+
const r = buf.subarray(0, 32);
|
|
118
|
+
const s = buf.subarray(32, 64);
|
|
119
|
+
let hashtype;
|
|
120
|
+
if (buf.length === 65) {
|
|
121
|
+
hashtype = buf.subarray(64, 65);
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
r: new BN(r, 'be'),
|
|
125
|
+
s: new BN(s, 'be'),
|
|
126
|
+
nhashtype: hashtype,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
static parseDER(buf, strict = true) {
|
|
130
|
+
if (!Buffer.isBuffer(buf)) {
|
|
131
|
+
throw new Error('DER formatted signature should be a buffer');
|
|
132
|
+
}
|
|
133
|
+
const header = buf[0];
|
|
134
|
+
if (header !== 0x30) {
|
|
135
|
+
throw new Error('Header byte should be 0x30');
|
|
136
|
+
}
|
|
137
|
+
let length = buf[1];
|
|
138
|
+
const buflength = buf.subarray(2).length;
|
|
139
|
+
if (strict && length !== buflength) {
|
|
140
|
+
throw new Error('Length byte should length of what follows');
|
|
141
|
+
}
|
|
142
|
+
length = length < buflength ? length : buflength;
|
|
143
|
+
const rheader = buf[2 + 0];
|
|
144
|
+
if (rheader !== 0x02) {
|
|
145
|
+
throw new Error('Integer byte for r should be 0x02');
|
|
146
|
+
}
|
|
147
|
+
const rlength = buf[2 + 1];
|
|
148
|
+
const rbuf = buf.subarray(2 + 2, 2 + 2 + rlength);
|
|
149
|
+
const r = new BN(rbuf, 'be');
|
|
150
|
+
if (rlength !== rbuf.length) {
|
|
151
|
+
throw new Error('Length of r incorrect');
|
|
152
|
+
}
|
|
153
|
+
const sheader = buf[2 + 2 + rlength + 0];
|
|
154
|
+
if (sheader !== 0x02) {
|
|
155
|
+
throw new Error('Integer byte for s should be 0x02');
|
|
156
|
+
}
|
|
157
|
+
const slength = buf[2 + 2 + rlength + 1];
|
|
158
|
+
const sbuf = buf.subarray(2 + 2 + rlength + 2, 2 + 2 + rlength + 2 + slength);
|
|
159
|
+
const s = new BN(sbuf, 'be');
|
|
160
|
+
if (slength !== sbuf.length) {
|
|
161
|
+
throw new Error('Length of s incorrect');
|
|
162
|
+
}
|
|
163
|
+
const sumlength = 2 + 2 + rlength + 2 + slength;
|
|
164
|
+
if (length !== sumlength - 2) {
|
|
165
|
+
throw new Error('Length of signature incorrect');
|
|
166
|
+
}
|
|
167
|
+
return { r, s };
|
|
168
|
+
}
|
|
169
|
+
toCompact(i, compressed) {
|
|
170
|
+
const recoveryId = typeof i === 'number' ? i : this.i;
|
|
171
|
+
const isCompressed = typeof compressed === 'boolean' ? compressed : this.compressed;
|
|
172
|
+
if (!(recoveryId === 0 ||
|
|
173
|
+
recoveryId === 1 ||
|
|
174
|
+
recoveryId === 2 ||
|
|
175
|
+
recoveryId === 3)) {
|
|
176
|
+
throw new Error('i must be equal to 0, 1, 2, or 3');
|
|
177
|
+
}
|
|
178
|
+
let val = recoveryId + 27 + 4;
|
|
179
|
+
if (isCompressed === false) {
|
|
180
|
+
val = val - 4;
|
|
181
|
+
}
|
|
182
|
+
const b1 = Buffer.from([val]);
|
|
183
|
+
const b2 = this.r.toArrayLike(Buffer, 'le', 32);
|
|
184
|
+
const b3 = this.s.toArrayLike(Buffer, 'le', 32);
|
|
185
|
+
return Buffer.concat([b1, b2, b3]);
|
|
186
|
+
}
|
|
187
|
+
toDER(signingMethod = 'ecdsa') {
|
|
188
|
+
if (signingMethod === 'schnorr') {
|
|
189
|
+
return Buffer.concat([
|
|
190
|
+
this.r.toArrayLike(Buffer, 'be', 32),
|
|
191
|
+
this.s.toArrayLike(Buffer, 'be', 32),
|
|
192
|
+
]);
|
|
193
|
+
}
|
|
194
|
+
const rnbuf = this.r.toArrayLike(Buffer, 'be');
|
|
195
|
+
const snbuf = this.s.toArrayLike(Buffer, 'be');
|
|
196
|
+
const rneg = (rnbuf[0] & 0x80) !== 0;
|
|
197
|
+
const sneg = (snbuf[0] & 0x80) !== 0;
|
|
198
|
+
const rbuf = rneg ? Buffer.concat([Buffer.from([0x00]), rnbuf]) : rnbuf;
|
|
199
|
+
const sbuf = sneg ? Buffer.concat([Buffer.from([0x00]), snbuf]) : snbuf;
|
|
200
|
+
const rlength = rbuf.length;
|
|
201
|
+
const slength = sbuf.length;
|
|
202
|
+
const length = 2 + rlength + 2 + slength;
|
|
203
|
+
const rheader = 0x02;
|
|
204
|
+
const sheader = 0x02;
|
|
205
|
+
const header = 0x30;
|
|
206
|
+
return Buffer.concat([
|
|
207
|
+
Buffer.from([header, length, rheader, rlength]),
|
|
208
|
+
rbuf,
|
|
209
|
+
Buffer.from([sheader, slength]),
|
|
210
|
+
sbuf,
|
|
211
|
+
]);
|
|
212
|
+
}
|
|
213
|
+
toBuffer(signingMethod) {
|
|
214
|
+
const method = signingMethod || (this.isSchnorr ? 'schnorr' : 'ecdsa');
|
|
215
|
+
return this.toDER(method);
|
|
216
|
+
}
|
|
217
|
+
toString() {
|
|
218
|
+
const method = this.isSchnorr ? 'schnorr' : 'ecdsa';
|
|
219
|
+
const buf = this.toDER(method);
|
|
220
|
+
return buf.toString('hex');
|
|
221
|
+
}
|
|
222
|
+
toTxFormat(signingMethod) {
|
|
223
|
+
const derbuf = this.toDER(signingMethod);
|
|
224
|
+
const buf = Buffer.alloc(1);
|
|
225
|
+
buf.writeUInt8(this.nhashtype || 0, 0);
|
|
226
|
+
return Buffer.concat([derbuf, buf]);
|
|
227
|
+
}
|
|
228
|
+
static isDER(buf) {
|
|
229
|
+
if (buf.length < 8 || buf.length > 72) {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
if (buf[0] !== 0x30) {
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
if (buf[1] !== buf.length - 2) {
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
if (buf[2] !== 0x02) {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
const lenR = buf[3];
|
|
242
|
+
if (lenR === 0) {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
if (buf[4] & 0x80) {
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
if (lenR > buf.length - 7) {
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
if (lenR > 1 && buf[4] === 0x00 && !(buf[5] & 0x80)) {
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
const startS = lenR + 4;
|
|
255
|
+
if (buf[startS] !== 0x02) {
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
const lenS = buf[startS + 1];
|
|
259
|
+
if (lenS === 0) {
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
if (buf[startS + 2] & 0x80) {
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
if (startS + lenS + 2 !== buf.length) {
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
if (lenS > 1 && buf[startS + 2] === 0x00 && !(buf[startS + 3] & 0x80)) {
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
hasLowS() {
|
|
274
|
+
const lowSThreshold = new BN('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0', 16);
|
|
275
|
+
if (this.s.lt(new BN(1)) || this.s.gt(lowSThreshold)) {
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
return true;
|
|
279
|
+
}
|
|
280
|
+
hasDefinedHashtype() {
|
|
281
|
+
if (typeof this.nhashtype !== 'number') {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
if (this.nhashtype & Signature.SIGHASH_LOTUS) {
|
|
285
|
+
const baseMask = ~(Signature.SIGHASH_LOTUS | Signature.SIGHASH_ANYONECANPAY) >>> 0;
|
|
286
|
+
const baseType = this.nhashtype & baseMask;
|
|
287
|
+
return (baseType >= Signature.SIGHASH_ALL &&
|
|
288
|
+
baseType <= Signature.SIGHASH_SINGLE);
|
|
289
|
+
}
|
|
290
|
+
const mask = ~(Signature.SIGHASH_FORKID | Signature.SIGHASH_ANYONECANPAY) >>> 0;
|
|
291
|
+
const temp = this.nhashtype & mask;
|
|
292
|
+
if (temp < Signature.SIGHASH_ALL || temp > Signature.SIGHASH_SINGLE) {
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
295
|
+
return true;
|
|
296
|
+
}
|
|
297
|
+
static isTxDER(buf) {
|
|
298
|
+
return Signature.isDER(buf.subarray(0, buf.length - 1));
|
|
299
|
+
}
|
|
300
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function isSchnorrSignature(sig: Buffer): boolean;
|
|
2
|
+
export declare function isECDSASignature(sig: Buffer): boolean;
|
|
3
|
+
export declare function getSignatureType(sig: Buffer): 'schnorr' | 'ecdsa';
|
|
4
|
+
export declare function isValidSignatureLength(sig: Buffer): boolean;
|
|
5
|
+
//# sourceMappingURL=sigtype.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sigtype.d.ts","sourceRoot":"","sources":["../../../../lib/bitcore/crypto/sigtype.ts"],"names":[],"mappings":"AAoBA,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAEvD;AAWD,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAErD;AAQD,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAEjE;AAcD,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAc3D"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function isSchnorrSignature(sig) {
|
|
2
|
+
return sig.length === 64;
|
|
3
|
+
}
|
|
4
|
+
export function isECDSASignature(sig) {
|
|
5
|
+
return sig.length !== 64;
|
|
6
|
+
}
|
|
7
|
+
export function getSignatureType(sig) {
|
|
8
|
+
return isSchnorrSignature(sig) ? 'schnorr' : 'ecdsa';
|
|
9
|
+
}
|
|
10
|
+
export function isValidSignatureLength(sig) {
|
|
11
|
+
if (sig.length === 64) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
if (sig.length >= 9 && sig.length <= 73) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface Base58Data {
|
|
2
|
+
buf?: Buffer;
|
|
3
|
+
}
|
|
4
|
+
export declare class Base58 {
|
|
5
|
+
buf?: Buffer;
|
|
6
|
+
constructor(obj?: Buffer | string | Base58Data);
|
|
7
|
+
static validCharacters(chars: string | Buffer): boolean;
|
|
8
|
+
set(obj: Base58Data): Base58;
|
|
9
|
+
static encode(buf: Buffer): string;
|
|
10
|
+
static decode(str: string): Buffer;
|
|
11
|
+
fromBuffer(buf: Buffer): Base58;
|
|
12
|
+
fromString(str: string): Base58;
|
|
13
|
+
toBuffer(): Buffer;
|
|
14
|
+
toString(): string;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=base58.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base58.d.ts","sourceRoot":"","sources":["../../../../lib/bitcore/encoding/base58.ts"],"names":[],"mappings":"AAcA,MAAM,WAAW,UAAU;IACzB,GAAG,CAAC,EAAE,MAAM,CAAA;CACb;AAED,qBAAa,MAAM;IACjB,GAAG,CAAC,EAAE,MAAM,CAAA;gBAEA,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,UAAU;IAY9C,MAAM,CAAC,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO;IAOvD,GAAG,CAAC,GAAG,EAAE,UAAU,GAAG,MAAM;IAK5B,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAOlC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAOlC,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAK/B,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAM/B,QAAQ,IAAI,MAAM;IAIlB,QAAQ,IAAI,MAAM;CAGnB"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import bs58 from 'bs58';
|
|
2
|
+
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'.split('');
|
|
3
|
+
export class Base58 {
|
|
4
|
+
buf;
|
|
5
|
+
constructor(obj) {
|
|
6
|
+
if (Buffer.isBuffer(obj)) {
|
|
7
|
+
const buf = obj;
|
|
8
|
+
this.fromBuffer(buf);
|
|
9
|
+
}
|
|
10
|
+
else if (typeof obj === 'string') {
|
|
11
|
+
const str = obj;
|
|
12
|
+
this.fromString(str);
|
|
13
|
+
}
|
|
14
|
+
else if (obj) {
|
|
15
|
+
this.set(obj);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
static validCharacters(chars) {
|
|
19
|
+
if (Buffer.isBuffer(chars)) {
|
|
20
|
+
chars = chars.toString();
|
|
21
|
+
}
|
|
22
|
+
return Array.from(chars).every(char => ALPHABET.includes(char));
|
|
23
|
+
}
|
|
24
|
+
set(obj) {
|
|
25
|
+
this.buf = obj.buf || this.buf || undefined;
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
static encode(buf) {
|
|
29
|
+
if (!Buffer.isBuffer(buf)) {
|
|
30
|
+
throw new Error('Input should be a buffer');
|
|
31
|
+
}
|
|
32
|
+
return bs58.encode(buf);
|
|
33
|
+
}
|
|
34
|
+
static decode(str) {
|
|
35
|
+
if (typeof str !== 'string') {
|
|
36
|
+
throw new Error('Input should be a string');
|
|
37
|
+
}
|
|
38
|
+
return Buffer.from(bs58.decode(str));
|
|
39
|
+
}
|
|
40
|
+
fromBuffer(buf) {
|
|
41
|
+
this.buf = buf;
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
fromString(str) {
|
|
45
|
+
const buf = Base58.decode(str);
|
|
46
|
+
this.buf = buf;
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
toBuffer() {
|
|
50
|
+
return this.buf;
|
|
51
|
+
}
|
|
52
|
+
toString() {
|
|
53
|
+
return Base58.encode(this.buf);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare class Base58Check {
|
|
2
|
+
static encode(buf: Buffer): string;
|
|
3
|
+
static decode(str: string): Buffer;
|
|
4
|
+
static checksum(data: Buffer): Buffer;
|
|
5
|
+
static validChecksum(data: Buffer, checksum: Buffer): boolean;
|
|
6
|
+
private static base58Encode;
|
|
7
|
+
private static base58Decode;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=base58check.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base58check.d.ts","sourceRoot":"","sources":["../../../../lib/bitcore/encoding/base58check.ts"],"names":[],"mappings":"AAkBA,qBAAa,WAAW;IAItB,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAelC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAwBlC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM;IASrC,MAAM,CAAC,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO;IAQ7D,OAAO,CAAC,MAAM,CAAC,YAAY;IA0B3B,OAAO,CAAC,MAAM,CAAC,YAAY;CAkC5B"}
|