Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Address as an ABIValue and handle encoding an Address #911

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/abi/abi_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// | string
// | (T1, ..., Tn)
*/
import { encodeAddress, decodeAddress } from '../encoding/address.js';
import { encodeAddress, decodeAddress, Address } from '../encoding/address.js';
import { bigIntToBytes, bytesToBigInt } from '../encoding/bigint.js';
import { concatArrays } from '../utils/utils.js';

Expand All @@ -37,7 +37,8 @@ export type ABIValue =
| bigint
| string
| Uint8Array
| ABIValue[];
| ABIValue[]
| Address;

export abstract class ABIType {
// Converts a ABIType object to a string
Expand Down Expand Up @@ -255,18 +256,24 @@ export class ABIAddressType extends ABIType {
}

encode(value: ABIValue) {
if (typeof value !== 'string' && !(value instanceof Uint8Array)) {
throw new Error(`Cannot encode value as ${this.toString()}: ${value}`);
}
if (typeof value === 'string') {
const decodedAddress = decodeAddress(value);
return decodedAddress.publicKey;
}
// Return the address if it is already in bytes
if (value.byteLength !== 32) {
throw new Error(`byte string must be 32 bytes long for an address`);

if (value instanceof Address) {
return value.publicKey;
}
return value;

if (value instanceof Uint8Array) {
if (value.byteLength !== 32) {
throw new Error(`byte string must be 32 bytes long for an address`);
}

return value;
}

throw new Error(`Cannot encode value as ${this.toString()}: ${value}`);
}

decode(byteString: Uint8Array): string {
Expand Down
16 changes: 16 additions & 0 deletions tests/10.ABI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,22 @@ describe('ABI type checking', () => {
});

describe('ABI encoding', () => {
// Note we are not using the newTestCase array below because we are not testing round-trip
// encoding. We added support for encoding Address as `address`, but decode should still return
// a string for backwards compatibility
it('ABIAddressType should encode Address properly', () => {
const abiType = new ABIAddressType();
const addr = decodeAddress(
'MO2H6ZU47Q36GJ6GVHUKGEBEQINN7ZWVACMWZQGIYUOE3RBSRVYHV4ACJI'
);

assert.deepStrictEqual(abiType.encode(addr), addr.publicKey);
assert.deepStrictEqual(
abiType.decode(abiType.encode(addr)),
addr.toString()
);
});

type TestCase<T> = {
abiType: ABIType;
input: T;
Expand Down
Loading