forked from GolosChain/golos-js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
132 lines (116 loc) · 3.88 KB
/
index.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
var bigi = require('bigi'),
bs58 = require('bs58'),
ecurve = require('ecurve'),
Point = ecurve.Point,
secp256k1 = ecurve.getCurveByName('secp256k1'),
config = require('../config'),
operations = require('./serializer/src/operations'),
Signature = require('./ecc/src/signature'),
KeyPrivate = require('./ecc/src/key_private'),
PublicKey = require('./ecc/src/key_public'),
hash = require('./ecc/src/hash');
var Auth = {};
var transaction = operations.transaction;
var signed_transaction = operations.signed_transaction;
Auth.signature=Signature;
Auth.verify = function (name, password, auths) {
var hasKey = false;
var roles = [];
for (var role in auths) {
roles.push(role);
}
var pubKeys = this.generateKeys(name, password, roles);
roles.forEach(function (role) {
if (auths[role][0][0] === pubKeys[role]) {
hasKey = true;
}
});
return hasKey;
};
Auth.generateKeys = function (name, password, roles) {
var pubKeys = {};
roles.forEach(function (role) {
var seed = name + role + password;
var brainKey = seed.trim().split(/[\t\n\v\f\r ]+/).join(' ');
var hashSha256 = hash.sha256(brainKey);
var bigInt = bigi.fromBuffer(hashSha256);
var toPubKey = secp256k1.G.multiply(bigInt);
var point = new Point(toPubKey.curve, toPubKey.x, toPubKey.y, toPubKey.z);
var pubBuf = point.getEncoded(toPubKey.compressed);
var checksum = hash.ripemd160(pubBuf);
var addy = Buffer.concat([pubBuf, checksum.slice(0, 4)]);
pubKeys[role] = config.get('address_prefix') + bs58.encode(addy);
});
return pubKeys;
};
/**
@arg {string} name - blockchain account name
@arg {string} password - very strong password typically no shorter than a private key
@arg {array} roles - defaults to standard Golos blockchain-level roles
*/
Auth.getPrivateKeys = function (name, password, roles = ['master', 'active', 'regular', 'memo']) {
var privKeys = {};
roles.forEach(function (role) {
privKeys[role] = this.toWif(name, password, role);
privKeys[role + 'Pubkey'] = this.wifToPublic(privKeys[role]);
}.bind(this));
return privKeys;
};
Auth.isWif = function (privWif) {
var isWif = false;
try {
var bufWif = new Buffer(bs58.decode(privWif));
var privKey = bufWif.slice(0, -4);
var checksum = bufWif.slice(-4);
var newChecksum = hash.sha256(privKey);
newChecksum = hash.sha256(newChecksum);
newChecksum = newChecksum.slice(0, 4);
if (checksum.toString() == newChecksum.toString()) {
isWif = true;
}
} catch (e) { }
return isWif;
};
Auth.toWif = function (name, password, role) {
var seed = name + role + password;
var brainKey = seed.trim().split(/[\t\n\v\f\r ]+/).join(' ');
var hashSha256 = hash.sha256(brainKey);
var privKey = Buffer.concat([new Buffer([0x80]), hashSha256]);
var checksum = hash.sha256(privKey);
checksum = hash.sha256(checksum);
checksum = checksum.slice(0, 4);
var privWif = Buffer.concat([privKey, checksum]);
return bs58.encode(privWif);
};
Auth.wifIsValid = function (privWif, pubWif) {
return (this.wifToPublic(privWif) == pubWif);
};
Auth.wifToPublic = function (privWif) {
var pubWif = KeyPrivate.fromWif(privWif);
pubWif = pubWif.toPublic().toString();
return pubWif;
};
Auth.isPubkey = function(pubkey, address_prefix) {
return PublicKey.fromString(pubkey, address_prefix) != null
}
Auth.signTransaction = function (trx, keys, debug = false) {
var signatures = [];
if (trx.signatures) {
signatures = [].concat(trx.signatures);
}
var cid = new Buffer(config.get('chain_id'), 'hex');
var buf = transaction.toBuffer(trx);
if(debug){
console.log('transaction',transaction.fromBuffer(buf));
console.log('raw transaction',buf.toString('hex'));
}
for (var key in keys) {
var sig = Signature.signBuffer(Buffer.concat([cid, buf]), keys[key]);
signatures.push(sig.toBuffer())
if(debug){
console.log('signature',sig.toBuffer().toString('hex'));
}
}
return signed_transaction.toObject(Object.assign(trx, { signatures: signatures }))
};
module.exports = Auth;