-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
231 lines (208 loc) · 6.75 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
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
const { generators, signers } = require('eth-signer')
const TxRelaySigner = signers.TxRelaySigner
const SimpleSigner = signers.SimpleSigner
const Web3 = require('web3')
const Transaction = require('ethereumjs-tx')
class MetaTxHandler {
constructor (relayerPrivKey, provider, txRelayAddress, txRelayABI, logger) {
if (!relayerPrivKey) throw new Error('relayerPrivKey is required')
this.privKey = relayerPrivKey
this.txRelayAddress = txRelayAddress
this.web3 = new Web3(provider)
this.BN = this.web3.utils.BN
this.TxRelayContract = new this.web3.eth.Contract(
txRelayABI,
txRelayAddress
)
this.logger = logger
}
getRelayerAddress () {
return this.TxRelayContract.options.address
}
async getRelayNonce (address) {
if (!address) throw new Error('no address')
try {
const nonce = await this.TxRelayContract.methods.getNonce(address).call()
} catch (err) {
console.log(err)
}
return nonce.toString(16)
}
initSimpleSigner () {
const signer = new SimpleSigner(generators.KeyPair.fromPrivateKey(this.privKey))
return signer
}
getSenderKeyPair (senderPrivKey) {
if (!senderPrivKey) throw new Error("sender's private key is required")
return generators.KeyPair.fromPrivateKey(senderPrivKey);
}
initTxRelaySigner (senderPrivKey, _whitelist) {
const keyPair = this.getSenderKeyPair(senderPrivKey)
const whitelist = _whitelist ? _whitelist : '0x0000000000000000000000000000000000000000'
const signer = new TxRelaySigner(
keyPair,
this.txRelayAddress,
keyPair.address,
whitelist
);
return signer
}
async estimateGas (tx, from) {
if (!tx) throw new Error('no tx object')
const txCopy = {
nonce: `0x${tx.nonce.toString('hex') || 0}`,
gasPrice: `0x${tx.gasPrice.toString('hex')}`,
to: `0x${tx.to.toString('hex')}`,
value: `0x${tx.value.toString('hex') || 0}`,
data: `0x${tx.data.toString('hex')}`,
from
}
let price = 3000000
try {
price = await this.web3.eth.estimateGas(txCopy)
} catch (err) {
throw err
}
return new this.BN(price)
}
async isMetaSignatureValid (metaSignedTx, metaNonce) {
if (!metaSignedTx) throw new Error('no metaSignedTx')
let decodedTx
let relayerAddress
try {
relayerAddress = await this.getRelayerAddress()
decodedTx = TxRelaySigner.decodeMetaTx(metaSignedTx)
} catch (error) {
if (this.logger) {
this.logger.error('Error on TxRelaySigner.decodeMetaTx or getRelayerAddress')
this.logger.error(error)
} else {
console.error('Error on TxRelaySigner.decodeMetaTx or getRelayerAddress')
console.error(error)
}
return false
}
if (decodedTx.claimedAddress === '0x') {
this.logger
? this.logger.info('no claimedAddress')
: console.log('no claimedAddress')
return false
}
try {
this.logger
? this.logger.info(
`trying to validate metasig, relayerAddress is ${relayerAddress}`
)
: console.log(
`trying to validate metasig, relayerAddress is ${relayerAddress}`
)
const validMetaSig = TxRelaySigner.isMetaSignatureValid(
relayerAddress,
decodedTx,
metaNonce
)
return validMetaSig
} catch (error) {
if (this.logger) {
this.logger.error('Error on TxRelaySigner.isMetaSignatureValid')
this.logger.error(error)
} else {
console.error('Error on TxRelaySigner.isMetaSignatureValid')
console.error(error)
}
return false
}
}
async signMetaTx (txParams, senderPrivKey, relayNonce, whitelist) {
let nonce
if (!relayNonce) {
const sender = this.getSenderKeyPair(senderPrivKey)
nonce = await this.getRelayNonce(sender.address)
} else { nonce = relayNonce }
const signer = this.initTxRelaySigner(senderPrivKey, whitelist)
txParams.nonce = this.web3.utils.toHex(nonce);
const tx = new Transaction(txParams);
const rawTx = `0x${tx.serialize().toString('hex')}`;
return new Promise((resolve, reject) => {
signer.signRawTx(rawTx, (err, metaSignedTx) => {
if (err) reject(err)
// if (this.logger) {
// this.logger.info(params)
// } else {
// console.log(params);
// }
resolve(metaSignedTx);
});
});
};
async signRelayerTx (txHex) {
if (!txHex) throw new Error('no txHex')
const tx = new Transaction(Buffer.from(txHex, 'hex'))
const signer = this.initSimpleSigner()
const price = await this.web3.eth.getGasPrice()
tx.gasPrice = new this.BN(price).toNumber()
tx.nonce = await this.web3.eth.getTransactionCount(signer.getAddress())
const estimatedGas = await this.estimateGas(tx, signer.getAddress())
tx.gasLimit = estimatedGas.add(new this.BN(1000000))
const rawTx = tx.serialize().toString('hex')
return new Promise((resolve, reject) => {
signer.signRawTx(rawTx, (error, signedRawTx) => {
if (error) reject(error)
resolve(signedRawTx)
})
})
}
async sendRawTransaction (signedRawTx) {
if (!signedRawTx) throw new Error('no signedRawTx')
if (!signedRawTx.startsWith('0x')) {
signedRawTx = `0x${signedRawTx}`
}
return new Promise((resolve, reject) => {
this.web3.eth.sendSignedTransaction(signedRawTx, (error, txHash) => {
if (error) reject(error)
resolve(txHash)
})
})
}
async handle (req) {
const body = req.body || JSON.parse(req.body)
if (!body.metaSignedTx) {
throw { code: 400, message: 'metaSignedTx parameter missing' }
}
// support hex strings starting with 0x
if (body.metaSignedTx.startsWith('0x')) {
body.metaSignedTx = body.metaSignedTx.slice(2)
}
// Check if metaTx signature is valid
if (!(await this.isMetaSignatureValid(body.metaSignedTx, body.metaNonce))) {
throw { code: 403, message: 'MetaTx signature invalid' }
}
let signedRawTx
try {
signedRawTx = await this.signRelayerTx(body.metaSignedTx)
} catch (error) {
if (this.logger) {
this.logger.error('Error signing transaction')
this.logger.error(error)
} else {
console.error('Error signing transaction')
console.error(error)
}
throw { code: 500, message: error.message }
}
try {
const txHash = await this.sendRawTransaction(signedRawTx)
return txHash
} catch (error) {
if (this.logger) {
this.logger.error('Error on sendRawTransaction')
this.logger.error(error)
} else {
console.error('Error on sendRawTransaction')
console.error(error)
}
throw { code: 500, message: error.message }
}
}
}
module.exports = MetaTxHandler