Skip to content

Commit

Permalink
convert from bech32 to base58 when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromtcosta committed Nov 23, 2022
1 parent 3b2ca5c commit 2988abf
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 11 deletions.
7 changes: 4 additions & 3 deletions src/neo4j/txs/io.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response } from "express";
import { Driver } from "neo4j-driver-core";
import { mapNeo4jAssets } from "../utils";
import { formatIOAddress } from "./utils";


export const io = (driver: Driver) => ({
Expand Down Expand Up @@ -33,7 +34,7 @@ export const io = (driver: Driver) => ({

const inputsForResponse = inputs.map((input: any) => {
return {
address: input.tx_out.properties.address,
address: formatIOAddress(input.tx_out.properties.address),
amount: input.tx_out.properties.amount.toNumber().toString(),
id: input.tx_out.properties.id.toString().replace(":", ""),
index: input.tx_out.properties.output_index.toNumber(),
Expand All @@ -46,7 +47,7 @@ export const io = (driver: Driver) => ({
const collateralInputsForResponse = collateralInputs.map((collateralInput: any) => {
if (collateralInput.tx_out) {
return {
address: collateralInput.tx_out.properties.address,
address: formatIOAddress(collateralInput.tx_out.properties.address),
amount: collateralInput.tx_out.properties.amount.toNumber().toString(),
id: collateralInput.tx_out.properties.id.toString().replace(":", ""),
index: collateralInput.tx_out.properties.output_index.toNumber(),
Expand All @@ -58,7 +59,7 @@ export const io = (driver: Driver) => ({

const outputsForResponse = outputs.map((output: any) => {
return {
address: output.properties.address,
address: formatIOAddress(output.properties.address),
amount: output.properties.amount.toNumber().toString(),
dataHash: (output.properties.datum_hash === undefined) ? null : output.properties.datum_hash,
assets: mapNeo4jAssets(output.properties.assets),
Expand Down
3 changes: 2 additions & 1 deletion src/neo4j/txs/ioByIndex.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response } from "express";
import { Driver } from "neo4j-driver-core";
import { mapNeo4jAssets } from "../utils";
import { formatIOAddress } from "./utils";


export const ioByIndex = (driver: Driver) => ({
Expand Down Expand Up @@ -28,7 +29,7 @@ export const ioByIndex = (driver: Driver) => ({
});

const outputsForResponse = {
address: foundInInputs.properties.address,
address: formatIOAddress(foundInInputs.properties.address),
amount: foundInInputs.properties.amount.toNumber().toString(),
dataHash: (foundInInputs.properties.datum_hash === undefined) ? null : foundInInputs.properties.datum_hash,
assets: mapNeo4jAssets(foundInInputs.properties.assets),
Expand Down
32 changes: 29 additions & 3 deletions src/neo4j/txs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Integer } from "neo4j-driver";
import { Driver } from "neo4j-driver-core";
import config from "config";
import {
Address,
ByronAddress,
Ed25519KeyHash,
NetworkInfo,
RewardAddress,
Expand Down Expand Up @@ -138,6 +140,30 @@ export const formatNeo4jCertificate = (cert: Neo4jModel.CERTIFICATE, block: Neo4
}
};

export const formatIOAddress = (addr?: string) => {
if (!addr) return addr;

if (ByronAddress.is_valid(addr)) {
return addr;
}

if (addr.startsWith("addr") || addr.startsWith("addr_test")) {
const address = Address.from_bech32(addr);
const hex = Buffer.from(address.to_bytes()).toString("hex");

if (hex.startsWith("8")) {
const byronAddress = ByronAddress.from_address(address);
if (!byronAddress) return addr;

return byronAddress.to_base58();
}

return addr;
}

return addr;
};

export const neo4jTxDataToResponseTxData = (records: any) => {
return records.map((r: any) => {
const tx = neo4jCast<Neo4jModel.TX>(r.get("tx"));
Expand Down Expand Up @@ -193,7 +219,7 @@ export const neo4jTxDataToResponseTxData = (records: any) => {
epoch: neo4jBigNumberAsNumber(block.epoch),
slot: neo4jBigNumberAsNumber(block.epoch_slot),
inputs: inputs.map(i => ({
address: i.tx_out?.address,
address: formatIOAddress(i.tx_out?.address),
amount: i.tx_out ?
formatNeo4jBigNumber(i.tx_out.amount)
: null,
Expand All @@ -203,7 +229,7 @@ export const neo4jTxDataToResponseTxData = (records: any) => {
assets: mapNeo4jAssets(i.tx_out?.assets)
})),
collateral_inputs: collateralInputs.map(i => ({
address: i.tx_out?.address,
address: formatIOAddress(i.tx_out?.address),
amount: i.tx_out
? formatNeo4jBigNumber(i.tx_out.amount)
: null,
Expand All @@ -213,7 +239,7 @@ export const neo4jTxDataToResponseTxData = (records: any) => {
assets: mapNeo4jAssets(i.tx_out?.assets)
})),
outputs: outputs.map(o => ({
address: o.address,
address: formatIOAddress(o.address),
amount: formatNeo4jBigNumber(o.amount),
dataHash: o.datum_hash ?? null,
assets: mapNeo4jAssets(o.assets)
Expand Down
3 changes: 2 additions & 1 deletion src/neo4j/txs/utxoAtPoint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response } from "express";
import { Integer, Driver } from "neo4j-driver-core";
import { mapNeo4jAssets } from "../utils";
import { formatIOAddress } from "./utils";

export const utxoAtPoint = (driver: Driver) => ({
handler: async (req: Request, res: Response) => {
Expand Down Expand Up @@ -76,7 +77,7 @@ export const utxoAtPoint = (driver: Driver) => ({
utxo_id: utxo.utxo_id,
tx_hash: utxo.tx_hash,
tx_index: utxo.tx_index.toNumber(),
receiver: utxo.receiver,
receiver: formatIOAddress(utxo.receiver),
amount: utxo.amount.toNumber().toString(),
assets: mapNeo4jAssets(utxo.assets),
block_num: utxo.block_num.toNumber()
Expand Down
4 changes: 2 additions & 2 deletions src/neo4j/txs/utxoDiffSincePoint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Driver, Integer } from "neo4j-driver";
import { Request, Response } from "express";
import { getAddressesByType, mapNeo4jAssets } from "../utils";
import { formatNeo4jBigNumber, getPaginationParameters } from "./utils";
import { formatIOAddress, formatNeo4jBigNumber, getPaginationParameters } from "./utils";

enum DiffItemType {
INPUT = "input",
Expand Down Expand Up @@ -383,7 +383,7 @@ export const utxoDiffSincePoint = (driver: Driver) => ({
linearized.push({
type: DiffItemType.OUTPUT,
id: `${obj.hash}:${obj.index}`,
receiver: obj.address,
receiver: formatIOAddress(obj.address),
amount: formatNeo4jBigNumber(obj.value),
assets: mapNeo4jAssets(obj.assets),
block_num: obj.blockNumber.toNumber(),
Expand Down
3 changes: 2 additions & 1 deletion src/neo4j/txs/utxoForAddresses.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response } from "express";
import { Driver } from "neo4j-driver-core";
import { getAddressesByType, mapNeo4jAssets } from "../utils";
import { formatIOAddress } from "./utils";

export const utxoForAddresses = (driver: Driver) => ({
handler: async (req: Request, res: Response) => {
Expand Down Expand Up @@ -48,7 +49,7 @@ export const utxoForAddresses = (driver: Driver) => ({
utxo_id: utxo.utxo_id,
tx_hash: utxo.tx_hash,
tx_index: utxo.tx_index.toNumber(),
receiver: utxo.receiver,
receiver: formatIOAddress(utxo.receiver),
amount: utxo.amount.toNumber().toString(),
dataHash: utxo.dataHash,
assets: mapNeo4jAssets(utxo.assets),
Expand Down

0 comments on commit 2988abf

Please sign in to comment.