Skip to content

Commit

Permalink
fix(address-hooks): use harden (or freeze)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Dec 11, 2024
1 parent 88eb9bc commit 8846a0d
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions packages/cosmic-proto/src/address-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
import { bech32 } from 'bech32';
import queryString from 'query-string';

/* global globalThis */
/** @type {<T>(x: T) => T} */
const harden = globalThis.harden || Object.freeze;

// ADDRESS_HOOK_VERSION is the version of the address hook format used in this
// module.
const ADDRESS_HOOK_VERSION = 0;
Expand All @@ -41,11 +45,12 @@ if ((ADDRESS_HOOK_VERSION & 0x0f) !== ADDRESS_HOOK_VERSION) {

// AddressHookMagic is a magic byte prefix that identifies a hooked address.
// Chosen to make bech32 address hooks that look like "agoric10rch..."
const ADDRESS_HOOK_MAGIC = new Uint8Array([
const ADDRESS_HOOK_MAGIC = [
0x78,
0xf1,
0x70, // | ADDRESS_HOOK_VERSION
]);
];
harden(ADDRESS_HOOK_MAGIC);

/**
* The default maximum number of characters in a bech32-encoded hooked address.
Expand All @@ -63,6 +68,9 @@ export const DEFAULT_HOOKED_ADDRESS_CHAR_LIMIT = 1024;
* { key: ['value1', null, 'value3'] } // '?key=value1&key&key=value3'
*/

/**
* How many bytes are used to store the length of the base address.
*/
export const BASE_ADDRESS_LENGTH_BYTES = 2;

/**
Expand All @@ -78,8 +86,9 @@ export const decodeBech32 = (
const rawBytes = bech32.fromWords(words);

const bytes = new Uint8Array(rawBytes);
return { prefix, bytes };
return harden({ prefix, bytes });
};
harden(decodeBech32);

/**
* @param {string} humanReadablePart
Expand All @@ -95,6 +104,7 @@ export const encodeBech32 = (
const words = bech32.toWords(bytes);
return bech32.encode(humanReadablePart, words, charLimit);
};
harden(encodeBech32);

/**
* Join raw base address bytes and hook data into a bech32-encoded hooked
Expand Down Expand Up @@ -162,6 +172,7 @@ export const joinHookedAddress = (

return encodeBech32(prefix, hookBuf, charLimit);
};
harden(joinHookedAddress);

/**
* @param {string} baseAddress
Expand All @@ -175,6 +186,7 @@ export const encodeAddressHook = (baseAddress, query, charLimit) => {
const hookData = te.encode(`?${queryStr}`);
return joinHookedAddress(baseAddress, hookData, charLimit);
};
harden(encodeAddressHook);

/**
* @param {string} addressHook
Expand All @@ -190,8 +202,9 @@ export const decodeAddressHook = (addressHook, charLimit) => {

/** @type {HookQuery} */
const query = queryString.parse(hookStr);
return { baseAddress, query };
return harden({ baseAddress, query });
};
harden(decodeAddressHook);

/**
* @param {string} specimen
Expand All @@ -214,7 +227,7 @@ export const splitHookedAddressUnsafe = (
maybeMagicByte &= 0xf0;
}
if (maybeMagicByte !== ADDRESS_HOOK_MAGIC[i]) {
return { baseAddress: specimen, hookData: new Uint8Array() };
return harden({ baseAddress: specimen, hookData: new Uint8Array() });
}
}

Expand Down Expand Up @@ -242,8 +255,9 @@ export const splitHookedAddressUnsafe = (

const hookData = bytes.subarray(magicLength + b, -BASE_ADDRESS_LENGTH_BYTES);

return { baseAddress, hookData };
return harden({ baseAddress, hookData });
};
harden(splitHookedAddressUnsafe);

/**
* @param {string} specimen
Expand All @@ -260,3 +274,4 @@ export const splitHookedAddress = (specimen, charLimit) => {
}
throw Error(result);
};
harden(splitHookedAddress);

0 comments on commit 8846a0d

Please sign in to comment.