Skip to content
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
1 change: 0 additions & 1 deletion integration_tests/Rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,6 @@ describe("rpc", () => {
});

test("getAssetScheme", async () => {
const shardId = 0;
expect(
await sdk.rpc.chain.getAssetSchemeByHash(
mintTransaction.hash(),
Expand Down
19 changes: 15 additions & 4 deletions src/core/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import { AssetTransferTransaction } from "./transaction/AssetTransferTransaction
import { NetworkId } from "./types";
import { U256 } from "./U256";

export interface AssetJSON {
assetType: string;
lockScriptHash: string;
parameters: number[][];
amount: string;
// The `hash` and the `index` are not included in an RPC response. See
// getAsset() in chain.ts for more details.
transactionHash: string;
transactionOutputIndex: number;
}

export interface AssetData {
assetType: H256;
lockScriptHash: H160;
Expand All @@ -21,7 +32,7 @@ export interface AssetData {
* Object created as an AssetMintTransaction or AssetTransferTransaction.
*/
export class Asset {
public static fromJSON(data: any) {
public static fromJSON(data: AssetJSON) {
const {
assetType,
lockScriptHash,
Expand Down Expand Up @@ -71,7 +82,7 @@ export class Asset {
});
}

public toJSON() {
public toJSON(): AssetJSON {
const {
assetType,
lockScriptHash,
Expand All @@ -83,8 +94,8 @@ export class Asset {
return {
assetType: assetType.value,
lockScriptHash: lockScriptHash.value,
parameters,
amount: amount.toEncodeObject(),
parameters: parameters.map(p => [...p]),
amount: `0x${amount.toString(16)}`,
transactionHash: transactionHash.value,
transactionOutputIndex: index
};
Expand Down
18 changes: 14 additions & 4 deletions src/core/AssetScheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@ import { AssetMintTransaction } from "./transaction/AssetMintTransaction";
import { NetworkId } from "./types";
import { U256 } from "./U256";

export interface AssetSchemeJSON {
metadata: string;
amount: string;
registrar: string | null;
pool: {
assetType: string;
amount: string;
}[];
}

/**
* Object that contains information about the Asset when performing AssetMintTransaction.
*/
export class AssetScheme {
public static fromJSON(data: any) {
public static fromJSON(data: AssetSchemeJSON) {
const { metadata, amount, registrar, pool } = data;
return new AssetScheme({
metadata,
Expand Down Expand Up @@ -50,15 +60,15 @@ export class AssetScheme {
this.pool = data.pool;
}

public toJSON() {
public toJSON(): AssetSchemeJSON {
const { metadata, amount, registrar, pool } = this;
return {
metadata,
amount: amount.toEncodeObject(),
amount: `0x${amount.toString(16)}`,
registrar: registrar === null ? null : registrar.toString(),
pool: pool.map(a => ({
assetType: a.assetType.value,
amount: a.amount.toEncodeObject()
amount: `0x${a.amount.toString(16)}`
}))
};
}
Expand Down
30 changes: 22 additions & 8 deletions src/core/transaction/AssetComposeTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,26 @@ import { Asset } from "../Asset";
import { AssetScheme } from "../AssetScheme";
import { NetworkId } from "../types";
import { U256 } from "../U256";
import { AssetMintOutput } from "./AssetMintOutput";
import { AssetTransferInput } from "./AssetTransferInput";
import { AssetMintOutput, AssetMintOutputJSON } from "./AssetMintOutput";
import {
AssetTransferInput,
AssetTransferInputJSON
} from "./AssetTransferInput";

const RLP = require("rlp");

export interface AssetComposeTransactionJSON {
type: "assetCompose";
data: {
networkId: NetworkId;
shardId: number;
metadata: string;
inputs: AssetTransferInputJSON[];
output: AssetMintOutputJSON;
registrar: string | null;
};
}

/**
* Compose assets.
*/
Expand All @@ -31,7 +46,7 @@ export class AssetComposeTransaction {
* @param obj An AssetComposeTransaction JSON object.
* @returns An AssetComposeTransaction.
*/
public static fromJSON(obj: any) {
public static fromJSON(obj: AssetComposeTransactionJSON) {
const {
data: { networkId, shardId, metadata, inputs, output, registrar }
} = obj;
Expand All @@ -41,9 +56,7 @@ export class AssetComposeTransaction {
metadata,
registrar:
registrar === null ? null : PlatformAddress.ensure(registrar),
inputs: inputs.map((input: any) =>
AssetTransferInput.fromJSON(input)
),
inputs: inputs.map(input => AssetTransferInput.fromJSON(input)),
output: AssetMintOutput.fromJSON(output)
});
}
Expand Down Expand Up @@ -93,14 +106,15 @@ export class AssetComposeTransaction {
* Convert to an AssetComposeTransaction JSON object.
* @returns An AssetComposeTransaction JSON object.
*/
public toJSON() {
public toJSON(): AssetComposeTransactionJSON {
return {
type: this.type,
data: {
networkId: this.networkId,
shardId: this.shardId,
metadata: this.metadata,
registrar: this.registrar,
registrar:
this.registrar === null ? null : this.registrar.toString(),
output: this.output.toJSON(),
inputs: this.inputs.map(input => input.toJSON())
}
Expand Down
25 changes: 20 additions & 5 deletions src/core/transaction/AssetDecomposeTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,26 @@ import * as _ from "lodash";
import { Asset } from "../Asset";
import { AssetTransferOutputValue, NetworkId } from "../types";
import { U256 } from "../U256";
import { AssetTransferInput } from "./AssetTransferInput";
import { AssetTransferOutput } from "./AssetTransferOutput";
import {
AssetTransferInput,
AssetTransferInputJSON
} from "./AssetTransferInput";
import {
AssetTransferOutput,
AssetTransferOutputJSON
} from "./AssetTransferOutput";

const RLP = require("rlp");

export interface AssetDecomposeTransactionJSON {
type: "assetDecompose";
data: {
input: AssetTransferInputJSON;
outputs: AssetTransferOutputJSON[];
networkId: NetworkId;
};
}

/**
* Decompose assets. The sum of inputs must be whole supply of the asset.
*/
Expand All @@ -24,13 +39,13 @@ export class AssetDecomposeTransaction {
* @param obj An AssetDecomposeTransaction JSON object.
* @returns An AssetDecomposeTransaction.
*/
public static fromJSON(obj: any) {
public static fromJSON(obj: AssetDecomposeTransactionJSON) {
const {
data: { input, outputs, networkId }
} = obj;
return new this({
input: AssetTransferInput.fromJSON(input),
outputs: outputs.map((o: any) => AssetTransferInput.fromJSON(o)),
outputs: outputs.map(o => AssetTransferOutput.fromJSON(o)),
networkId
});
}
Expand Down Expand Up @@ -59,7 +74,7 @@ export class AssetDecomposeTransaction {
* Convert to an AssetDecomposeTransaction JSON object.
* @returns An AssetDecomposeTransaction JSON object.
*/
public toJSON() {
public toJSON(): AssetDecomposeTransactionJSON {
const { type, input, outputs, networkId } = this;
return {
type,
Expand Down
18 changes: 11 additions & 7 deletions src/core/transaction/AssetMintOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import { P2PKH } from "../../key/P2PKH";
import { P2PKHBurn } from "../../key/P2PKHBurn";
import { U256 } from "../U256";

export interface AssetMintOutputJSON {
lockScriptHash: string;
parameters: number[][];
amount?: string | null;
}

export class AssetMintOutput {
/**
* Create an AssetMintOutput from an AssetMintOutput JSON object.
* @param data An AssetMintOutput JSON object.
* @returns An AssetMintOutput.
*/
public static fromJSON(data: {
lockScriptHash: string;
parameters: Buffer[];
amount?: string | null;
}) {
public static fromJSON(data: AssetMintOutputJSON) {
const { lockScriptHash, parameters, amount } = data;
return new this({
lockScriptHash: H160.ensure(lockScriptHash),
Expand Down Expand Up @@ -83,12 +85,14 @@ export class AssetMintOutput {
* Convert to an AssetMintOutput JSON object.
* @returns An AssetMintOutput JSON object.
*/
public toJSON() {
public toJSON(): AssetMintOutputJSON {
return {
lockScriptHash: this.lockScriptHash.value,
parameters: this.parameters.map(p => [...p]),
amount:
this.amount == null ? undefined : this.amount.toEncodeObject()
this.amount == null
? undefined
: `0x${this.amount.toString(16)}`
};
}
}
19 changes: 15 additions & 4 deletions src/core/transaction/AssetMintTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@ import { Asset } from "../Asset";
import { AssetScheme } from "../AssetScheme";
import { H256 } from "../H256";
import { NetworkId } from "../types";
import { AssetMintOutput } from "./AssetMintOutput";
import { AssetMintOutput, AssetMintOutputJSON } from "./AssetMintOutput";

const RLP = require("rlp");

export interface AssetMintTransactionJSON {
type: "assetMint";
data: {
networkId: NetworkId;
shardId: number;
metadata: string;
output: AssetMintOutputJSON;
registrar: string | null;
};
}

/**
* Creates a new asset type and that asset itself.
*
Expand All @@ -26,7 +37,7 @@ export class AssetMintTransaction {
* @param data An AssetMintTransaction JSON object.
* @returns An AssetMintTransaction.
*/
public static fromJSON(data: any) {
public static fromJSON(data: AssetMintTransactionJSON) {
const {
data: { networkId, shardId, metadata, output, registrar }
} = data;
Expand Down Expand Up @@ -68,15 +79,15 @@ export class AssetMintTransaction {
this.networkId = networkId;
this.shardId = shardId;
this.metadata = metadata;
this.output = new AssetMintOutput(output);
this.output = output;
this.registrar = registrar;
}

/**
* Convert to an AssetMintTransaction JSON object.
* @returns An AssetMintTransaction JSON object.
*/
public toJSON() {
public toJSON(): AssetMintTransactionJSON {
const { networkId, shardId, metadata, output, registrar } = this;
return {
type: this.type,
Expand Down
13 changes: 10 additions & 3 deletions src/core/transaction/AssetOutPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { H160 } from "../H160";
import { H256 } from "../H256";
import { U256 } from "../U256";

export interface AssetOutPointJSON {
transactionHash: string;
index: number;
assetType: string;
amount: string;
}

export interface AssetOutPointData {
transactionHash: H256;
index: number;
Expand All @@ -24,7 +31,7 @@ export class AssetOutPoint {
* @param data An AssetOutPoint JSON object.
* @returns An AssetOutPoint.
*/
public static fromJSON(data: any) {
public static fromJSON(data: AssetOutPointJSON) {
const { transactionHash, index, assetType, amount } = data;
return new this({
transactionHash: new H256(transactionHash),
Expand Down Expand Up @@ -82,13 +89,13 @@ export class AssetOutPoint {
* Convert to an AssetOutPoint JSON object.
* @returns An AssetOutPoint JSON object.
*/
public toJSON() {
public toJSON(): AssetOutPointJSON {
const { transactionHash, index, assetType, amount } = this;
return {
transactionHash: transactionHash.value,
index,
assetType: assetType.value,
amount: amount.toEncodeObject()
amount: `0x${amount.toString(16)}`
};
}
}
17 changes: 12 additions & 5 deletions src/core/transaction/AssetTransferInput.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Buffer } from "buffer";

import { AssetOutPoint } from "./AssetOutPoint";
import { AssetOutPoint, AssetOutPointJSON } from "./AssetOutPoint";

export type TimelockType = "block" | "blockAge" | "time" | "timeAge";
export interface Timelock {
Expand All @@ -9,6 +9,13 @@ export interface Timelock {
value: number;
}

export interface AssetTransferInputJSON {
prevOut: AssetOutPointJSON;
timelock: Timelock | null;
lockScript: number[];
unlockScript: number[];
}

/**
* An AssetTransferInput consists of the following:
* - An AssetOutPoint, which points to the asset to be spent.
Expand All @@ -22,13 +29,13 @@ export class AssetTransferInput {
* @param data An AssetTransferInput JSON object.
* @returns An AssetTransferInput.
*/
public static fromJSON(data: any) {
public static fromJSON(data: AssetTransferInputJSON) {
const { prevOut, timelock, lockScript, unlockScript } = data;
return new this({
prevOut: AssetOutPoint.fromJSON(prevOut),
timelock,
lockScript,
unlockScript
lockScript: Buffer.from(lockScript),
unlockScript: Buffer.from(unlockScript)
});
}
public readonly prevOut: AssetOutPoint;
Expand Down Expand Up @@ -76,7 +83,7 @@ export class AssetTransferInput {
* Convert to an AssetTransferInput JSON object.
* @returns An AssetTransferInput JSON object.
*/
public toJSON() {
public toJSON(): AssetTransferInputJSON {
const { prevOut, timelock, lockScript, unlockScript } = this;
return {
prevOut: prevOut.toJSON(),
Expand Down
Loading