-
Notifications
You must be signed in to change notification settings - Fork 11
/
bitcoinjs-lib-wrapper.js
76 lines (66 loc) · 2.46 KB
/
bitcoinjs-lib-wrapper.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
/**
* bitcoinjs-lib backwards compatibility lib
* exposes some old functions for easily backporting
*/
'use strict';
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require) {
var Bitcoin = require('bitcoinjs-lib');
var BigInteger = require('bigi');
var Buffer = require('buffer').Buffer;
var base58check = require('bs58check');
Bitcoin.Address.validate = function(address) {
try {
base58check.decode(address);
return true;
} catch (e) {
return false;
}
}
Bitcoin.Address.fromInputScript = function(script, network) {
network = network || Bitcoin.networks.bitcoin
var type = Bitcoin.scripts.classifyInput(script)
if (type === 'pubkey') {
var hash = Bitcoin.crypto.hash160(script.chunks[0]);
return new Bitcoin.Address(hash, network.pubKeyHash);
}
if (type === 'pubkeyhash') {
var hash = Bitcoin.crypto.hash160(script.chunks[1]);
return new Bitcoin.Address(hash, network.pubKeyHash);
}
if (type === 'scripthash') {
var hash = Bitcoin.crypto.hash160(script.chunks[script.chunks.length-1]);
return new Bitcoin.Address(hash, network.scriptHash);
}
// throw Error(type + ' has no matching Address')
}
Bitcoin.Address.getVersion = function(address) {
return base58check.decode(address).readUInt8(0);
}
Bitcoin.ECKey.fromBytes = function(bytes, compressed) {
if (!bytes) {
return Bitcoin.ECKey.makeRandom(compressed);
}
var d = BigInteger.fromByteArrayUnsigned(bytes.slice(0, 32));
if (compressed === null || compressed === undefined) compressed = (bytes[32] === 1);
return new Bitcoin.ECKey(d, compressed);
}
Bitcoin.ECPubKey.fromBytes = function(bytes) {
return Bitcoin.ECPubKey.fromBuffer(new Buffer(bytes));
}
Bitcoin.ECKey.prototype.toBytes = function() {
var bytes = this.d.toBuffer().toJSON().data;
while(bytes.length < 32) bytes.unshift(0);
if (this.pub.compressed) bytes.push(1);
return bytes;
}
Bitcoin.ECPubKey.prototype.toBytes = function(compressed) {
if (compressed === undefined) compressed = this.compressed;
return this.Q.getEncoded(compressed).toJSON().data;
}
return Bitcoin;
});