-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions.js
375 lines (292 loc) · 9.55 KB
/
transactions.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import _ from 'lodash'
import Joi from 'joi'
import sha256 from 'sha256'
import canonicalize from './canonicalize.js'
import { validateSignature, signMessage } from './utils.js'
import { logger, colorizeTransactionManager } from './logger.js'
import { ObjectManager } from './objects.js'
const COINBASE_BASE_AMOUNT = 50 * (10 ** 12)
const transactionSchema = Joi.object({
inputs: Joi.array().items(Joi.object({
outpoint: Joi.object({
txid: Joi.string().hex().required(),
index: Joi.number().integer().min(0).required()
}).required(),
sig: Joi.string().hex().required()
})).required(),
outputs: Joi.array().items(Joi.object({
pubkey: Joi.string().hex().required(),
value: Joi.number().integer().unsafe().min(0).required()
})).required()
})
const coinbaseTransactionSchema = Joi.object({
outputs: Joi.array().length(1).items(Joi.object({
pubkey: Joi.string().hex().required(),
value: Joi.number().integer().unsafe().min(0).required()
})).required(),
height: Joi.number().integer().min(1).required()
})
export class TransactionManager {
objectManager
constructor() {
this.objectManager = new ObjectManager()
}
getTransactionObject(transaction) {
const transactionObject = {
type: 'transaction',
outputs: transaction.outputs
}
if (transaction.inputs) {
transactionObject.inputs = transaction.inputs
}
return transactionObject
}
getTransactionHash(transaction) {
const transactionObject = this.getTransactionObject(transaction)
return sha256(canonicalize(transactionObject))
}
validateTransactionSignatures({ UTXO, transaction }) {
const messageObject = {
type: 'transaction',
inputs: transaction.inputs.map(input => ({outpoint: input.outpoint, sig: null })),
outputs: transaction.outputs
}
const isValid = _.every(transaction.inputs, input => {
const txid = input.outpoint.txid
const index = input.outpoint.index
const prevOutput = this.getOutputFromUTXO({ UTXO, txid, index })
if (!prevOutput) {
return false
}
const isValidSignature = validateSignature({ message: messageObject, signature: input.sig,
publicKey: prevOutput.pubkey })
if (!isValidSignature) {
return false
}
return true
})
if (!isValid) {
this.logger('Transaction failed signature validation')
return false
}
this.logger(`Successfully validated transaction's signatures`)
return true
}
validateTransactionSchema(transaction) {
const schemaValidation = transactionSchema.validate(transaction)
if (schemaValidation.error) {
this.logger('Transaction schema validation failed', transaction, schemaValidation.error)
return false
}
this.logger('Successfully validated transaction schema', transaction)
return true
}
validateTransactionConservation({ UTXO, transaction }) {
let inputAmount = 0
let outputAmount = 0
for (const input of transaction.inputs) {
const txid = input.outpoint.txid
const index = input.outpoint.index
const prevOutput = this.getOutputFromUTXO({ UTXO, txid, index })
if (!prevOutput) {
return false
}
inputAmount += prevOutput.value
}
transaction.outputs.forEach(output => {
outputAmount += output.value
})
if (inputAmount < outputAmount) {
this.logger('This transaction does not satisfy the law of conservation')
return false
}
const fee = inputAmount - outputAmount
this.logger(`Successfully validated law of conservation with fee ${fee} picabus`)
return { fee }
}
getNewUTXO({ UTXO, transaction }) {
const newUTXO = _.cloneDeep(UTXO)
if (transaction.inputs) {
transaction.inputs.forEach(input => {
const txid = input.outpoint.txid
const index = input.outpoint.index
try {
delete newUTXO[txid][index]
if (_.isEmpty(newUTXO[txid])) {
delete newUTXO[txid]
}
} catch {
return false
}
})
}
const transactionObject = this.getTransactionObject(transaction)
const transactionId = this.objectManager.getObjectHash(transactionObject)
transaction.outputs.forEach((output, outputIndex) => {
if (!newUTXO[transactionId]) {
newUTXO[transactionId] = {}
}
newUTXO[transactionId][outputIndex] = output
})
return newUTXO
}
validateTransaction({ UTXO, transaction }) {
this.logger('Validating transaction', transaction)
if (!this.validateTransactionSchema(transaction)) {
return false
}
if (!this.validateTransactionSignatures({ UTXO, transaction })) {
return false
}
if (!this.validateTransactionConservation({ UTXO, transaction })) {
return false
}
this.logger('Successfully validated the transaction', transaction)
return true
}
validateCoinbaseTransactionSchema(coinbaseTransaction) {
const schemaValidation = coinbaseTransactionSchema.validate(coinbaseTransaction)
if (schemaValidation.error) {
this.logger('Coinbase transaction schema validation failed: %O', schemaValidation.error)
return false
}
this.logger('Successfully validated coinbase transaction schema')
return true
}
validateCoinbaseAmount({ coinbaseTransaction, normalTransactions, UTXO }) {
this.logger('Validating coinbase amount')
const output = coinbaseTransaction.outputs[0]
const coinbaseAmount = output.value
let expectedAmount = COINBASE_BASE_AMOUNT
let currentUTXO = _.cloneDeep(UTXO)
for (const transaction of normalTransactions) {
if (!this.validateTransactionSchema(transaction)) {
this.logger(`Can not get transaction's fee`)
return false
}
const transactionConservation = this.validateTransactionConservation({ UTXO: currentUTXO, transaction })
if (!transactionConservation) {
this.logger(`Transaction's fee is a negative number`)
return false
}
expectedAmount += transactionConservation.fee
currentUTXO = this.getNewUTXO({ UTXO: currentUTXO, transaction })
}
if (expectedAmount < coinbaseAmount) {
this.logger('Coinbase amount is higher than it should be')
return false
}
this.logger('Successfully validated coinbase amount')
return true
}
validateCoinbaseHeight(coinbaseTransaction, height) {
if (coinbaseTransaction.height !== height) {
return false
}
return true
}
validateCoinbaseTransaction({ coinbaseTransaction, normalTransactions, UTXO, height }) {
this.logger('Validating coinbase transaction')
if (!this.validateCoinbaseTransactionSchema(coinbaseTransaction)) {
return false
}
if (!this.validateCoinbaseHeight(coinbaseTransaction, height)) {
return false
}
if (!this.validateCoinbaseAmount({ coinbaseTransaction, normalTransactions, UTXO })) {
return false
}
this.logger('Successfully validated coinbase transaction')
return true
}
getOutputFromUTXO({ UTXO, txid, index }) {
let outputObject = null
try {
outputObject = UTXO[txid][index]
} catch {
this.logger('Could not find output object in UTXO')
return false
}
if (!outputObject) {
this.logger('Could not find output object in UTXO')
return false
}
return outputObject
}
createCoinbase({ receiverPublicKey, blockTransactions, height }) {
return new Transaction({
type: "transaction",
outputs: [{
pubkey: receiverPublicKey,
value: 50 * 10**12
}],
height
})
}
createTransaction({ keyPair, UTXO, receiverPublicKey, amount, fee }) {
this.logger('Creating a transaction')
const transactionToSign = {
type: 'transaction',
inputs: [],
outputs: []
}
let totalAmount = 0
for (const [txid, outputs] of Object.entries(UTXO)) {
for (const [index, output] of Object.entries(outputs)) {
if (output.pubkey !== keyPair.publicKey) {
continue
}
transactionToSign.inputs.push({
outpoint: {
txid,
index
},
sig: null
})
totalAmount += output.value
}
}
let amountLeft = totalAmount - amount
if (fee) {
amountLeft -= fee
}
if (amountLeft < 0) {
logger.warn('Not enough balance to make the transaction')
throw 'not-enough-balance'
}
transactionToSign.outputs.push({
pubkey: receiverPublicKey,
value: amount
})
if (amountLeft > 0) {
this.logger(`This transaction has change (${amountLeft} picabus)`)
transactionToSign.outputs.push({
pubkey: keyPair.publicKey,
value: amountLeft
})
} else {
this.logger('This transaction does not have change')
}
const signedTransaction = _.cloneDeep(transactionToSign)
transactionToSign.inputs.forEach((input, inputIndex) => {
signedTransaction.inputs[inputIndex].sig = signMessage({ message: transactionToSign, privateKey: keyPair.privateKey })
})
this.logger('The transaction was signed')
return new Transaction(signedTransaction)
}
logger(message, ...args) {
logger.info(`${colorizeTransactionManager()}: ${message}`, ...args)
}
}
export class Transaction {
outputs
constructor(transaction) {
if (transaction?.inputs) {
this.inputs = transaction.inputs.map(input => input)
}
if (transaction?.height) {
this.height = transaction.height
}
this.outputs = transaction.outputs.map(output => output)
}
}