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

refactor(tx-builder): extract common pointers code #1992

Merged
merged 1 commit into from
Jun 18, 2024
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
59 changes: 46 additions & 13 deletions src/tx/builder/field-types/pointers.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,74 @@
import { NamePointer as NamePointerString } from '../../../apis/node';
import { toBytes } from '../../../utils/bytes';
import { Encoded } from '../../../utils/encoder';
import { IllegalArgumentError } from '../../../utils/errors';
import {
Encoded, Encoding, decode, encode,
} from '../../../utils/encoder';
import { isAddressValid } from '../../../utils/crypto';
import { IllegalArgumentError, DecodeError, ArgumentError } from '../../../utils/errors';
import address, { AddressEncodings, idTagToEncoding } from './address';

const ID_TAG = Buffer.from([1]);
const DATA_TAG = Buffer.from([2]);
const DATA_LENGTH_MAX = 1024;
const addressAny = address(...idTagToEncoding);

// TODO: remove after fixing node types
type NamePointer = NamePointerString & {
id: Encoded.Generic<AddressEncodings>;
};
type NamePointerRaw = NamePointerString & {
id: Encoded.Generic<AddressEncodings | Encoding.Bytearray>;
};

export default {
export default <AllowRaw extends boolean>(allowRaw: AllowRaw): {
serialize: (pointers: Array<AllowRaw extends true ? NamePointerRaw : NamePointer>) => Buffer[][];
deserialize: (
pointers: Array<[key: Buffer, id: Buffer]>,
) => Array<AllowRaw extends true ? NamePointerRaw : NamePointer>;
} => ({
/**
* Helper function to build pointers for name update TX
* @param pointers - Array of pointers
* `([ { key: 'account_pubkey', id: 'ak_32klj5j23k23j5423l434l2j3423'} ])`
* @returns Serialized pointers array
*/
serialize(pointers: NamePointer[]): Buffer[][] {
serialize(pointers) {
if (pointers.length > 32) {
throw new IllegalArgumentError(`Expected 32 pointers or less, got ${pointers.length} instead`);
}

return pointers.map(
(pointer) => [toBytes(pointer.key), addressAny.serialize(pointer.id)],
);
return pointers.map(({ key, id }) => {
let payload;
if (isAddressValid(id, ...idTagToEncoding)) {
payload = [...allowRaw ? [ID_TAG] : [], addressAny.serialize(id)];
}
if (isAddressValid(id, Encoding.Bytearray)) {
const data = decode(id);
if (data.length > DATA_LENGTH_MAX) {
throw new ArgumentError('Raw pointer', `shorter than ${DATA_LENGTH_MAX + 1} bytes`, `${data.length} bytes`);
}
payload = [DATA_TAG, data];
}
if (payload == null) throw new DecodeError(`Unknown AENS pointer value: ${id}`);
return [toBytes(key), Buffer.concat(payload)];
});
},

/**
* Helper function to read pointers from name update TX
* @param pointers - Array of pointers
* @returns Deserialize pointer array
*/
deserialize(pointers: Array<[key: Buffer, id: Buffer]>): NamePointer[] {
return pointers.map(
([key, id]) => ({ key: key.toString(), id: addressAny.deserialize(id) }),
);
deserialize(pointers) {
return pointers.map(([bKey, bId]) => {
if (!allowRaw) return { key: bKey.toString(), id: addressAny.deserialize(bId) };
const tag = bId.subarray(0, 1);
const payload = bId.subarray(1);
let id;
if (tag.equals(ID_TAG)) id = addressAny.deserialize(payload);
// TS can't figure out the real type depending on allowRaw
if (tag.equals(DATA_TAG)) id = encode(payload, Encoding.Bytearray) as Encoded.AccountAddress;
if (id == null) throw new DecodeError(`Unknown AENS pointer tag: ${tag}`);
return { key: bKey.toString(), id };
});
},
};
});
62 changes: 0 additions & 62 deletions src/tx/builder/field-types/pointers2.ts

This file was deleted.

6 changes: 2 additions & 4 deletions src/tx/builder/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import nameFee from './field-types/name-fee';
import nameId from './field-types/name-id';
import nonce from './field-types/nonce';
import pointers from './field-types/pointers';
import pointers2 from './field-types/pointers2';
import queryFee from './field-types/query-fee';
import raw from './field-types/raw';
import shortUInt from './field-types/short-u-int';
Expand Down Expand Up @@ -70,7 +69,6 @@ interface EntryTreesPoi {

const entryTreesPoi = entry(EntryTag.TreesPoi) as unknown as EntryTreesPoi;

// TODO: inline after dropping Iris compatibility
const clientTtl = withDefault(60 * 60, shortUInt);
// https://github.com/aeternity/protocol/blob/fd17982/AENS.md#update
/**
Expand Down Expand Up @@ -130,7 +128,7 @@ export const txSchema = [{
nonce: nonce('accountId'),
nameId,
nameTtl,
pointers,
pointers: pointers(false),
clientTtl,
fee,
ttl,
Expand All @@ -141,7 +139,7 @@ export const txSchema = [{
nonce: nonce('accountId'),
nameId,
nameTtl,
pointers: pointers2,
pointers: pointers(true),
clientTtl,
fee,
ttl,
Expand Down