diff --git a/yarn-project/aztec.js/src/index.ts b/yarn-project/aztec.js/src/index.ts index 6e202c2dc1ee..291c91e825d7 100644 --- a/yarn-project/aztec.js/src/index.ts +++ b/yarn-project/aztec.js/src/index.ts @@ -52,9 +52,11 @@ export { computeInnerAuthWitHashFromAction, computeInnerAuthWitHash, generatePublicKey, + readFieldCompressedString, waitForAccountSynch, waitForPXE, } from './utils/index.js'; + export { NoteSelector } from '@aztec/foundation/abi'; export { createPXEClient, createCompatibleClient } from './rpc_clients/index.js'; diff --git a/yarn-project/aztec.js/src/utils/field_compressed_string.ts b/yarn-project/aztec.js/src/utils/field_compressed_string.ts new file mode 100644 index 000000000000..30ac8fe02fc9 --- /dev/null +++ b/yarn-project/aztec.js/src/utils/field_compressed_string.ts @@ -0,0 +1,27 @@ +import { Fr } from '@aztec/circuits.js'; + +/** + * The representation of a FieldCompressedString in aztec.nr + */ +interface NoirFieldCompressedString { + /** + * The field value of the string + */ + value: bigint; +} +/** + * This turns + * @param field - The field that contains the string + * @returns - the string that is decoded from the field + */ +export const readFieldCompressedString = (field: NoirFieldCompressedString): string => { + const vals: number[] = Array.from(new Fr(field.value).toBuffer()); + + let str = ''; + for (let i = 0; i < vals.length; i++) { + if (vals[i] != 0) { + str += String.fromCharCode(Number(vals[i])); + } + } + return str; +}; diff --git a/yarn-project/aztec.js/src/utils/index.ts b/yarn-project/aztec.js/src/utils/index.ts index 3fdc939fca1b..6691a934a10d 100644 --- a/yarn-project/aztec.js/src/utils/index.ts +++ b/yarn-project/aztec.js/src/utils/index.ts @@ -6,3 +6,4 @@ export * from './authwit.js'; export * from './pxe.js'; export * from './account.js'; export * from './anvil_test_watcher.js'; +export * from './field_compressed_string.js'; diff --git a/yarn-project/end-to-end/src/e2e_token_contract/reading_constants.test.ts b/yarn-project/end-to-end/src/e2e_token_contract/reading_constants.test.ts index 7fdfd2b2f829..3dade0e79bba 100644 --- a/yarn-project/end-to-end/src/e2e_token_contract/reading_constants.test.ts +++ b/yarn-project/end-to-end/src/e2e_token_contract/reading_constants.test.ts @@ -1,19 +1,7 @@ -import { Fr } from '@aztec/circuits.js'; +import { readFieldCompressedString } from '@aztec/aztec.js'; import { TokenContractTest } from './token_contract_test.js'; -const toString = ({ value }: { value: bigint }) => { - const vals: number[] = Array.from(new Fr(value).toBuffer()); - - let str = ''; - for (let i = 0; i < vals.length; i++) { - if (vals[i] != 0) { - str += String.fromCharCode(Number(vals[i])); - } - } - return str; -}; - describe('e2e_token_contract reading constants', () => { const t = new TokenContractTest('reading_constants'); const { TOKEN_DECIMALS, TOKEN_NAME, TOKEN_SYMBOL } = TokenContractTest; @@ -34,22 +22,22 @@ describe('e2e_token_contract reading constants', () => { }); it('check name private', async () => { - const name = toString(await t.asset.methods.private_get_name().simulate()); + const name = readFieldCompressedString(await t.asset.methods.private_get_name().simulate()); expect(name).toBe(TOKEN_NAME); }); it('check name public', async () => { - const name = toString(await t.asset.methods.public_get_name().simulate()); + const name = readFieldCompressedString(await t.asset.methods.public_get_name().simulate()); expect(name).toBe(TOKEN_NAME); }); it('check symbol private', async () => { - const sym = toString(await t.asset.methods.private_get_symbol().simulate()); + const sym = readFieldCompressedString(await t.asset.methods.private_get_symbol().simulate()); expect(sym).toBe(TOKEN_SYMBOL); }); it('check symbol public', async () => { - const sym = toString(await t.asset.methods.public_get_symbol().simulate()); + const sym = readFieldCompressedString(await t.asset.methods.public_get_symbol().simulate()); expect(sym).toBe(TOKEN_SYMBOL); }); diff --git a/yarn-project/end-to-end/src/spartan/transfer.test.ts b/yarn-project/end-to-end/src/spartan/transfer.test.ts index 4c675c7541db..066dfb355bae 100644 --- a/yarn-project/end-to-end/src/spartan/transfer.test.ts +++ b/yarn-project/end-to-end/src/spartan/transfer.test.ts @@ -2,9 +2,9 @@ import { getSchnorrAccount } from '@aztec/accounts/schnorr'; import { type AccountWalletWithSecretKey, type AztecAddress, - Fr, type PXE, createCompatibleClient, + readFieldCompressedString, } from '@aztec/aztec.js'; import { createDebugLogger } from '@aztec/foundation/log'; import { TokenContract } from '@aztec/noir-contracts.js'; @@ -18,18 +18,6 @@ if (!PXE_URL) { throw new Error('PXE_URL env variable must be set'); } -const toString = ({ value }: { value: bigint }) => { - const vals: number[] = Array.from(new Fr(value).toBuffer()); - - let str = ''; - for (let i = 0; i < vals.length; i++) { - if (vals[i] != 0) { - str += String.fromCharCode(Number(vals[i])); - } - } - return str; -}; - describe('token transfer test', () => { jest.setTimeout(10 * 60 * 2000); // 20 minutes @@ -100,7 +88,7 @@ describe('token transfer test', () => { }); it('can get info', async () => { - const name = toString(await tokenAdminWallet.methods.private_get_name().simulate()); + const name = readFieldCompressedString(await tokenAdminWallet.methods.private_get_name().simulate()); expect(name).toBe(TOKEN_NAME); });