-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmining.ts
81 lines (70 loc) · 2.11 KB
/
mining.ts
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
import BN = require("bn.js");
import ABI = require("ethereumjs-abi");
import { Context } from "./context";
import { MultiHashUtil } from "./multihash";
import { OrderUtil } from "./order";
import { Ring } from "./ring";
import { OrderInfo, TransferItem } from "./types";
export class Mining {
public feeRecipient: string;
public miner?: string;
public sig?: string;
public hash?: Buffer;
public interceptor?: string;
private context: Context;
constructor(context: Context,
feeRecipient: string,
miner: string,
sig: string) {
this.context = context;
this.feeRecipient = feeRecipient;
this.miner = miner;
this.sig = sig;
}
public async updateMinerAndInterceptor() {
if (!this.miner) {
this.miner = this.feeRecipient;
} else {
// const [registered, interceptor] = await this.context.minerBrokerRegistry.getBroker(
// this.feeRecipient,
// this.miner,
// );
// // assert(registered, "miner unregistered");
// if (registered) {
// this.interceptor = interceptor;
// }
}
}
public updateHash(rings: Ring[]) {
let ringHashes = rings[0].hash;
for (let i = 1; i < rings.length; i++) {
ringHashes = this.xor(ringHashes, rings[i].hash);
}
const args = [
this.feeRecipient,
this.miner ? this.miner : "0x0",
"0x" + ringHashes.toString("hex"),
];
const argTypes = [
"address",
"address",
"bytes32",
];
this.hash = ABI.soliditySHA3(argTypes, args);
}
public checkMinerSignature(transactionOrigin: string) {
const multiHashUtil = new MultiHashUtil();
if (!this.sig) {
return (transactionOrigin === this.miner);
} else {
return multiHashUtil.verifySignature(this.miner, this.hash, this.sig);
}
}
private xor(s1: Buffer, s2: Buffer) {
assert(s1.length === s2.length, "can't xor buffers of unequal length");
const x1 = new BN(s1.toString("hex"), 16);
const x2 = new BN(s2.toString("hex"), 16);
const result = x1.xor(x2);
return new Buffer(result.toString(16, s1.length), "hex");
}
}