Skip to content

Commit 02b66ac

Browse files
Lavinia TalpasLavinia Talpas
authored andcommitted
Add decoder for blockExtraData
1 parent 129a616 commit 02b66ac

File tree

1 file changed

+252
-0
lines changed

1 file changed

+252
-0
lines changed
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
import "dotenv/config"
2+
import { Buffer } from "../../src"
3+
import { Tx } from "../../src/apis/evm"
4+
import { Serialization } from "../../src/utils"
5+
import { SerializedType } from "../../src/utils"
6+
import * as bech32 from "bech32"
7+
import "dotenv/config"
8+
9+
const cb58: SerializedType = "cb58"
10+
const serialization: Serialization = Serialization.getInstance()
11+
12+
const getTxData = (item: string) => {
13+
const txSplit = item.split("0x000000000001")
14+
const prefix = "0x0000"
15+
const txData = prefix + txSplit[1]
16+
return txData
17+
}
18+
19+
const fromDecToHex = (item: number) => {
20+
let hexVal = item.toString(16)
21+
let hexString = hexVal.length < 2 ? "0" + hexVal : hexVal
22+
return hexString
23+
}
24+
const fromHexToDec = (item: string) => {
25+
let hexString = item.split("0x").join("")
26+
let decNumber = parseInt(hexString, 16)
27+
let value = decNumber / 10 ** 9
28+
return value
29+
}
30+
const toHexThenDec = (item: number) => {
31+
let toHex = fromDecToHex(item).split(",").join("")
32+
let hexString = toHex.split("0x").join("")
33+
let decNumber = parseInt(hexString, 16)
34+
return decNumber
35+
}
36+
const bufToHex = (item: string) => {
37+
let valueFromJSON = item
38+
let bufValueFromJson = Buffer.from(valueFromJSON)
39+
let arrValueFromJSON = [...bufValueFromJson]
40+
let hexValueFromJSON = arrValueFromJSON.map((item) => fromDecToHex(item))
41+
return "0x" + hexValueFromJSON.toString().split(",").join("")
42+
}
43+
const bech32Encoder = (item: string) => {
44+
const hrp = "avax"
45+
let valueFromJSON = item
46+
let bufValueFromJson = Buffer.from(valueFromJSON)
47+
let arrValueFromJSON = [...bufValueFromJson]
48+
let bech32Address = bech32.bech32.encode(
49+
hrp,
50+
bech32.bech32.toWords(arrValueFromJSON)
51+
)
52+
return "C-" + bech32Address
53+
}
54+
const base58Encoder = (item: string) => {
55+
let valToBeEncoded = Buffer.from(item)
56+
let base58Val: string = serialization.bufferToType(valToBeEncoded, cb58)
57+
return base58Val
58+
}
59+
const chainName = (item: string) => {
60+
const chainID = base58Encoder(item)
61+
let name: string = "null"
62+
const cchainID = "2q9e4r6Mu3U68nU1fYjgbR6JvwrRx36CohpAX5UQxse55x1Q5"
63+
const pchainID = "11111111111111111111111111111111LpoYY"
64+
chainID == "11111111111111111111111111111111LpoYY"
65+
? (name = "P-Chain")
66+
: (name = "X-Chain")
67+
chainID == cchainID
68+
? (name = "C-Chain")
69+
: chainID == pchainID
70+
? name == "P-Chain"
71+
: (name = "X-Chain")
72+
return name
73+
}
74+
75+
const main = async (): Promise<any> => {
76+
const blockExtraData: string =
77+
"0x00000000000100000001000000010427d4b22a2a78bcddd456742caf91b56badbff985ee19aef14573e7343fd652000000000000000000000000000000000000000000000000000000000000000000000001bb900bbe1a20da4d474666b79a5fa6ce1262973300000000009dba8421e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff00000000000000370000000121e67317cbc4be2aeb00677ad6462778a8f52274b9d605df2591b23027a87dff000000070000000000989680000000000000000000000001000000015feaa6c211cc8376e16211a76eff1e88bad8079d000000010000000900000001f526c9a38a2da08291583bf86e5160bd8b49df585b3fc2fb57884390c673f748428c58e95c6514b9d6a27d273550c63070ab64d257798e8d07f8a208489ebb2100"
78+
const txData = getTxData(blockExtraData)
79+
const buf: Buffer = new Buffer(txData.slice(2), "hex")
80+
const tx: Tx = new Tx()
81+
tx.fromBuffer(buf)
82+
const txString: string = JSON.stringify(tx)
83+
const txToObject = JSON.parse(txString)
84+
85+
let displayExportTx = () => {
86+
//exportTx
87+
let exportedTxInputs = txToObject.unsignedTx.transaction.inputs.map(
88+
(input) => ({
89+
Address: bufToHex(input.address.data),
90+
Amount: bufToHex(input.amount.data),
91+
AmountValue: "0x" + input.amountValue,
92+
DecimalAmountValue: fromHexToDec(input.amountValue) + " AVAX",
93+
AssetID: base58Encoder(input.assetID.data),
94+
Nonce: bufToHex(input.nonce.data),
95+
NonceValue: input.nonceValue,
96+
SignaturesCount: toHexThenDec(input.sigCount.data),
97+
SignaturesIDs: input.sigIdxs
98+
})
99+
)
100+
let exportedTxExpOutputs =
101+
txToObject.unsignedTx.transaction.exportedOutputs.map((out) => ({
102+
Type: out._typeName,
103+
AssetID: base58Encoder(out.assetID.data),
104+
Output: {
105+
Type: out.output._typeName,
106+
TypeID: out.output._typeID,
107+
Locktime: toHexThenDec(out.output.locktime.data),
108+
Threshold: toHexThenDec(out.output.threshold.data),
109+
NumberOfAddresses: toHexThenDec(out.output.numaddrs.data),
110+
Addresses: out.output.addresses.map((address) => ({
111+
Type: address._typeName,
112+
Bytes: bufToHex(address.bytes.data),
113+
BytesSize: address.bsize,
114+
Bech32Format: bech32Encoder(address.bytes.data)
115+
})),
116+
Amount: bufToHex(out.output.amount),
117+
AmountValue: "0x" + out.output.amountValue,
118+
DecimalAmountValue: fromHexToDec(out.output.amountValue) + " AVAX"
119+
}
120+
}))
121+
let exportedTxCredentials = txToObject.credentials.map((credential) => ({
122+
Type: credential._typeName,
123+
TypeID: credential._typeID,
124+
Signatures: credential.sigArray.map((signature) => ({
125+
Type: signature._typeName,
126+
Bytes: bufToHex(signature.bytes.data),
127+
BytesSize: signature.bsize
128+
}))
129+
}))
130+
let exportTx = {
131+
Type: txToObject._typeName,
132+
UnsignedTx: {
133+
Type: txToObject.unsignedTx._typeName,
134+
CodecID: txToObject.unsignedTx.codecID,
135+
Transaction: {
136+
Type: txToObject.unsignedTx.transaction._typeName,
137+
TypeID: txToObject.unsignedTx.transaction._typeID,
138+
NetworkID: toHexThenDec(
139+
txToObject.unsignedTx.transaction.networkID.data
140+
),
141+
BlockchainID: base58Encoder(
142+
txToObject.unsignedTx.transaction.blockchainID.data
143+
),
144+
BlockchainIDName: chainName(
145+
txToObject.unsignedTx.transaction.blockchainID.data
146+
),
147+
DestinationChain: base58Encoder(
148+
txToObject.unsignedTx.transaction.destinationChain.data
149+
),
150+
DestinationChainName: chainName(
151+
txToObject.unsignedTx.transaction.destinationChain.data
152+
),
153+
NumberOfInputs: toHexThenDec(
154+
txToObject.unsignedTx.transaction.numInputs.data
155+
),
156+
Inputs: exportedTxInputs,
157+
NumberOfExportedOutputs: toHexThenDec(
158+
txToObject.unsignedTx.transaction.numExportedOutputs.data
159+
),
160+
ExportedOutputs: exportedTxExpOutputs
161+
}
162+
},
163+
Credentials: exportedTxCredentials
164+
}
165+
console.log(require("util").inspect(exportTx, true, 10))
166+
}
167+
168+
let displayImportTx = () => {
169+
//importTX
170+
let importedTxImpInputs = txToObject.unsignedTx.transaction.importIns.map(
171+
(inp) => ({
172+
Type: inp._typeName,
173+
TransactionId: base58Encoder(inp.txid.data),
174+
OutputId: toHexThenDec(inp.outputidx.data),
175+
AssetID: base58Encoder(inp.assetID.data),
176+
Input: {
177+
Type: inp.input._typeName,
178+
TypeID: inp.input._typeID,
179+
SignaturesIds: inp.input.sigIdxs.map((signature) => ({
180+
Type: signature._typeName,
181+
Source: bufToHex(signature.source),
182+
Bytes: bufToHex(signature.bytes.data),
183+
BytesSize: signature.bsize
184+
})),
185+
Amount: bufToHex(inp.input.amount),
186+
AmountValue: "0x" + inp.input.amountValue,
187+
DecimalAmountValue: fromHexToDec(inp.input.amountValue) + " AVAX"
188+
}
189+
})
190+
)
191+
let importedTxOutputs = txToObject.unsignedTx.transaction.outs.map(
192+
(out) => ({
193+
Address: bufToHex(out.address.data),
194+
Amount: bufToHex(out.amount.data),
195+
AmountValue: "0x" + out.amountValue,
196+
DecimalAmountValue: fromHexToDec(out.amountValue) + " AVAX",
197+
AssetID: base58Encoder(out.assetID.data)
198+
})
199+
)
200+
let importedTxCredentials = txToObject.credentials.map((credential) => ({
201+
Type: credential._typeName,
202+
TypeID: credential._typeID,
203+
Signatures: credential.sigArray.map((signature) => ({
204+
Type: signature._typeName,
205+
Bytes: bufToHex(signature.bytes.data),
206+
BytesSize: signature.bsize
207+
}))
208+
}))
209+
let importTx = {
210+
Type: txToObject._typeName,
211+
UnsignedTx: {
212+
Type: txToObject.unsignedTx._typeName,
213+
CodecID: txToObject.unsignedTx.codecID,
214+
Transaction: {
215+
Type: txToObject.unsignedTx.transaction._typeName,
216+
TypeID: txToObject.unsignedTx.transaction._typeID,
217+
NetworkID: toHexThenDec(
218+
txToObject.unsignedTx.transaction.networkID.data
219+
),
220+
BlockchainID: base58Encoder(
221+
txToObject.unsignedTx.transaction.blockchainID.data
222+
),
223+
BlockchainIDName: chainName(
224+
txToObject.unsignedTx.transaction.blockchainID.data
225+
),
226+
SourceChain: base58Encoder(
227+
txToObject.unsignedTx.transaction.sourceChain.data
228+
),
229+
SourceChainName: chainName(
230+
txToObject.unsignedTx.transaction.sourceChain.data
231+
),
232+
NumberOfImportedInputs: toHexThenDec(
233+
txToObject.unsignedTx.transaction.numIns.data
234+
),
235+
ImportedInputs: importedTxImpInputs,
236+
NumberOfOutputs: toHexThenDec(
237+
txToObject.unsignedTx.transaction.numOuts.data
238+
),
239+
Outputs: importedTxOutputs
240+
}
241+
},
242+
Credentials: importedTxCredentials
243+
}
244+
console.log(require("util").inspect(importTx, true, 10))
245+
}
246+
247+
txToObject.unsignedTx.transaction._typeName == "ExportTx"
248+
? displayExportTx()
249+
: displayImportTx()
250+
}
251+
252+
main()

0 commit comments

Comments
 (0)