Skip to content

Commit

Permalink
fix RLP encoding bug; stripping too many zeroes (#54)
Browse files Browse the repository at this point in the history
* fix RLP encoding bug; stripping too many zeroes

* remove commented code
  • Loading branch information
zeroXbrock authored Jul 3, 2024
1 parent 328a9e1 commit 6556ac8
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/chains/suave/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ import {
type TransactionSerializedSuave,
} from './types.js'

const safeHex = (hex_: Hex): Hex => {
// trim leading 00s
const hex = hex_.replace(/^0x(00)+/, '0x') as Hex
if (hex === '0x0') {
const safeHex = (hex: Hex): Hex => {
if (hex === '0x0' || hex === '0x00') {
return '0x'
} else if (hex.length % 2 !== 0) {
return `0x0${hex.slice(2)}`
Expand Down Expand Up @@ -155,6 +153,16 @@ export const serializeConfidentialComputeRequest = (
})
}

/** Strips leading zero bytes ('00') from a hex string.
* @param hex - The hex string to strip.
* @returns The hex string with leading zero bytes removed.
* @example
* stripLeadingZeroBytes('0x0000ff') // '0xff'
*/
const stripLeadingZeroBytes = (hex: Hex): Hex => {
return hex.replace(/^0x(00)+/, '0x') as Hex
}

/* This is the final serialization step; what's sent to the JSON-RPC node. */
const preSerializedTransaction: (Hex | Hex[])[] = [
[
Expand All @@ -173,8 +181,8 @@ export const serializeConfidentialComputeRequest = (

numberToHex(transaction.chainId),
toHex(transaction.v),
transaction.r,
transaction.s,
stripLeadingZeroBytes(transaction.r), // strip leading 0 bytes to prevent "non-canonical integer (leading zero bytes)" error
stripLeadingZeroBytes(transaction.s),
].map(safeHex),
safeHex(transaction.confidentialInputs),
]
Expand Down

0 comments on commit 6556ac8

Please sign in to comment.