-
Notifications
You must be signed in to change notification settings - Fork 216
/
Handler.js
191 lines (180 loc) · 5.9 KB
/
Handler.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { Api, JsonRpc, Serialize } from 'eosjs2';
import ecc from 'eosjs-ecc';
import sha256 from 'fast-sha256';
import { isObject } from 'lodash';
import serialize from '../../actions/helpers/ledger/serialize';
const eosjs1 = require('eosjs');
const { convertLegacyPublicKeys } = require('eosio-signing-request/node_modules/eosjs/dist/eosjs-numeric');
const { JsSignatureProvider } = require('eosio-signing-request/node_modules/eosjs/dist/eosjs-jssig');
const { remote } = require('electron');
const LedgerApi = require('../../actions/helpers/hardware/ledger').default;
const fuelTransaction = {
account: 'greymassnoop',
name: 'noop',
authorization: [
{
actor: 'greymassfuel',
permission: 'cosign',
}
],
data: {}
};
const testTransaction = {
account: 'eosio.token',
name: 'transfer',
authorization: [
{
actor: 'greymassfuel',
permission: 'cosign',
}
],
data: {
to: 'teamgreymass',
from: 'greymassfuel',
quantity: '0.0100 EOS',
memo: ''
}
};
export default class EOSHandler {
constructor(config) {
this.config = config;
if (config.signMethod === 'ledger') {
this.signatureProvider = new LedgerSignatureProvider(config);
} else {
this.signatureProvider = new JsSignatureProvider(config.keyProvider || []);
}
this.rpc = new JsonRpc(config.httpEndpoint);
this.api = new Api({
authorityProvider: this.getAuthorityProvider(),
rpc: this.rpc,
signatureProvider: this.signatureProvider,
textDecoder: new TextDecoder(),
textEncoder: new TextEncoder()
});
this.tapos = {
blocksBehind: 3,
broadcast: config.broadcast,
expireSeconds: config.expireSeconds,
sign: config.sign,
};
return this;
}
getAuthorityProvider() {
const { rpc } = this;
return {
async getRequiredKeys(args) {
const { transaction } = args;
transaction.actions.forEach((action, ti) => {
action.authorization.forEach((auth, ai) => {
if (auth.actor === 'greymassfuel' && auth.permission === 'cosign') {
delete transaction.actions[ti].authorization.splice(ai, 1);
}
});
});
return convertLegacyPublicKeys((await rpc.fetch('/v1/chain/get_required_keys', {
transaction,
available_keys: args.availableKeys,
})).required_keys);
}
};
}
transact(tx) {
const transaction = Object.assign({}, tx);
// append Fuel data where appropriate
if (this.config.greymassfuel) {
transaction.actions.unshift(fuelTransaction);
}
// no broadcast + sign = create a v16 format transaction
// should likely be converted to use esr payloads
if (!this.tapos.broadcast && !this.tapos.sign) {
return this.createTransaction(transaction, this.tapos);
}
// otherwise transact
return this.api.transact(transaction, this.tapos);
}
// return a transaction like v16 would
createTransaction = async (tx) => {
const info = await this.rpc.get_info();
const height = info.last_irreversible_block_num - this.tapos.blocksBehind;
const blockInfo = await this.rpc.get_block(height);
const transaction = Object.assign({}, tx, {
actions: await this.ensureSerializedActions(tx.actions),
context_free_actions: [],
transaction_extensions: [],
expiration: this.getExpiration(),
ref_block_num: blockInfo.block_num & 0xffff,
ref_block_prefix: blockInfo.ref_block_prefix,
max_cpu_usage_ms: 0,
max_net_usage_words: 0,
delay_sec: 0,
});
const serializedTransaction = this.api.serializeTransaction(transaction);
return {
broadcast: false,
transaction_id: Serialize.arrayToHex(sha256(serializedTransaction)).toLowerCase(),
transaction: {
compression: 'none',
signatures: [],
transaction
}
};
}
getExpiration = () => {
const { expireSeconds } = this.tapos;
const currentDate = new Date();
const timePlus = currentDate.getTime() + (expireSeconds * 1000);
const timeInISOString = (new Date(timePlus)).toISOString();
return timeInISOString.substr(0, timeInISOString.length - 1);
}
ensureSerializedActions = async (actions) => (await (isObject(actions[0].data))
? this.api.serializeActions(actions)
: actions)
getInfo = () => this.rpc.get_info()
getBlock = (height) => this.rpc.get_block(height)
getAbi = (account) => this.rpc.get_abi(account)
pushTransaction = (tx) => {
console.log("push", tx)
}
}
class LedgerSignatureProvider {
constructor(config) {
this.config = config;
this.rpc = new JsonRpc(config.httpEndpoint);
this.api = new Api({ rpc: this.rpc });
this.ledger = global.hardwareLedger || remote.getGlobal('hardwareLedger');
}
async getAvailableKeys() {
const { transport } = this.ledger;
const ledgerApi = new LedgerApi(transport);
const key = await ledgerApi.getPublicKey(this.config.signPath, false);
return [key.wif];
}
async sign({ chainId, serializedTransaction }) {
const { transport } = this.ledger;
const ledgerApi = new LedgerApi(transport);
const { fc } = eosjs1(this.config);
const tx = this.api.deserializeTransaction(serializedTransaction);
const buffer = serialize(chainId, tx, fc.types);
const response = await ledgerApi.signTransaction(this.config.signPath, buffer.toString('hex'));
console.log(response)
return {
signatures: convertSignatures(response.v + response.r + response.s),
serializedTransaction: this.api.serializeTransaction(tx),
};
}
}
const convertSignatures = (sigs) => {
if (!Array.isArray(sigs)) {
sigs = [sigs];
}
sigs = [].concat.apply([], sigs);
for (let i = 0; i < sigs.length; i++) {
const sig = sigs[i]
console.log(sig, i, sigs, sig.length)
if (typeof sig === 'string' && sig.length === 130) {
sigs[i] = ecc.Signature.from(sig).toString()
console.log(sigs)
}
}
return sigs
}