Skip to content

Commit

Permalink
make codec required, some pvm types (#89)
Browse files Browse the repository at this point in the history
* wip

* xchain

* rename to avm

* nits

* wip

* wip

* add test

* refactor type
  • Loading branch information
hiimoliverwang committed Jun 30, 2022
1 parent 8710224 commit 3d1b6a0
Show file tree
Hide file tree
Showing 28 changed files with 102 additions and 130 deletions.
3 changes: 1 addition & 2 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import type { Codec } from '../codec';
export interface Serializable {
_type: symbol;

toBytes(codec?: Codec): Uint8Array;
toBytes(codec: Codec): Uint8Array;
}

export interface SerializableStatic {
new (...args: any[]): Serializable;

fromBytes(bytes: Uint8Array, codec?: Codec): [Serializable, Uint8Array];
fromBytes(bytes: Uint8Array, codec: Codec): [Serializable, Uint8Array];
}

Expand Down
6 changes: 3 additions & 3 deletions src/components/avax/baseTx.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { concatBytes } from '../../utils/buffer';
import type { Codec } from '../../codec/codec';
import { serializable } from '../../common/types';
import { TransferableInput, TransferableOutput } from '../../components/avax';
import { Id } from '../../fxs/common/id';
import { Bytes, Int } from '../../primitives';
import { concatBytes } from '../../utils/buffer';
import { convertListStruct, packList } from '../../utils/serializeList';
import { packSimple, unpack } from '../../utils/struct';
import { pack, unpack } from '../../utils/struct';

const _symbol = Symbol('avax.BaseTx');

Expand Down Expand Up @@ -45,7 +45,7 @@ export class BaseTx {

toBytes(codec) {
return concatBytes(
packSimple(this.NetworkId, this.BlockchainId),
pack([this.NetworkId, this.BlockchainId], codec),
packList(this.outputs, codec),
packList(this.inputs, codec),
this.memo.toBytes(),
Expand Down
4 changes: 2 additions & 2 deletions src/components/avax/transferableInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Serializable } from '../../common/types';
import { serializable } from '../../common/types';
import { Id } from '../../fxs/common/id';
import { concatBytes } from '../../utils/buffer';
import { packSimple, unpack } from '../../utils/struct';
import { pack, unpack } from '../../utils/struct';

const _symbol = Symbol('avax.TransferableInput');

Expand Down Expand Up @@ -38,7 +38,7 @@ export class TransferableInput {

toBytes(codec: Codec) {
return concatBytes(
packSimple(this.utxoID, this.assetId),
pack([this.utxoID, this.assetId], codec),
codec.PackPrefix(this.input),
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/components/avax/transferableOp.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { concatBytes } from '../../utils/buffer';
import { UTXOID } from '.';
import { Codec } from '../../codec/codec';
import type { Serializable } from '../../common/types';
import { serializable } from '../../common/types';
import { Id } from '../../fxs/common/id';
import { concatBytes } from '../../utils/buffer';
import { convertListStruct, packList } from '../../utils/serializeList';
import { packSimpleWithCodec, unpack } from '../../utils/struct';
import { pack, unpack } from '../../utils/struct';

const _symbol = Symbol('avax.TransferableOp');

Expand Down Expand Up @@ -37,8 +37,8 @@ export class TransferableOp {

toBytes(codec: Codec) {
return concatBytes(
packSimpleWithCodec([this.assetId], codec),
packList(this.UTXOId),
pack([this.assetId], codec),
packList(this.UTXOId, codec),
codec.PackPrefix(this.transferOp),
);
}
Expand Down
7 changes: 5 additions & 2 deletions src/components/avax/transferableOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Amounter } from '../../common/types';
import { serializable } from '../../common/types';
import { Id } from '../../fxs/common/id';
import { concatBytes } from '../../utils/buffer';
import { packSimple, unpack } from '../../utils/struct';
import { pack, unpack } from '../../utils/struct';

const _symbol = Symbol('avax.TransferableOutput');

Expand All @@ -29,6 +29,9 @@ export class TransferableOutput {
}

toBytes(codec: Codec) {
return concatBytes(packSimple(this.assetId), codec.PackPrefix(this.output));
return concatBytes(
pack([this.assetId], codec),
codec.PackPrefix(this.output),
);
}
}
4 changes: 2 additions & 2 deletions src/components/avax/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Codec } from '../../codec/codec';
import { Serializable, serializable } from '../../common/types';
import { UTXOID } from '../../components/avax';
import { Id } from '../../fxs/common';
import { packSimpleWithCodec, unpack } from '../../utils/struct';
import { pack, unpack } from '../../utils/struct';

const _symbol = Symbol('avax.Utxo');

Expand Down Expand Up @@ -31,7 +31,7 @@ export class Utxo {

toBytes(codec) {
return concatBytes(
packSimpleWithCodec([this.utxoId, this.assetId], codec),
pack([this.utxoId, this.assetId], codec),
codec.PackPrefix(this.output),
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/avax/utxoId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Id } from '../../fxs/common/id';
import { BigIntPr, Int } from '../../primitives';
import { base58check } from '../../utils/base58';
import { concatBytes } from '../../utils/buffer';
import { packSimple, unpack } from '../../utils/struct';
import { pack, unpack } from '../../utils/struct';

const _symbol = Symbol('avax.UTXOID');

Expand All @@ -23,8 +23,8 @@ export class UTXOID {
return [new UTXOID(txID, outputIdx), remaining];
}

toBytes() {
return packSimple(this.txID, this.outputIdx);
toBytes(codec) {
return pack([this.txID, this.outputIdx], codec);
}

ID() {
Expand Down
12 changes: 5 additions & 7 deletions src/fxs/nft/mintOperation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { serializable } from '../../common/types';
import { Bytes, Int } from '../../primitives';
import { pack, unpack } from '../../utils/struct';
import { Input, OutputOwnersList } from '../secp256k1';
import { packSimple, unpack } from '../../utils/struct';

const _symbol = Symbol('nftfx.MintOperation');

Expand Down Expand Up @@ -30,12 +30,10 @@ export class MintOperation {
];
}

toBytes() {
return packSimple(
this.input,
this.groupId,
this.payload,
this.outputOwnerList,
toBytes(codec) {
return pack(
[this.input, this.groupId, this.payload, this.outputOwnerList],
codec,
);
}
}
17 changes: 9 additions & 8 deletions src/fxs/nft/mintOutput.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { serializable } from '../../common/types';
import { Int } from '../../primitives';
import { packSimple, unpack } from '../../utils/struct';
import { pack, unpack } from '../../utils/struct';
import { OutputOwners } from '../secp256k1';

const _symbol = Symbol('nftfx.MintOutput');
Expand All @@ -15,16 +15,17 @@ export class MintOutput {

constructor(private groupId: Int, private outputOwners: OutputOwners) {}

static fromBytes(bytes: Uint8Array): [MintOutput, Uint8Array] {
const [groupId, owners, remaining] = unpack(bytes, [
Int,
OutputOwners,
] as const);
static fromBytes(bytes: Uint8Array, codec): [MintOutput, Uint8Array] {
const [groupId, owners, remaining] = unpack(
bytes,
[Int, OutputOwners] as const,
codec,
);

return [new MintOutput(groupId, owners), remaining];
}

toBytes() {
return packSimple(this.groupId, this.outputOwners);
toBytes(codec) {
return pack([this.groupId, this.outputOwners], codec);
}
}
6 changes: 3 additions & 3 deletions src/fxs/nft/transferOperation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { serializable } from '../../common/types';
import { packSimple, unpack } from '../../utils/struct';
import { pack, unpack } from '../../utils/struct';
import { Input } from '../secp256k1';
import { TransferOutput } from './transferOutput';

Expand All @@ -21,7 +21,7 @@ export class TransferOperation {
return [new TransferOperation(input, output), remaining];
}

toBytes() {
return packSimple(this.input, this.output);
toBytes(codec) {
return pack([this.input, this.output], codec);
}
}
6 changes: 3 additions & 3 deletions src/fxs/nft/transferOutput.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { serializable } from '../../common/types';
import { Bytes, Int } from '../../primitives';
import { packSimple, unpack } from '../../utils/struct';
import { pack, unpack } from '../../utils/struct';
import { OutputOwners } from '../secp256k1';

const _symbol = Symbol('nftfx.TransferOutput');
Expand Down Expand Up @@ -29,7 +29,7 @@ export class TransferOutput {
return [new TransferOutput(groupId, payload, outputOwners), remaining];
}

toBytes() {
return packSimple(this.groupId, this.payload, this.outputOwners);
toBytes(codec) {
return pack([this.groupId, this.payload, this.outputOwners], codec);
}
}
9 changes: 5 additions & 4 deletions src/fxs/secp256k1/credential.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Codec } from '../../codec';
import { serializable } from '../../common/types';
import { packList, unpackList } from '../../utils/serializeList';
import { Signature } from './signature';
Expand All @@ -13,12 +14,12 @@ export class Credential {

constructor(private signatures: Signature[]) {}

static fromBytes(bytes: Uint8Array): [Credential, Uint8Array] {
const [sigs, remaining] = unpackList(bytes, Signature);
static fromBytes(bytes: Uint8Array, codec: Codec): [Credential, Uint8Array] {
const [sigs, remaining] = unpackList(bytes, Signature, codec);
return [new Credential(sigs), remaining];
}

toBytes() {
return packList(this.signatures);
toBytes(codec) {
return packList(this.signatures, codec);
}
}
4 changes: 2 additions & 2 deletions src/fxs/secp256k1/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Input {
return [new Input(sigIndices), remaining];
}

toBytes() {
return packList(this.sigIndices);
toBytes(codec) {
return packList(this.sigIndices, codec);
}
}
8 changes: 4 additions & 4 deletions src/fxs/secp256k1/mintOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ export class MintOperation {
return [new MintOperation(input, mintOutput, transferOutput), remaining];
}

toBytes() {
toBytes(codec) {
return concatBytes(
this.input.toBytes(),
this.mintOutput.toBytes(),
this.transferOutput.toBytes(),
this.input.toBytes(codec),
this.mintOutput.toBytes(codec),
this.transferOutput.toBytes(codec),
);
}
}
8 changes: 4 additions & 4 deletions src/fxs/secp256k1/mintOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ export class MintOutput {

constructor(private outputOwners: OutputOwners) {}

static fromBytes(bytes: Uint8Array): [MintOutput, Uint8Array] {
static fromBytes(bytes: Uint8Array, codec): [MintOutput, Uint8Array] {
let owners: OutputOwners;
[owners, bytes] = OutputOwners.fromBytes(bytes);
[owners, bytes] = OutputOwners.fromBytes(bytes, codec);

return [new MintOutput(owners), bytes];
}

toBytes() {
return this.outputOwners.toBytes();
toBytes(codec) {
return this.outputOwners.toBytes(codec);
}
}
20 changes: 10 additions & 10 deletions src/fxs/secp256k1/outputOwners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { concatBytes } from '@noble/hashes/utils';
import { serializable } from '../../common/types';
import { BigIntPr, Int } from '../../primitives';
import { convertListStruct, packList } from '../../utils/serializeList';
import { packSimple, unpack } from '../../utils/struct';
import { pack, unpack } from '../../utils/struct';
import { Address } from '../common';

const _symbol = Symbol('secp256k1fx.OutputOwners');
Expand All @@ -21,20 +21,20 @@ export class OutputOwners {
public readonly addrs: Address[],
) {}

static fromBytes(bytes: Uint8Array): [OutputOwners, Uint8Array] {
const [locktime, threshold, addresses, remaining] = unpack(bytes, [
BigIntPr,
Int,
convertListStruct(Address),
]);
static fromBytes(bytes: Uint8Array, codec): [OutputOwners, Uint8Array] {
const [locktime, threshold, addresses, remaining] = unpack(
bytes,
[BigIntPr, Int, convertListStruct(Address)],
codec,
);

return [new OutputOwners(locktime, threshold, addresses), remaining];
}

toBytes() {
toBytes(codec) {
return concatBytes(
packSimple(this.locktime, this.threshold),
packList(this.addrs),
pack([this.locktime, this.threshold], codec),
packList(this.addrs, codec),
);
}
}
8 changes: 4 additions & 4 deletions src/fxs/secp256k1/outputOwnersList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export class OutputOwnersList {

constructor(private outputOwners: OutputOwners[]) {}

static fromBytes(bytes: Uint8Array): [OutputOwnersList, Uint8Array] {
const [owners, remaining] = unpackList(bytes, OutputOwners);
static fromBytes(bytes: Uint8Array, codec): [OutputOwnersList, Uint8Array] {
const [owners, remaining] = unpackList(bytes, OutputOwners, codec);
return [new OutputOwnersList(owners), remaining];
}

toBytes() {
return packList(this.outputOwners);
toBytes(codec) {
return packList(this.outputOwners, codec);
}
}
6 changes: 3 additions & 3 deletions src/fxs/secp256k1/transferInput.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Input } from '.';
import { serializable } from '../../common/types';
import { BigIntPr } from '../../primitives';
import { packSimple, unpack } from '../../utils/struct';
import { pack, unpack } from '../../utils/struct';

const _symbol = Symbol('secp256k1fx.TransferInput');

Expand All @@ -22,7 +22,7 @@ export class TransferInput {
return [new TransferInput(amt, input), remaining];
}

toBytes() {
return packSimple(this.amt, this.input);
toBytes(codec) {
return pack([this.amt, this.input], codec);
}
}
6 changes: 3 additions & 3 deletions src/fxs/secp256k1/transferOutput.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OutputOwners } from '.';
import { serializable } from '../../common/types';
import { BigIntPr } from '../../primitives';
import { packSimple, unpack } from '../../utils/struct';
import { pack, unpack } from '../../utils/struct';

const _symbol = Symbol('secp256k1fx.TransferOutput');

Expand Down Expand Up @@ -30,7 +30,7 @@ export class TransferOutput {
return [new TransferOutput(amt, owners), remaining];
}

toBytes() {
return packSimple(this.amt, this.outputOwners);
toBytes(codec) {
return pack([this.amt, this.outputOwners], codec);
}
}
Loading

0 comments on commit 3d1b6a0

Please sign in to comment.