Browse Source

clients/js: right-pad coin symbol/name

previously these were left-padded
Csongor Kiss 3 years ago
parent
commit
73e6d6865f

+ 1 - 1
clients/js/parse_tests/nft-bridge-transfer-1.expected

@@ -20,5 +20,5 @@
     toAddress: '0x0000000000000000000000000000000000000000000000000000000000000004',
     chain: 10
   },
-  digest: '0xe2a7a0f5d67018c74683851fca870403455f89d9f474e2af104740e31a00da63'
+  digest: '0x5e09cb958c8ee111319472907c3772f63bf4cc599b7126b1ef1bbac82f2fea7a'
 }

+ 1 - 1
clients/js/parse_tests/token-bridge-attestation-1.expected

@@ -23,5 +23,5 @@
     symbol: 'WETH',
     name: 'Wrapped ether'
   },
-  digest: '0x4bb52b9a44ff6062ba5db1c47afc40c186f7485c8972b1c6261eb070ce0b1c6e'
+  digest: '0x6793c77cc9283df50ab5f2cdd688637d6ba935d4e6baabf46e07b83c55655461'
 }

+ 10 - 4
clients/js/vaa.ts

@@ -593,8 +593,8 @@ function serialiseTokenBridgeAttestMeta(payload: TokenBridgeAttestMeta): string
         encode("bytes32", hex(payload.tokenAddress)),
         encode("uint16", payload.tokenChain),
         encode("uint8", payload.decimals),
-        encode("bytes32", encodeString(payload.symbol)),
-        encode("bytes32", encodeString(payload.name)),
+        encode("bytes32", encodeStringRight(payload.symbol)),
+        encode("bytes32", encodeStringRight(payload.name)),
     ]
     return body.join("")
 }
@@ -742,8 +742,8 @@ function serialiseNFTBridgeTransfer(payload: NFTBridgeTransfer): string {
         encode("uint8", 1),
         encode("bytes32", hex(payload.tokenAddress)),
         encode("uint16", payload.tokenChain),
-        encode("bytes32", encodeString(payload.tokenSymbol)),
-        encode("bytes32", encodeString(payload.tokenName)),
+        encode("bytes32", encodeStringRight(payload.tokenSymbol)),
+        encode("bytes32", encodeStringRight(payload.tokenName)),
         encode("uint256", payload.tokenId),
         encode("uint8", payload.tokenURI.length),
         Buffer.from(payload.tokenURI, "utf8").toString("hex"),
@@ -805,6 +805,12 @@ export function encodeString(str: string): Buffer {
     return Buffer.from(Buffer.from(str).toString("hex").padStart(64, "0"), "hex")
 }
 
+// Encode a string as binary right-padded to 32 bytes, represented as a hex
+// string (64 chars long)
+export function encodeStringRight(str: string): Buffer {
+    return Buffer.from(Buffer.from(str).toString("hex").padEnd(64, "0"), "hex")
+}
+
 // Turn hex string with potentially missing 0x prefix into Buffer
 function hex(x: string): Buffer {
     return Buffer.from(ethers.utils.hexlify(x, { allowMissingPrefix: true }).substring(2), "hex")