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 12, 2024
1 parent fe738e7 commit a8a6172
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions packages/cosmic-proto/src/address-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
* const decoded = decodeAddressHook(addressHook);
* // {
* // baseAddress: 'agoric1qqp0e5ys',
* // query: [Object: null prototype] { foo: [ 'bar', 'baz' ], key: 'value' }
* // query: { foo: [ 'bar', 'baz' ], key: 'value' }
* // }
*/

/* eslint-disable no-bitwise */
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 @@ -46,6 +50,7 @@ const ADDRESS_HOOK_BYTE_PREFIX = [
0xf1,
0x70, // | ADDRESS_HOOK_VERSION
];
harden(ADDRESS_HOOK_BYTE_PREFIX);

/**
* 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 @@ -188,10 +200,15 @@ export const decodeAddressHook = (addressHook, charLimit) => {
throw Error(`Hook data does not start with '?': ${hookStr}`);
}

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

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

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

Expand Down Expand Up @@ -246,5 +263,6 @@ export const splitHookedAddress = (

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

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

0 comments on commit a8a6172

Please sign in to comment.