forked from EOSIO/eosjs-ecc
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkey_public.js
More file actions
139 lines (116 loc) · 3.8 KB
/
key_public.js
File metadata and controls
139 lines (116 loc) · 3.8 KB
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
const assert = require('assert');
const secp256k1 = require('secp256k1');
const hash = require('./hash');
const keyUtils = require('./key_utils');
module.exports = PublicKey
/**
@param {string|Buffer|PublicKey|ecurve.Point} public key
@param {string} [pubkey_prefix = 'EOS']
*/
function PublicKey(Q, pubkey_prefix = 'EOS') {
if(typeof Q === 'string') {
const publicKey = PublicKey.fromString(Q, pubkey_prefix)
assert(publicKey != null, 'Invalid public key')
return publicKey
} else if(typeof Q === 'object' && Buffer.isBuffer(Q.Q)) {
return PublicKey(Q.Q)
}
if(!Buffer.isBuffer(Q)) {
throw new TypeError('Invalid public key')
}
let compressed = Q.length === 33;
function toBuffer(_compressed = compressed) {
if(!_compressed && compressed) {
return Buffer.from(secp256k1.publicKeyConvert(Q, false));
}
return Q;
}
/** @todo rename to toStringLegacy
* @arg {string} [pubkey_prefix = 'EOS'] - public key prefix
*/
function toString(pubkey_prefix = 'EOS') {
return pubkey_prefix + keyUtils.checkEncode(Q)
}
function toUncompressed() {
return Buffer.from(secp256k1.publicKeyConvert(Q, false));
}
/** @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 )
return PublicKey.fromBuffer(Buffer.from(secp256k1.privateKeyTweakAdd(Q, offset)))
}
function toHex() {
return Q.toString('hex');
}
return {
Q,
toString,
// toStringLegacy,
toUncompressed,
toBuffer,
child,
toHex
}
}
/**
@param {string|Buffer|PublicKey|ecurve.Point} pubkey - public key
@param {string} [pubkey_prefix = 'EOS']
*/
PublicKey.isValid = function(pubkey, pubkey_prefix = 'EOS') {
try {
PublicKey(pubkey, pubkey_prefix)
return true
} catch(e) {
return false
}
}
PublicKey.fromBinary = function(bin) {
return PublicKey.fromBuffer(new Buffer(bin, 'binary'));
}
PublicKey.fromBuffer = function(buffer) {
return PublicKey(buffer);
}
/**
@arg {string} public_key - like PUB_K1_base58pubkey..
@arg {string} [pubkey_prefix = 'EOS'] - public key prefix
@return PublicKey or `null` (invalid)
*/
PublicKey.fromString = function(public_key, pubkey_prefix = 'EOS') {
try {
return PublicKey.fromStringOrThrow(public_key, pubkey_prefix)
} catch (e) {
return null;
}
}
/**
@arg {string} public_key - like PUB_K1_base58pubkey..
@arg {string} [pubkey_prefix = 'EOS'] - public key prefix
@throws {Error} if public key is invalid
@return PublicKey
*/
PublicKey.fromStringOrThrow = function(public_key, pubkey_prefix = 'EOS') {
assert.equal(typeof public_key, 'string', 'public_key')
const match = public_key.match(/^PUB_([A-Za-z0-9]+)_([A-Za-z0-9]+)$/)
if(match === null) {
// legacy
var prefix_match = new RegExp("^" + pubkey_prefix);
if(prefix_match.test(public_key)) {
public_key = public_key.substring(pubkey_prefix.length)
}
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'));
}