The parse now we used is

1
2
3
4
5
6
7
8
9
10
11
const parser = (content: string) => {
const bytes = hexToBytes(content)
const decoded = pb.UnverifiedTransaction.deserializeBinary(bytes)
const tx = decoded.getTransaction()
return {
from: tx.getFrom ? tx.getFrom() : '',
to: tx.getTo ? tx.getTo() : '',
data: tx.getData ? tx.getData() : '',
value: tx.getValue ? tx.getValue().toString() : ''
}
}

Here we use these methods:

  • hexToBytes(content) => bytes

  • pb.UnverifiesTransaction.deserializeBinary(bytes) => decoded

  • decoded.getTransaction() => transaction

  • transaction.{getFrom, getTo, getValue, getData}

hexToBytes
1
2
3
4
5
6
7
8
9
const hexToBytes = (hex: string) => {
let _hex = hex.startsWith('0x') ? hex.slice(2) : hex // remove 0x prefix
let result = []
while (_hex.length >= 2) {
result.push(parseInt(_hex.substring(0, 2), 16)) // parse hex to bytes(int8 array)
_hex = _hex.substring(2, _hex.length)
}
return result
}
pb.UnverifiedTransaction.deserializeBinary
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
const jspb = require('google-protobuf')
const goog = jspb

proto.UnverifiedTransaction = opt_data => {
jspb.Message.initialize(this, opt_data, 0, -1, null, null)
}

proto.UnverifiedTransaction.deserializeBinaryFromReader = (msg, reader) => {
while (reader.nextField()) {
if (reader.isEndGroup()) {
break
}

let field = reader.getFieldNumber()
switch (field) {
case 1: {
let value = new proto.Transaction()
reader.readMessage(value.proto.Transaction.deserializeBinaryFromReader)
msg.setTransaction(value)
}
case 2: {
let value = /** @type {!Uint8Array} */ (reader.readBytes())
msg.setSignature(value)
break
}
case 3: {
let value = /** @type {!proto.Crypto} */ (reader.readEnum())
msg.setCrypto(value)
break
}
default: {
reader.skipField()
break
}
}
}
return msg
}

proto.UnverifiedTransaction.deserializeBinary = bytes => {
const reader = new jspb.BinaryReader(bytes)
const msg = new proto.UnverifiedTransaction()
return proto.UnverifiedTransaction.deserializeBinaryFromReader(msg, reader)
}