-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.ts
52 lines (44 loc) · 1.22 KB
/
transaction.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
import { ecdsaRecover, publicKeyConvert } from "secp256k1";
import { rlpEncode } from "./rlp";
import { bufferToBigInt, keccak256, pkToAddress, zfill } from "./util";
export class Transaction {
// nonce: P,
// gas-price: P,
// gas-limit: P,
// recipient: {B_0, B_20},
// value: P,
// data: B,
// V: P,
// R: P,
// S: P,
raw: Buffer[];
constructor (raw: Buffer[]) {
this.raw = raw;
}
nonce = () => bufferToBigInt(this.raw[0]);
gasPrice = () => bufferToBigInt(this.raw[1]);
gasLimit = () => bufferToBigInt(this.raw[2]);
recipient = () => this.raw[3];
value = () => bufferToBigInt(this.raw[4]);
data = () => this.raw[5];
publicKey = () => Buffer.from(
publicKeyConvert(
ecdsaRecover(
Buffer.concat([zfill(this.raw[7], 32), zfill(this.raw[8], 32)]),
this.raw[6][0] - 27,
keccak256(rlpEncode(this.raw.slice(0, 6)))
),
false
).slice(1)
)
origin = () => pkToAddress(this.publicKey());
intrinsicGas () {
const data = this.data();
let numZeroBytes = 0;
for (const byte of data) if (byte === 0) numZeroBytes++;
return 21000 + (data.length * 16) - (numZeroBytes * 12);
}
hash () {
return keccak256(rlpEncode(this.raw));
}
}