Skip to content

Commit 7442b19

Browse files
committed
feat: update bridge memoHex decode mechanism
1 parent 76b2456 commit 7442b19

File tree

2 files changed

+38
-31
lines changed

2 files changed

+38
-31
lines changed

main/api/chainport/vendor/metadata.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ export class ChainportMemoMetadata {
4444
return String.fromCharCode(num - 10 + "a".charCodeAt(0));
4545
}
4646

47+
public static convertHexToBinary(encodedHex: string): string {
48+
const buffer = Buffer.from(encodedHex, "hex");
49+
let binaryString = "";
50+
for (let i = 0; i < buffer.length; i++) {
51+
binaryString += buffer[i].toString(2).padStart(8, "0");
52+
}
53+
return binaryString;
54+
}
55+
4756
/**
4857
* Decode the encoded hex string into a network id, address, and toIronfish flag
4958
* @param encodedHex - The encoded hex string
@@ -70,12 +79,30 @@ export class ChainportMemoMetadata {
7079
}
7180

7281
public static decodeV2(encodedHex: string): [number, string, boolean] {
73-
const bytes = Buffer.from(encodedHex, "hex");
74-
const networkId = bytes.readUInt8(0);
75-
const addressBytes = bytes.subarray(1, 21);
76-
const address = "0x" + addressBytes.toString("hex");
77-
const toIronfish = (bytes[21] & 0x80) !== 0;
82+
const bits = this.convertHexToBinary(encodedHex);
83+
const toIronfish = bits[6] === "1";
84+
const memoHexVersion = bits.slice(8, 10);
85+
if (memoHexVersion !== "01") {
86+
throw new Error(`Unexpected memoHex version: ${memoHexVersion}`);
87+
}
88+
89+
const networkIdBits = bits.slice(10, 16);
90+
const networkId = parseInt(networkIdBits, 2);
91+
const addressBits = bits.slice(16, 176);
92+
let address = "0x";
93+
for (let i = 0; i < addressBits.length; i += 4) {
94+
address += parseInt(addressBits.slice(i, i + 4), 2).toString(16);
95+
}
7896

7997
return [networkId, address.toLowerCase(), toIronfish];
8098
}
99+
100+
public static decode(encodedHex: string): [number, string, boolean] {
101+
const bits = this.convertHexToBinary(encodedHex);
102+
const memoHexVersion = bits.slice(8, 10);
103+
if (memoHexVersion === "01") {
104+
return this.decodeV2(encodedHex);
105+
}
106+
return this.decodeV1(encodedHex);
107+
}
81108
}

main/api/chainport/vendor/utils.ts

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,9 @@ const getIncomingChainportTransactionData = (
4747
return undefined;
4848
}
4949

50-
let sourceNetwork: number;
51-
let address: string;
52-
let _toIronfish: boolean;
53-
54-
if (new Date(transaction.timestamp) < config.bridgeFeeUpgrade) {
55-
[sourceNetwork, address, _toIronfish] = ChainportMemoMetadata.decodeV1(
56-
bridgeNote.memoHex,
57-
);
58-
} else {
59-
[sourceNetwork, address, _toIronfish] = ChainportMemoMetadata.decodeV2(
60-
bridgeNote.memoHex,
61-
);
62-
}
50+
const [sourceNetwork, address, _] = ChainportMemoMetadata.decode(
51+
bridgeNote.memoHex,
52+
);
6353

6454
return {
6555
type: TransactionType.RECEIVE,
@@ -90,19 +80,9 @@ const getOutgoingChainportTransactionData = (
9080
return undefined;
9181
}
9282

93-
let sourceNetwork: number;
94-
let address: string;
95-
let _toIronfish: boolean;
96-
97-
if (new Date(transaction.timestamp) < config.bridgeFeeUpgrade) {
98-
[sourceNetwork, address, _toIronfish] = ChainportMemoMetadata.decodeV1(
99-
bridgeNote.memoHex,
100-
);
101-
} else {
102-
[sourceNetwork, address, _toIronfish] = ChainportMemoMetadata.decodeV2(
103-
bridgeNote.memoHex,
104-
);
105-
}
83+
const [sourceNetwork, address, _] = ChainportMemoMetadata.decode(
84+
bridgeNote.memoHex,
85+
);
10686

10787
return {
10888
type: TransactionType.SEND,

0 commit comments

Comments
 (0)