Skip to content

Instantly share code, notes, and snippets.

@stephancill
Created June 9, 2024 17:43
Show Gist options
  • Save stephancill/d6b9850e0a7aaf86083358fae988fe86 to your computer and use it in GitHub Desktop.
Save stephancill/d6b9850e0a7aaf86083358fae988fe86 to your computer and use it in GitHub Desktop.
This function reverses the transformation done by `transformHash` in hub-monorepo
import { base58ToBytes } from "@farcaster/hub-web";
/**
* ~The protobuf format specifies encoding bytes as base64 strings, but we want to return hex strings
* to be consistent with the rest of the API, so we need to convert the base64 strings to hex strings
* before returning them.~
*
* This function reverses the transformation done by `transformHash` in `transformHash.ts`.
*/
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
export function transformHashReverse(objRaw: any): any {
const obj = structuredClone(objRaw);
// Map of current key names to old key names that we want to preserve for backwards compatibility reasons
// If you are renaming a protobuf field, add the current name as the key, and the old name as the value, and we
// will copy the contents of the current field to the old field
const BACKWARDS_COMPATIBILITY_MAP: Record<string, string> = {
verificationAddAddressBody: "verificationAddEthAddressBody",
claimSignature: "ethSignature",
};
if (obj === null || typeof obj !== "object") {
return obj;
}
// These are the target keys that are base64 encoded, which should be converted to hex
const toHexKeys = [
"hash",
"signer",
"transactionHash",
"key",
"owner",
"to",
"from",
"recoveryAddress",
];
// Convert these target keys to strings
const toStringKeys = ["name"];
const toHexOrBase58Keys = ["address", "blockHash"];
for (const key in obj) {
// biome-ignore lint/suspicious/noPrototypeBuiltins: <explanation>
if (obj.hasOwnProperty(key)) {
if (toHexKeys.includes(key) && typeof obj[key] === "string") {
// obj[key] = convertB64ToHex(obj[key]);
// Reverse: convert hex to base64
obj[key] = Buffer.from(obj[key].slice(2), "hex").toString("base64");
} else if (toStringKeys.includes(key) && typeof obj[key] === "string") {
// obj[key] = Buffer.from(obj[key], "base64").toString("utf-8");
// Reverse: convert string to base64
obj[key] = Buffer.from(obj[key]).toString("base64");
} else if (
toHexOrBase58Keys.includes(key) &&
typeof obj[key] === "string"
) {
// We need to convert solana related bytes to base58
if (obj["protocol"] === "PROTOCOL_SOLANA") {
// obj[key] = convertB64ToB58(obj[key]);
// Reverse: convert base58 to base64
obj[key] = Buffer.from(
base58ToBytes(obj[key]).unwrapOr(new Uint8Array())
).toString("base64");
} else {
// obj[key] = convertB64ToHex(obj[key]);
// Reverse: convert hex to base64
obj[key] = Buffer.from(obj[key].slice(2), "hex").toString("base64");
}
} else if (typeof obj[key] === "object") {
obj[key] = transformHashReverse(obj[key]);
}
const backwardsCompatibleName = BACKWARDS_COMPATIBILITY_MAP[key];
if (backwardsCompatibleName) {
obj[backwardsCompatibleName] = obj[key];
}
}
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment