Skip to content

Commit

Permalink
chore: expose util func to convert field compressed string back to st…
Browse files Browse the repository at this point in the history
…ring in aztec js (#9239)

Resolves #9233
  • Loading branch information
sklppy88 authored Oct 15, 2024
1 parent c857cd9 commit ce7e687
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 31 deletions.
2 changes: 2 additions & 0 deletions yarn-project/aztec.js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
27 changes: 27 additions & 0 deletions yarn-project/aztec.js/src/utils/field_compressed_string.ts
Original file line number Diff line number Diff line change
@@ -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;
};
1 change: 1 addition & 0 deletions yarn-project/aztec.js/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
});

Expand Down
16 changes: 2 additions & 14 deletions yarn-project/end-to-end/src/spartan/transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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

Expand Down Expand Up @@ -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);
});

Expand Down

0 comments on commit ce7e687

Please sign in to comment.