/
ecpubkey.js
56 lines (42 loc) · 1.26 KB
/
ecpubkey.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
var crypto = require('./crypto')
var ecdsa = require('./ecdsa')
var typeForce = require('typeforce')
var networks = require('./networks')
var Address = require('./address')
var ecurve = require('ecurve')
var secp256k1 = ecurve.getCurveByName('secp256k1')
function ECPubKey (Q, compressed) {
if (compressed === undefined) {
compressed = true
}
typeForce('Point', Q)
typeForce('Boolean', compressed)
this.compressed = compressed
this.Q = Q
}
// Constants
ECPubKey.curve = secp256k1
// Static constructors
ECPubKey.fromBuffer = function (buffer) {
var Q = ecurve.Point.decodeFrom(ECPubKey.curve, buffer)
return new ECPubKey(Q, Q.compressed)
}
ECPubKey.fromHex = function (hex) {
return ECPubKey.fromBuffer(new Buffer(hex, 'hex'))
}
// Operations
ECPubKey.prototype.getAddress = function (network) {
network = network || networks.bitcoin
return new Address(crypto.hash160(this.toBuffer()), network.pubKeyHash)
}
ECPubKey.prototype.verify = function (hash, signature) {
return ecdsa.verify(ECPubKey.curve, hash, signature, this.Q)
}
// Export functions
ECPubKey.prototype.toBuffer = function () {
return this.Q.getEncoded(this.compressed)
}
ECPubKey.prototype.toHex = function () {
return this.toBuffer().toString('hex')
}
module.exports = ECPubKey