Skip to content

Commit 4a45d81

Browse files
committed
feat(signature): signature
1 parent fc674c9 commit 4a45d81

22 files changed

+8363
-1065
lines changed

dist/bip39_wordlist.js

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/btc_crypto.js

+1,746
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/classes/address.js

+295
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
"use strict";
2+
3+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4+
5+
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
6+
7+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
8+
9+
function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
10+
11+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
12+
13+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
14+
15+
module.exports = function (S) {
16+
var Buffer = S.Buffer;
17+
var defArgs = S.defArgs;
18+
var getBuffer = S.getBuffer;
19+
var BF = Buffer.from;
20+
var BC = Buffer.concat;
21+
var O = S.OPCODE;
22+
23+
var PrivateKey = function PrivateKey(k) {
24+
var A = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
25+
(0, _classCallCheck2["default"])(this, PrivateKey);
26+
defArgs(A, {
27+
compressed: null,
28+
testnet: false
29+
});
30+
31+
if (k === undefined) {
32+
if (A.compressed === null) A.compressed = true;
33+
this.compressed = A.compressed;
34+
this.testnet = A.testnet;
35+
this.key = S.createPrivateKey({
36+
wif: false
37+
});
38+
this.hex = this.key.hex();
39+
this.wif = S.privateKeyToWif(this.key, A);
40+
} else {
41+
if (S.isString(k)) {
42+
if (S.isHex(k)) {
43+
if (A.compressed === null) A.compressed = true;
44+
this.key = BF(k, 'hex');
45+
this.compressed = A.compressed;
46+
this.testnet = A.testnet;
47+
this.hex = this.key.hex();
48+
this.wif = S.privateKeyToWif(this.key, A);
49+
} else {
50+
this.wif = k;
51+
this.key = S.wifToPrivateKey(k, {
52+
hex: false
53+
});
54+
this.hex = this.key.hex();
55+
this.compressed = ![S.MAINNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX, S.TESTNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX].includes(k[0]);
56+
this.testnet = [S.TESTNET_PRIVATE_KEY_COMPRESSED_PREFIX, S.TESTNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX].includes(k[0]);
57+
}
58+
} else {
59+
k = BF(k);
60+
if (k.length !== 32) throw new Error('private key invalid');
61+
if (A.compressed === null) A.compressed = true;
62+
this.compressed = A.compressed;
63+
this.testnet = A.testnet;
64+
this.key = k;
65+
this.hex = this.key.hex();
66+
this.wif = S.privateKeyToWif(this.key, A);
67+
}
68+
}
69+
};
70+
71+
PrivateKey.prototype.toString = function () {
72+
return "".concat(this.wif);
73+
};
74+
75+
var PublicKey = function PublicKey(k) {
76+
var A = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
77+
(0, _classCallCheck2["default"])(this, PublicKey);
78+
defArgs(A, {
79+
compressed: null,
80+
testnet: false
81+
});
82+
this.compressed = A.compressed;
83+
this.testnet = A.testnet;
84+
85+
if (k instanceof PrivateKey) {
86+
A.testnet = k.testnet;
87+
A.compressed = k.compressed;
88+
k = k.wif;
89+
}
90+
91+
if (S.isString(k)) {
92+
if (S.isHex(k)) {
93+
k = BF(k, 'hex');
94+
if (A.compressed === null) A.compressed = true;
95+
} else if (S.isWifValid(k)) {
96+
this.compressed = ![S.MAINNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX, S.TESTNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX].includes(k[0]);
97+
this.testnet = [S.TESTNET_PRIVATE_KEY_COMPRESSED_PREFIX, S.TESTNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX].includes(k[0]);
98+
k = S.privateToPublicKey(k, {
99+
compressed: this.compressed,
100+
testnet: this.testnet,
101+
hex: false
102+
});
103+
} else throw new Error('private/public key invalid');
104+
} else k = BF(k);
105+
106+
if (k.length === 32) {
107+
if (A.compressed === null) A.compressed = true;
108+
this.key = S.privateToPublicKey(k, {
109+
compressed: A.compressed,
110+
testnet: A.testnet,
111+
hex: false
112+
});
113+
this.compressed = A.compressed;
114+
this.testnet = A.testnet;
115+
this.hex = this.key.hex();
116+
} else if (S.isPublicKeyValid(k)) {
117+
this.hex = k.hex();
118+
this.key = k;
119+
this.compressed = this.key.length === 33;
120+
this.testnet = A.testnet;
121+
} else throw new Error('private/public key invalid');
122+
};
123+
124+
PublicKey.prototype.toString = function () {
125+
return "".concat(this.hex);
126+
};
127+
128+
var Address = function Address(k) {
129+
var A = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
130+
(0, _classCallCheck2["default"])(this, Address);
131+
defArgs(A, {
132+
addressType: null,
133+
testnet: false,
134+
compressed: null
135+
});
136+
137+
if (k === undefined) {
138+
if (A.compressed === null) A.compressed = true;
139+
this.privateKey = new PrivateKey(undefined, A);
140+
this.publicKey = new PublicKey(this.privateKey, A);
141+
} else if (S.isString(k)) {
142+
if (S.isWifValid(k)) {
143+
this.privateKey = new PrivateKey(k, A);
144+
A.compressed = this.privateKey.compressed;
145+
this.publicKey = new PublicKey(this.privateKey, A);
146+
A.testnet = this.privateKey.testnet;
147+
} else if (S.isHex(k)) {
148+
if (A.compressed === null) A.compressed = true;
149+
k = BF(k, 'hex');
150+
} else {
151+
throw new Error('private/public key invalid');
152+
}
153+
} else if (k instanceof PrivateKey) {
154+
this.privateKey = k;
155+
A.testnet = k.testnet;
156+
A.compressed = k.compressed;
157+
this.publicKey = new PublicKey(this.privateKey, A);
158+
} else if (k instanceof PublicKey) {
159+
A.testnet = k.testnet;
160+
A.compressed = k.compressed;
161+
this.publicKey = k;
162+
} else {
163+
if (!Buffer.isBuffer(k)) k = BF(k);
164+
}
165+
166+
if (Buffer.isBuffer(k)) {
167+
if (k.length === 32) {
168+
if (A.compressed === null) A.compressed = true;
169+
this.privateKey = new PrivateKey(k, A);
170+
this.publicKey = new PublicKey(this.privateKey, A);
171+
} else if (S.isPublicKeyValid(k)) {
172+
this.publicKey = new PublicKey(k, A);
173+
A.compressed = this.publicKey.compressed;
174+
} else throw new Error('private/public key invalid');
175+
}
176+
177+
this.testnet = A.testnet;
178+
179+
if (A.addressType === null) {
180+
if (A.compressed === false) A.addressType = "P2PKH";else A.addressType = "P2WPKH";
181+
}
182+
183+
if (!["P2PKH", "PUBKEY", "P2WPKH", "P2SH_P2WPKH"].includes(A.addressType)) {
184+
throw new Error('address type invalid');
185+
}
186+
187+
this.type = A.addressType;
188+
189+
if (this.type === 'PUBKEY') {
190+
this.publicKeyScript = BC([S.opPushData(this.publicKey.key), BF([O.OP_CHECKSIG])]);
191+
this.publicKeyScriptHex = this.publicKeyScript.hex();
192+
}
193+
194+
this.witnessVersion = this.type === "P2WPKH" ? 0 : null;
195+
196+
if (this.type === "P2SH_P2WPKH") {
197+
this.scriptHash = true;
198+
this.redeemScript = S.publicKeyTo_P2SH_P2WPKH_Script(this.publicKey.key);
199+
this.redeemScriptHex = this.redeemScript.hex();
200+
this.hash = S.hash160(this.redeemScript);
201+
this.witnessVersion = null;
202+
} else {
203+
this.scriptHash = false;
204+
this.hash = S.hash160(this.publicKey.key);
205+
}
206+
207+
this.hashHex = this.hash.hex();
208+
this.testnet = A.testnet;
209+
this.address = S.hashToAddress(this.hash, {
210+
scriptHash: this.scriptHash,
211+
witnessVersion: this.witnessVersion,
212+
testnet: this.testnet
213+
});
214+
};
215+
216+
Address.prototype.toString = function () {
217+
return "".concat(this.address);
218+
};
219+
220+
var ScriptAddress = /*#__PURE__*/function () {
221+
function ScriptAddress(s) {
222+
var A = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
223+
(0, _classCallCheck2["default"])(this, ScriptAddress);
224+
defArgs(A, {
225+
witnessVersion: 0,
226+
testnet: false
227+
});
228+
this.witnessVersion = A.witnessVersion;
229+
this.testnet = A.testnet;
230+
s = getBuffer(s);
231+
this.script = s;
232+
this.scriptHex = s.hex();
233+
if (this.witnessVersion === null) this.hash = S.hash160(this.script);else this.hash = S.sha256(this.script);
234+
this.scriptOpcodes = S.decodeScript(this.script);
235+
this.scriptOpcodesAsm = S.decodeScript(this.script, {
236+
asm: true
237+
});
238+
this.address = S.hashToAddress(this.hash, {
239+
scriptHash: true,
240+
witnessVersion: this.witnessVersion,
241+
testnet: this.testnet
242+
});
243+
}
244+
245+
(0, _createClass2["default"])(ScriptAddress, null, [{
246+
key: "multisig",
247+
value: function multisig(n, m, keyList) {
248+
var A = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
249+
if (n > 15 || m > 15 || n > m || n < 1 || m < 1) throw new Error('invalid n of m maximum 15 of 15 multisig allowed');
250+
if (keyList.length !== m) throw new Error('invalid address list count');
251+
var s = [BF([0x50 + n])];
252+
253+
var _iterator = _createForOfIteratorHelper(keyList),
254+
_step;
255+
256+
try {
257+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
258+
var k = _step.value;
259+
260+
if (S.isString(k)) {
261+
if (S.isHex(k)) k = BF(k, 'hex');else if (S.isWifValid(k)) k = S.privateToPublicKey(k, {
262+
hex: false
263+
});else throw new Error('invalid key in key list');
264+
}
265+
266+
if (k instanceof Address) k = k.publicKey.key;
267+
if (k instanceof PrivateKey) k = S.privateToPublicKey(k.publicKey.key);
268+
if (!Buffer.isBuffer(k)) k = BF(k);
269+
if (k.length === 32) k = S.privateToPublicKey(k);
270+
if (k.length !== 33) throw new Error('invalid public key list element size');
271+
s.push(BC([BF(S.intToVarInt(k.length)), k]));
272+
}
273+
} catch (err) {
274+
_iterator.e(err);
275+
} finally {
276+
_iterator.f();
277+
}
278+
279+
s.push(BF([0x50 + m, O.OP_CHECKMULTISIG]));
280+
s = BC(s);
281+
return new ScriptAddress(s, A);
282+
}
283+
}]);
284+
return ScriptAddress;
285+
}();
286+
287+
ScriptAddress.prototype.toString = function () {
288+
return "".concat(this.address);
289+
};
290+
291+
S.PrivateKey = PrivateKey;
292+
S.PublicKey = PublicKey;
293+
S.ScriptAddress = ScriptAddress;
294+
S.Address = Address;
295+
};

0 commit comments

Comments
 (0)