-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.js
More file actions
327 lines (296 loc) · 10.2 KB
/
Copy pathindex.js
File metadata and controls
327 lines (296 loc) · 10.2 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// QuarkChain client library built on web3.js
//
// Call QuarkChain.injectWeb3(web3, jrpcUrl) to inject qkc object.
// The functions provided by the web3.qkc object mirror the corresponding ones in web3.eth.
// One notable difference is that the qkc functions accepts QuarkChain address (48 hex chars).
//
// Available functions:
// getBalance: $QKC_ADDRESS
// getTransactionCount: $QKC_ADDRESS
// call: {to: $QKC_ADDRESS, ...}
// sendTransaction:
// {
// to: $QKC_ADDRESS,
// fromFullShardKey: $NUMBER,
// toFullShardKey: $NUMBER,
// ...
// }, nonce is fetched from network
// getTransactionReceipt: $TX_ID
// contract: $ABI
// new:
// at: $QKC_ADDRESS
//
// Example:
// QuarkChain.injectWeb3(web3, "http://jrpc.quarkchain.io:38391");
// const ethAddr = web3.eth.accounts[0];
// const qkcAddr = ethAddr + "00000001"; // Default to chain 0.
// web3.qkc.getBalance(qkcAddr).toString(10);
//
// Also for now this package needs `Web3` global variable from MetaMask, which means it can
// only work in browser.
import ethUtil from 'ethereumjs-util';
import Transaction from './quarkchain-ethereum-tx';
/* eslint-enable */
const defaultTokenSetting = {
transferTokenId: '0x8bb0',
gasTokenId: '0x8bb0',
};
function assert(condition, msg) {
if (!condition) {
throw msg;
}
}
function getFullShardKeyFromQkcAddress(qkcAddress) {
assert(qkcAddress.length === 50, 'Invalid qkc address');
return `0x${qkcAddress.slice(-8)}`;
}
function getEthAddressFromQkcAddress(qkcAddress) {
assert(qkcAddress.length === 50, 'Invalid qkc address');
return qkcAddress.slice(0, 42);
}
function getTypedTx(tx) {
const msgParams = [
{
type: 'uint256',
name: 'nonce',
value: `0x${tx.nonce.toString('hex')}`,
},
{
type: 'uint256',
name: 'gasPrice',
value: `0x${tx.gasPrice.toString('hex')}`,
},
{
type: 'uint256',
name: 'gasLimit',
value: `0x${tx.gasLimit.toString('hex')}`,
},
{
type: 'uint160',
name: 'to',
value: `0x${tx.to.toString('hex')}`,
},
{
type: 'uint256',
name: 'value',
value: `0x${tx.value.toString('hex')}`,
},
{
type: 'bytes',
name: 'data',
value: `0x${tx.data.toString('hex')}`,
},
{
type: 'uint256',
name: 'networkId',
value: `0x${tx.networkId.toString('hex')}`,
},
{
type: 'uint32',
name: 'fromFullShardKey',
value: `0x${tx.fromFullShardKey.toString('hex')}`,
},
{
type: 'uint32',
name: 'toFullShardKey',
value: `0x${tx.toFullShardKey.toString('hex')}`,
},
{
type: 'uint64',
name: 'gasTokenId',
value: `0x${tx.gasTokenId.toString('hex')}`,
},
{
type: 'uint64',
name: 'transferTokenId',
value: `0x${tx.transferTokenId.toString('hex')}`,
},
{
type: 'string',
name: 'qkcDomain',
value: 'bottom-quark',
},
];
return msgParams;
}
async function metaMaskSignTyped(from, web3in, tx) {
return new Promise((resolve, reject) => {
const params = [getTypedTx(tx), from];
const method = 'eth_signTypedData';
web3in.currentProvider.sendAsync(
{
method,
params,
from,
},
(err, result) => {
if (err) {
return reject(err);
}
if (result.error !== undefined) {
return reject(result.error);
}
return resolve(result.result);
},
);
});
}
function decodeSignature(sig) {
const ret = {};
const signature = sig.slice(2);
ret.r = ethUtil.toBuffer(`0x${signature.slice(0, 64)}`);
ret.s = ethUtil.toBuffer(`0x${signature.slice(64, 128)}`);
ret.v = ethUtil.toBuffer(`0x${signature.slice(128, 130)}`);
return ret;
}
function loadContract(abi, contractAddress, web3in, web3http) {
const web3contract = new Web3(web3http.currentProvider);
// Override call and sendTransaction to include full shard id
web3contract.eth.call = (obj, blockId, callback) => {
const ret = web3in.qkc.call(
Object.assign({}, obj, { to: contractAddress }),
callback,
);
return ret;
};
web3contract.eth.sendTransaction = async (obj, callback) => {
const ret = await web3in.qkc.sendTransaction(
Object.assign({}, obj, { to: contractAddress }),
callback,
);
return ret;
};
return web3contract.eth.contract(abi).at(contractAddress.slice(0, 42));
}
export default {
getFullShardKeyFromQkcAddress,
getEthAddressFromQkcAddress,
injectWeb3(web3in, jrpcUrl) {
// Inject QuarkChain specific logic into the provided web3 instance.
//
// The web3 instance passed in will be used for account management and signing transactions.
// It should have a provider implementing RPC eth_signTypedData (https://github.com/ethereum/EIPs/pull/712)
// Normally you should just pass in a web3 instance with provider from MetaMask.
//
// Args:
// web3in: web3 instance
// jrpcUrl: QuarkChain JSON RPC endpoint (e.g., http://localhost:38391)
const web3http = new Web3(new Web3.providers.HttpProvider(jrpcUrl));
const web3eth = web3in.eth || (new Web3(web3in.currentProvider)).eth;
Object.defineProperty(web3in, 'qkc', {
value: {
getBalance(qkcAddress, callback) {
const ethAddress = getEthAddressFromQkcAddress(qkcAddress);
const shard = getFullShardKeyFromQkcAddress(qkcAddress);
return web3http.eth.getBalance(ethAddress, shard, callback);
},
getTransactionCount(qkcAddress, callback) {
const ethAddress = getEthAddressFromQkcAddress(qkcAddress);
const shard = getFullShardKeyFromQkcAddress(qkcAddress);
return web3http.eth.getTransactionCount(ethAddress, shard, callback);
},
call(obj, callback) {
const qkcAddress = obj.to;
const rawTx = Object.assign({}, obj, {
to: getEthAddressFromQkcAddress(qkcAddress),
});
const shard = getFullShardKeyFromQkcAddress(qkcAddress);
return web3http.eth.call(rawTx, shard, callback);
},
setPrivateKey(privateKey) {
if (privateKey.length !== 66) {
throw new Error('Invalid key');
}
this.key = privateKey;
this.address = `0x${ethUtil.privateToAddress(ethUtil.toBuffer(privateKey)).toString('hex')}`;
},
unsetPrivateKey() {
this.key = null;
this.address = null;
},
async sendTransaction(obj, callback) {
let fromEthAddress;
if (this.address) {
fromEthAddress = this.address;
} else {
fromEthAddress = web3eth.accounts[0];
}
if (obj.fromFullShardKey === undefined || obj.toFullShardKey === undefined) {
throw new Error('`fromFullShardKey` and `toFullShardKey` are required');
}
const rawTx = Object.assign({}, defaultTokenSetting, obj);
if (obj.to !== undefined) {
if (obj.to.length === 42) {
rawTx.to = obj.to;
} else {
rawTx.to = getEthAddressFromQkcAddress(obj.to);
if (getFullShardKeyFromQkcAddress(obj.to) !== obj.toFullShardKey) {
throw new Error('Target shard key mismatch');
}
}
}
// FIXME: make this async
if (rawTx.nonce == undefined) { // eslint-disable-line
rawTx.nonce = web3http.eth.getTransactionCount(
fromEthAddress,
rawTx.fromFullShardKey,
);
}
if (!rawTx.networkId) {
// default network is devnet
rawTx.networkId = '0xff';
}
// rawTx.version is part of transaction sent to QuarkChain Network but not part of
// signature it determines the signature version:
// '0x0' RLP-encoded transaction of all fields in Transaction (minus version, v, r, s)
// '0x1' typed encoding matching MetaMask initial implementation of EIP-712
rawTx.version = '0x1';
const tx = new Transaction(rawTx);
if (this.key) {
// sign with a key
tx.version = '0x0';
tx.sign(ethUtil.toBuffer(this.key));
} else {
const from = web3eth.accounts[0];
const sig = await metaMaskSignTyped(from, web3in, tx);
Object.assign(tx, decodeSignature(sig));
}
const payload = `0x${tx.serialize().toString('hex')}`;
return web3http.eth.sendRawTransaction(payload, callback);
},
getTransactionReceipt: web3http.eth.getTransactionReceipt,
getCode(qkcAddress, callback) {
const ethAddress = getEthAddressFromQkcAddress(qkcAddress);
const shard = getFullShardKeyFromQkcAddress(qkcAddress);
return web3http.eth.getCode(ethAddress, shard, callback);
},
contract(abi) {
const contractFactory = web3eth.contract(abi);
const originalFactory = web3eth.contract(abi);
contractFactory.at = addr => loadContract(abi, addr, web3in, web3http);
contractFactory.new = (...args) => {
const size = args.length;
const callback = args[size - 1];
const newArguments = [...args.slice(0, -1)];
return originalFactory.new(...newArguments, (err, contract) => {
if (!contract) {
callback(err, contract);
}
if (!contract.address) {
// The transactionHash returned from eth_sendRawTransaction is already a tx id
contract.transactionId = contract.transactionHash; // eslint-disable-line
}
callback(err, contract);
});
};
contractFactory.new.getData = originalFactory.new.getData;
return contractFactory;
},
},
});
// Override to support `contract.new`
web3in.eth.sendTransaction = web3in.qkc.sendTransaction.bind(web3in.qkc); // eslint-disable-line
web3in.eth.getTransactionReceipt = web3in.qkc.getTransactionReceipt; // eslint-disable-line
web3in.eth.getCode = web3in.qkc.getCode; // eslint-disable-line
},
};