Skip to content

Commit

Permalink
fix-hexprefix
Browse files Browse the repository at this point in the history
  • Loading branch information
wsdt committed Aug 16, 2024
1 parent c176cc6 commit 89bef36
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions backend/offchain/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { ethers } from "ethers";

/**
* Parse arguments to object.
*/
export function parseOffchainParameter(
params: OffchainParameter
): OffchainParameterParsed {
Expand All @@ -16,22 +13,15 @@ export function parseOffchainParameter(
};
}

/**
* Parses offchain parameters and returns a structured request object.
*
* This function takes an object containing offchain parameters, parses them,
* and returns an object with properly formatted values. It converts the secret
* key to bytes, ensures the source address is in checksum format, and converts
* nonces from hexadecimal to bigint format.
*
*/
export function parseRequest(params: OffchainParameterParsed) {
const addHexPrefix = (value: string) => value.startsWith('0x') ? value : `0x${value}`;

return {
skey: ethers.getBytes(params.sk),
srcAddr: ethers.getAddress(params.srcAddr),
srcNonce: ethers.getBigInt("0x" + params.srcNonce),
opNonce: ethers.getBigInt(params.ooNonce),
reqBytes: params.payload,
skey: ethers.getBytes(addHexPrefix(params.sk)),
srcAddr: ethers.getAddress(addHexPrefix(params.srcAddr)),
srcNonce: ethers.getBigInt(addHexPrefix(params.srcNonce)),
opNonce: ethers.getBigInt(addHexPrefix(params.ooNonce)),
reqBytes: ethers.getBytes(addHexPrefix(params.payload)),
} as const;
}

Expand Down

0 comments on commit 89bef36

Please sign in to comment.