forked from EOSIO/eosjs-ecc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_public.js
158 lines (129 loc) · 4.08 KB
/
key_public.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const assert = require('assert');
const ecurve = require('ecurve');
const BigInteger = require('bigi');
const secp256k1 = ecurve.getCurveByName('secp256k1');
const hash = require('./hash');
const keyUtils = require('./key_utils');
var G = secp256k1.G
var n = secp256k1.n
module.exports = PublicKey
/** @param {ecurve.Point} public key */
function PublicKey(Q) {
if(typeof Q === 'string') {
const publicKey = PublicKey.fromString(Q)
assert(publicKey != null, 'Invalid public key')
return publicKey
} else if(Buffer.isBuffer(Q)) {
return PublicKey.fromBuffer(Q)
} else if(typeof Q === 'object' && Q.Q) {
return PublicKey(Q.Q)
}
assert.equal(typeof Q, 'object', 'Invalid public key')
assert.equal(typeof Q.compressed, 'boolean', 'Invalid public key')
function toBuffer(compressed = Q.compressed) {
return Q.getEncoded(compressed);
}
let pubdata // cache
// /**
// @todo secp224r1
// @return {string} PUB_K1_base58pubkey..
// */
// function toString() {
// if(pubdata) {
// return pubdata
// }
// pubdata = `PUB_K1_` + keyUtils.checkEncode(toBuffer(), 'K1')
// return pubdata;
// }
/** @todo rename to toStringLegacy */
function toString() {
return 'EOS' + keyUtils.checkEncode(toBuffer())
}
function toUncompressed() {
var buf = Q.getEncoded(false);
var point = ecurve.Point.decodeFrom(secp256k1, buf);
return PublicKey.fromPoint(point);
}
/** @deprecated */
function child( offset ) {
console.error('Deprecated warning: PublicKey.child')
assert(Buffer.isBuffer(offset), "Buffer required: offset")
assert.equal(offset.length, 32, "offset length")
offset = Buffer.concat([ toBuffer(), offset ])
offset = hash.sha256( offset )
let c = BigInteger.fromBuffer( offset )
if (c.compareTo(n) >= 0)
throw new Error("Child offset went out of bounds, try again")
let cG = G.multiply(c)
let Qprime = Q.add(cG)
if( secp256k1.isInfinity(Qprime) )
throw new Error("Child offset derived to an invalid key, try again")
return PublicKey.fromPoint(Qprime)
}
function toHex() {
return toBuffer().toString('hex');
}
return {
Q,
toString,
// toStringLegacy,
toUncompressed,
toBuffer,
child,
toHex
}
}
PublicKey.isValid = function(text) {
try {
PublicKey(text)
return true
} catch(e) {
return false
}
}
PublicKey.fromBinary = function(bin) {
return PublicKey.fromBuffer(new Buffer(bin, 'binary'));
}
PublicKey.fromBuffer = function(buffer) {
return PublicKey(ecurve.Point.decodeFrom(secp256k1, buffer));
}
PublicKey.fromPoint = function(point) {
return PublicKey(point);
}
/**
@arg {string} public_key - like PUB_K1_base58pubkey..
@return PublicKey or `null` (invalid)
*/
PublicKey.fromString = function(public_key) {
try {
return PublicKey.fromStringOrThrow(public_key)
} catch (e) {
return null;
}
}
/**
@arg {string} public_key - like PUB_K1_base58pubkey..
@throws {Error} if public key is invalid
@return PublicKey
*/
PublicKey.fromStringOrThrow = function(public_key) {
assert(typeof public_key, 'string', 'public_key')
const match = public_key.match(/^PUB_([A-Za-z0-9]+)_([A-Za-z0-9]+)$/)
if(match === null) {
// legacy
if(/^EOS/.test(public_key)) {
public_key = public_key.substring(3)
}
return PublicKey.fromBuffer(keyUtils.checkDecode(public_key))
}
assert(match.length === 3, 'Expecting public key like: PUB_K1_base58pubkey..')
const [, keyType, keyString] = match
assert.equal(keyType, 'K1', 'K1 private key expected')
return PublicKey.fromBuffer(keyUtils.checkDecode(keyString, keyType))
}
PublicKey.fromHex = function(hex) {
return PublicKey.fromBuffer(new Buffer(hex, 'hex'));
}
PublicKey.fromStringHex = function(hex) {
return PublicKey.fromString(new Buffer(hex, 'hex'));
}