Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

atomicassets

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

atomicassets - npm Package Compare versions

Comparing version 0.2.1 to 0.3.0

build/Actions/Explorer.d.ts

1

build/API/Rpc/Asset.d.ts

@@ -14,3 +14,4 @@ import { AssetRow } from "./Cache";

mutableData(): Promise<object | string>;
data(): Promise<object>;
toObject(): Promise<object>;
}

33

build/API/Rpc/Asset.js

@@ -6,4 +6,4 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
const big_integer_1 = __importDefault(require("big-integer"));
const Serialization_1 = require("../../Serialization");
const Binary_1 = require("../../Serialization/Binary");
const Preset_1 = __importDefault(require("./Preset"));

@@ -46,3 +46,3 @@ class RpcAsset {

async backedTokens() {
return (await this._data).backed_core_tokens;
return big_integer_1.default((await this._data).backed_core_amount).divide(this.api.precision).toString();
}

@@ -53,12 +53,4 @@ async immutableData() {

const data = await this._data;
let pdata = await preset.immutableData();
if (typeof pdata === "string") {
pdata = {};
}
try {
return Object.assign({}, pdata, Serialization_1.deserialize(data.immutable_serialized_data, await scheme.format()));
}
catch (e) {
return Binary_1.hex_encode(data.idata);
}
const pdata = await preset.immutableData();
return Object.assign({}, pdata, Serialization_1.deserialize(data.immutable_serialized_data, await scheme.format()));
}

@@ -69,13 +61,10 @@ async mutableData() {

const data = await this._data;
let pdata = await preset.mutableData();
if (typeof pdata === "string") {
pdata = {};
}
try {
return Object.assign({}, pdata, Serialization_1.deserialize(data.mutable_serialized_data, await scheme.format()));
}
catch (e) {
return Binary_1.hex_encode(data.mdata);
}
const pdata = await preset.mutableData();
return Object.assign({}, pdata, Serialization_1.deserialize(data.mutable_serialized_data, await scheme.format()));
}
async data() {
const mutableData = await this.mutableData();
const immutableData = await this.immutableData();
return Object.assign({}, mutableData, immutableData);
}
async toObject() {

@@ -82,0 +71,0 @@ return {

@@ -1,7 +0,47 @@

export declare type AssetRow = any;
export declare type PresetRow = any;
export declare type SchemeRow = any;
export declare type CollectionRow = any;
export declare type OfferRow = any;
export declare type ConfigRow = any;
export declare type SchemeFormat = Array<{
name: string;
type: string;
}>;
export declare type AssetRow = {
id: string;
preset_id: string;
ram_payer: string;
backed_core_amount: string;
immutable_serialized_data: Uint8Array;
mutable_serialized_data: Uint8Array;
};
export declare type PresetRow = {
scheme_name: string;
collection_name: string;
transferable: boolean;
burnable: boolean;
max_supply: number;
issued_supply: number;
mutable_serialized_data: Uint8Array;
immutable_serialized_data: Uint8Array;
};
export declare type SchemeRow = {
scheme_name: string;
author: string;
format: SchemeFormat;
};
export declare type CollectionRow = {
collection_name: string;
author: string;
authorized_accounts: string[];
notify_accounts: string[];
serialized_data: Uint8Array;
};
export declare type OfferRow = {
id: string;
offer_sender: string;
offer_recipient: string;
sender_asset_ids: string[];
recipient_asset_ids: string[];
memo: string;
};
export declare type ConfigRow = {
offer_counter: string;
collection_format: SchemeFormat;
};
export default class RpcCache {

@@ -13,8 +53,8 @@ private readonly assets;

private readonly offers;
asset(assetID: string, data?: AssetRow): AssetRow | null;
preset(presetID: number, data?: PresetRow): PresetRow | null;
scheme(scheme: string, data?: SchemeRow): SchemeRow | null;
collection(collection: string, data?: CollectionRow): CollectionRow | null;
offer(offerID: string, data?: OfferRow): OfferRow | null;
asset(assetID: string, data?: any | null | false): AssetRow | undefined;
preset(presetID: string, data?: any | null | false): PresetRow | undefined;
scheme(scheme: string, data?: any | null | false): SchemeRow | undefined;
collection(collection: string, data?: any | null | false): CollectionRow | undefined;
offer(offerID: string, data?: any | null | false): OfferRow | undefined;
private access;
}

@@ -16,3 +16,2 @@ "use strict";

data.immutable_serialized_data = new Uint8Array(data.immutable_serialized_data);
data.backed_core_tokens = data.backed_core_tokens.replace(" WAX", "");
}

@@ -40,4 +39,8 @@ return this.access(assetID, this.assets, data);

access(identifier, cache, data) {
if (data === null) {
delete cache[String(identifier)];
return undefined;
}
if (data) {
cache[String(identifier)] = { expiration: Date.now() + 15 * 60 * 1000, data };
cache[String(identifier)] = { expiration: Date.now() + 15 * 60 * 1000, updated: Date.now(), data };
return data;

@@ -48,2 +51,6 @@ }

}
// if data is false then only return cache if it is not older than 5 seconds
if (data === false && Date.now() - cache[String(identifier)].updated > 5 * 1000) {
return undefined;
}
return cache[String(identifier)].data;

@@ -50,0 +57,0 @@ }

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Schema_1 = require("../../Schema");
const Serialization_1 = require("../../Serialization");

@@ -32,3 +33,3 @@ class RpcCollection {

async data() {
return Serialization_1.deserialize((await this._data).serialized_data, (await this.api.config()).collection_format);
return Serialization_1.deserialize((await this._data).serialized_data, Schema_1.ObjectSchema((await this.api.config()).collection_format));
}

@@ -35,0 +36,0 @@ async toObject() {

@@ -0,3 +1,5 @@

import ActionGenerator from "../../Actions/Generator";
import RpcAsset from "./Asset";
import RpcCache from "./Cache";
import RpcCache, { ConfigRow } from "./Cache";
import RpcCollection from "./Collection";
import RpcOffer from "./Offer";

@@ -15,17 +17,21 @@ import RpcPreset from "./Preset";

readonly cache: RpcCache;
readonly action: ActionGenerator;
readonly endpoint: string;
readonly contract: string;
readonly coreToken: string;
readonly precision: number;
private readonly fetchBuiltin;
private readonly _config;
constructor(endpoint: string, contract: string, args?: ApiArgs);
config(): Promise<any>;
get_asset(owner: string, id: string, cache?: boolean): Promise<RpcAsset>;
get_preset(id: number, cache?: boolean): Promise<RpcPreset>;
get_scheme(name: string, cache?: boolean): Promise<RpcScheme>;
get_offer(id: number, cache?: boolean): Promise<RpcOffer>;
get_account_offers(account: string, cache?: boolean): Promise<RpcOffer[]>;
get_account_assets(account: string, cache?: boolean): Promise<RpcAsset[]>;
get_table_rows({ code, scope, table, table_key, lower_bound, upper_bound, index_position, key_type, }: any): Promise<any>;
fetch_rpc(path: string, body: any): Promise<any>;
constructor(endpoint: string, contract: string, coreToken: string, precision: number, args?: ApiArgs);
config(): Promise<ConfigRow>;
getAsset(owner: string, id: string, cache?: boolean): Promise<RpcAsset>;
getPreset(id: string, cache?: boolean): Promise<RpcPreset>;
getCollection(name: string, cache?: boolean): Promise<RpcCollection>;
getScheme(name: string, cache?: boolean): Promise<RpcScheme>;
getOffer(id: string, cache?: boolean): Promise<RpcOffer>;
getAccountOffers(account: string, cache?: boolean): Promise<RpcOffer[]>;
getAccountAssets(account: string, cache?: boolean): Promise<RpcAsset[]>;
getTableRows({ code, scope, table, table_key, lower_bound, upper_bound, index_position, key_type, }: any): Promise<any>;
fetchRpc(path: string, body: any): Promise<any>;
}
export {};

@@ -6,6 +6,7 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
const Rpc_1 = __importDefault(require("../../Actions/Rpc"));
const RpcError_1 = __importDefault(require("../../Errors/RpcError"));
const Schema_1 = require("../../Schema");
const Asset_1 = __importDefault(require("./Asset"));
const Cache_1 = __importDefault(require("./Cache"));
const Collection_1 = __importDefault(require("./Collection"));
const Offer_1 = __importDefault(require("./Offer"));

@@ -16,5 +17,7 @@ const Preset_1 = __importDefault(require("./Preset"));

class RpcApi {
constructor(endpoint, contract, args = { rateLimit: 4 }) {
constructor(endpoint, contract, coreToken, precision, args = { rateLimit: 4 }) {
this.endpoint = endpoint;
this.contract = contract;
this.coreToken = coreToken;
this.precision = precision;
if (args.fetch) {

@@ -28,5 +31,6 @@ this.fetchBuiltin = args.fetch;

this.cache = new Cache_1.default();
this.action = new Rpc_1.default(this);
this._config = new Promise((async (resolve, reject) => {
try {
const resp = await this.get_table_rows({
const resp = await this.getTableRows({
code: this.contract, scope: this.contract, table: "config",

@@ -37,5 +41,3 @@ });

}
const data = resp.rows[0];
data.collection_format = Schema_1.ObjectSchema(data.collection_format);
return resolve(data);
return resolve(resp.rows[0]);
}

@@ -50,15 +52,33 @@ catch (e) {

}
async get_asset(owner, id, cache = true) {
async getAsset(owner, id, cache = true) {
if (!cache) {
this.cache.asset(id, null);
}
return new Asset_1.default(this, owner, id, undefined, undefined, cache);
}
async get_preset(id, cache = true) {
async getPreset(id, cache = true) {
if (!cache) {
this.cache.preset(id, null);
}
return new Preset_1.default(this, id, undefined, undefined, undefined, cache);
}
async get_scheme(name, cache = true) {
async getCollection(name, cache = true) {
if (!cache) {
this.cache.collection(name, null);
}
return new Collection_1.default(this, name, undefined, cache);
}
async getScheme(name, cache = true) {
if (!cache) {
this.cache.scheme(name, null);
}
return new Scheme_1.default(this, name, undefined, cache);
}
async get_offer(id, cache = true) {
async getOffer(id, cache = true) {
if (!cache) {
this.cache.offer(id, null);
}
return new Offer_1.default(this, id, undefined, undefined, undefined, cache);
}
async get_account_offers(account, cache = true) {
async getAccountOffers(account, cache = true) {
return (await this.queue.account_offers(account)).map((offer) => {

@@ -68,3 +88,3 @@ return new Offer_1.default(this, offer.id, offer, undefined, undefined, cache);

}
async get_account_assets(account, cache = true) {
async getAccountAssets(account, cache = true) {
return (await this.queue.account_assets(account)).map((asset) => {

@@ -74,4 +94,4 @@ return new Asset_1.default(this, account, asset.id, asset, undefined, cache);

}
async get_table_rows({ code, scope, table, table_key = "", lower_bound = "", upper_bound = "", index_position = 1, key_type = "", }) {
return await this.fetch_rpc("/v1/chain/get_table_rows", {
async getTableRows({ code, scope, table, table_key = "", lower_bound = "", upper_bound = "", index_position = 1, key_type = "", }) {
return await this.fetchRpc("/v1/chain/get_table_rows", {
code, scope, table, table_key,

@@ -82,6 +102,5 @@ lower_bound, upper_bound, index_position,

}
async fetch_rpc(path, body) {
async fetchRpc(path, body) {
let response;
let json;
// console.log(path, body);
try {

@@ -88,0 +107,0 @@ response = await this.fetchBuiltin(this.endpoint + path, {

@@ -6,7 +6,7 @@ import RpcAsset from "./Asset";

private readonly api;
readonly id: number;
readonly id: string;
private readonly _data;
private readonly _senderAssets;
private readonly _recipientAssets;
constructor(api: RpcApi, id: number, data?: OfferRow, senderAssets?: RpcAsset[], receiverAssets?: RpcAsset[], cache?: boolean);
constructor(api: RpcApi, id: string, data?: OfferRow, senderAssets?: RpcAsset[], receiverAssets?: RpcAsset[], cache?: boolean);
sender(): Promise<string>;

@@ -13,0 +13,0 @@ recipient(): Promise<string>;

@@ -31,4 +31,4 @@ "use strict";

const row = await this._data;
const inventory = await this.api.queue.account_assets(await row.offer_sender, cache);
let offerAssets = inventory.filter((element) => {
const inventory = await this.api.queue.account_assets(row.offer_sender, cache);
const offerAssets = inventory.filter((element) => {
return row.sender_asset_ids.indexOf(element.id) >= 0;

@@ -39,4 +39,3 @@ });

}
offerAssets = offerAssets.map((asset) => new Asset_1.default(this.api, asset.owner, asset.id, asset));
return resolve(offerAssets);
return resolve(offerAssets.map((asset) => new Asset_1.default(this.api, row.offer_sender, asset.id, asset)));
}

@@ -55,4 +54,4 @@ catch (e) {

const row = await this._data;
const inventory = await this.api.queue.account_assets(await row.offer_recipient, cache);
let offerAssets = inventory.filter((element) => {
const inventory = await this.api.queue.account_assets(row.offer_recipient, cache);
const offerAssets = inventory.filter((element) => {
return row.recipient_asset_ids.indexOf(element.id) >= 0;

@@ -63,4 +62,3 @@ });

}
offerAssets = offerAssets.map((asset) => new Asset_1.default(this.api, asset.owner, asset.id, asset));
return resolve(offerAssets);
return resolve(offerAssets.map((asset) => new Asset_1.default(this.api, row.offer_recipient, asset.id, asset)));
}

@@ -67,0 +65,0 @@ catch (e) {

@@ -7,11 +7,11 @@ import { PresetRow } from "./Cache";

private readonly api;
readonly id: number;
readonly id: string;
private readonly _data;
private readonly _collection;
private readonly _scheme;
constructor(api: RpcApi, id: number, data?: PresetRow, scheme?: RpcScheme, collection?: RpcCollection, cache?: boolean);
constructor(api: RpcApi, id: string, data?: PresetRow, scheme?: RpcScheme, collection?: RpcCollection, cache?: boolean);
collection(): Promise<RpcCollection>;
scheme(): Promise<RpcScheme>;
immutableData(): Promise<object | string>;
mutableData(): Promise<object | string>;
immutableData(): Promise<object>;
mutableData(): Promise<object>;
isTransferable(): Promise<boolean>;

@@ -18,0 +18,0 @@ isBurnable(): Promise<boolean>;

@@ -7,3 +7,2 @@ "use strict";

const Serialization_1 = require("../../Serialization");
const Binary_1 = require("../../Serialization/Binary");
const Collection_1 = __importDefault(require("./Collection"));

@@ -65,17 +64,7 @@ const Scheme_1 = __importDefault(require("./Scheme"));

const scheme = await this._scheme;
try {
return Serialization_1.deserialize((await this._data).immutable_serialized_data, await scheme.format());
}
catch (e) {
return Binary_1.hex_encode((await this._data).idata);
}
return Serialization_1.deserialize((await this._data).immutable_serialized_data, await scheme.format());
}
async mutableData() {
const scheme = await this._scheme;
try {
return Serialization_1.deserialize((await this._data).mutable_serialized_data, await scheme.format());
}
catch (e) {
return Binary_1.hex_encode((await this._data).mdata);
}
return Serialization_1.deserialize((await this._data).mutable_serialized_data, await scheme.format());
}

@@ -82,0 +71,0 @@ async isTransferable() {

@@ -11,6 +11,6 @@ import { AssetRow, CollectionRow, OfferRow, PresetRow, SchemeRow } from "./Cache";

account_assets(account: string, useCache?: boolean): Promise<AssetRow[]>;
preset(id: number, useCache?: boolean): Promise<PresetRow>;
preset(id: string, useCache?: boolean): Promise<PresetRow>;
scheme(name: string, useCache?: boolean): Promise<SchemeRow>;
collection(name: string, useCache?: boolean): Promise<CollectionRow>;
offer(id: number, useCache?: boolean): Promise<OfferRow>;
offer(id: string, useCache?: boolean): Promise<OfferRow>;
account_offers(account: string, useCache?: boolean): Promise<OfferRow[]>;

@@ -17,0 +17,0 @@ private dequeue;

@@ -54,3 +54,3 @@ "use strict";

async fetch_single_row(scope, table, match, cache = null, useCache = true, indexPosition = 1, keyType = "") {
let data = useCache ? cache(match) : null;
let data = cache(match, useCache ? undefined : false);
return new Promise((resolve, reject) => {

@@ -61,3 +61,3 @@ if (data) {

this.elements.push(async () => {
data = useCache ? cache(match) : null;
data = cache(match, useCache ? undefined : false);
if (data) {

@@ -70,3 +70,3 @@ if (this.elements.length > 0) {

try {
const resp = await this.api.get_table_rows({
const resp = await this.api.getTableRows({
code: this.api.contract, table, scope,

@@ -91,3 +91,3 @@ limit: 1, lower_bound: match, upper_bound: match,

this.elements.push(async () => {
const resp = await this.api.get_table_rows({
const resp = await this.api.getTableRows({
code: this.api.contract, scope, table,

@@ -94,0 +94,0 @@ lower_bound: lowerBound, upper_bound: upperBound, limit: 100,

@@ -11,6 +11,13 @@ import { ISchema } from "../../Schema";

format(): Promise<ISchema>;
rawFormat(): Promise<{
name: string;
type: string;
}[]>;
toObject(): Promise<{
author: string;
format: any;
format: {
name: string;
type: string;
}[];
}>;
}

@@ -28,2 +28,5 @@ "use strict";

}
async rawFormat() {
return (await this._data).format;
}
async toObject() {

@@ -30,0 +33,0 @@ return {

@@ -16,3 +16,3 @@ "use strict";

while (state.position < state.data.length) {
const identifier = Binary_1.unpackInteger(state);
const identifier = Binary_1.varint_decode(state);
if (identifier.equals(0)) {

@@ -33,6 +33,6 @@ break;

}
data.push(Binary_1.packInteger(i + this.reserved));
data.push(Binary_1.varint_encode(i + this.reserved));
data.push(attribute.value.serialize(object[attribute.name]));
}
data.push(Binary_1.packInteger(0));
data.push(Binary_1.varint_encode(0));
return Binary_1.concat_byte_arrays(data);

@@ -39,0 +39,0 @@ }

@@ -9,3 +9,3 @@ "use strict";

deserialize(state) {
const length = Binary_1.unpackInteger(state).toJSNumber();
const length = Binary_1.varint_decode(state).toJSNumber();
const array = [];

@@ -18,3 +18,3 @@ for (let i = 0; i < length; i++) {

serialize(array) {
const data = [Binary_1.packInteger(array.length)];
const data = [Binary_1.varint_encode(array.length)];
for (const element of array) {

@@ -21,0 +21,0 @@ data.push(this.element.serialize(element));

import SerializationState from "./State";
import { BigInteger } from "big-integer";
export declare function packInteger(input: any, maxSize?: number): Uint8Array;
export declare function unpackInteger(state: SerializationState, maxSize?: number): BigInteger;
export declare function signInteger(input: any, size: number): BigInteger;
export declare function unsignInteger(input: any, size: number): BigInteger;
export declare function zigzag_encode(input: any, size?: number): BigInteger;
export declare function zigzag_decode(input: any, size?: number): BigInteger;
export declare function varint_encode(input: any): Uint8Array;
export declare function varint_decode(state: SerializationState): BigInteger;
export declare function integer_sign(input: any, size: number): BigInteger;
export declare function integer_unsign(input: any, size: number): BigInteger;
export declare function zigzag_encode(input: any): BigInteger;
export declare function zigzag_decode(input: any): BigInteger;
export declare function base58_decode(data: string): Uint8Array;

@@ -10,0 +10,0 @@ export declare function base58_encode(data: Uint8Array): string;

@@ -10,3 +10,3 @@ "use strict";

const Base_1 = __importDefault(require("./Coders/Base"));
function packInteger(input, maxSize = 8) {
function varint_encode(input) {
const bytes = [];

@@ -17,34 +17,17 @@ let n = big_integer_1.default(input);

}
if (maxSize > 8) {
throw new SerializationError_1.default("cant pack integer greater than 64bit");
}
if (n.greaterOrEquals(big_integer_1.default(2).pow(maxSize * 8))) {
throw new SerializationError_1.default("integer too large");
}
for (let i = 0; i < maxSize + 1; i++) {
let byte;
if (i === maxSize) {
byte = n.and(0xFF);
n = n.shiftRight(8);
for (let i = 0; true; i++) {
const byte = n.and(0x7F);
n = n.shiftRight(7);
if (n.equals(0)) {
bytes.push(byte.toJSNumber());
break;
}
else {
byte = n.and(0x7F);
n = n.shiftRight(7);
if (n.equals(0)) {
bytes.push(byte.toJSNumber());
break;
}
bytes.push(byte.toJSNumber() + 128);
}
bytes.push(byte.toJSNumber() + 128);
}
return new Uint8Array(bytes);
}
exports.packInteger = packInteger;
function unpackInteger(state, maxSize = 8) {
exports.varint_encode = varint_encode;
function varint_decode(state) {
let result = big_integer_1.default(0);
if (maxSize > 8) {
throw new DeserializationError_1.default("cant unpack integer greater than 64bit");
}
for (let i = 0; i < maxSize + 1; i++) {
for (let i = 0; true; i++) {
if (state.position >= state.data.length) {

@@ -55,3 +38,3 @@ throw new DeserializationError_1.default("failed to unpack integer");

state.position += 1;
if (byte.lesser(128) || i === maxSize) {
if (byte.lesser(128)) {
result = result.plus(byte.shiftLeft(7 * i));

@@ -66,4 +49,4 @@ break;

}
exports.unpackInteger = unpackInteger;
function signInteger(input, size) {
exports.varint_decode = varint_decode;
function integer_sign(input, size) {
const n = big_integer_1.default(input);

@@ -80,4 +63,4 @@ if (n.greaterOrEquals(big_integer_1.default(2).pow(8 * size - 1))) {

}
exports.signInteger = signInteger;
function unsignInteger(input, size) {
exports.integer_sign = integer_sign;
function integer_unsign(input, size) {
const n = big_integer_1.default(input);

@@ -94,19 +77,21 @@ if (n.greater(big_integer_1.default(2).pow(8 * size))) {

}
exports.unsignInteger = unsignInteger;
function zigzag_encode(input, size = 8) {
exports.integer_unsign = integer_unsign;
function zigzag_encode(input) {
const n = big_integer_1.default(input);
const result = n.shiftRight(size * 8 - 1).xor(n.shiftLeft(1));
if (result.greaterOrEquals(big_integer_1.default(2).pow(size * 8))) {
throw new Error("zigzag encode: number too big");
if (n.lesserOrEquals(0)) {
return n.plus(1).multiply(-2).plus(1);
}
return result;
else {
return n.multiply(2);
}
}
exports.zigzag_encode = zigzag_encode;
function zigzag_decode(input, size = 8) {
function zigzag_decode(input) {
const n = big_integer_1.default(input);
const result = n.shiftRight(1).xor(big_integer_1.default(-1).multiply(n.and(1)));
if (result.greaterOrEquals(big_integer_1.default(2).pow((size * 8) - 1))) {
throw new Error("zigzag encode: number too big");
if (n.mod(2).equals(0)) {
return n.divmod(2).quotient;
}
return result;
else {
return n.divmod(2).quotient.multiply(-1).minus(1);
}
}

@@ -113,0 +98,0 @@ exports.zigzag_decode = zigzag_decode;

@@ -20,3 +20,3 @@ "use strict";

if (schema instanceof MappingSchema_1.default) {
data = Binary_1.concat_byte_arrays([data, Binary_1.packInteger(0)]);
data = Binary_1.concat_byte_arrays([data, Binary_1.varint_encode(0)]);
}

@@ -23,0 +23,0 @@ const state = new State_1.default(data, 0);

@@ -17,3 +17,3 @@ "use strict";

}
n = Binary_1.unsignInteger(n, this.size);
n = Binary_1.integer_unsign(n, this.size);
if (this.size <= 6) {

@@ -27,3 +27,3 @@ return n.toJSNumber();

const buffer = [];
n = Binary_1.signInteger(n, this.size);
n = Binary_1.integer_sign(n, this.size);
for (let i = 0; i < this.size; i++) {

@@ -30,0 +30,0 @@ buffer.push(n.and(0xFF).toJSNumber());

@@ -6,2 +6,4 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
const DeserializationError_1 = __importDefault(require("../../Errors/DeserializationError"));
const SerializationError_1 = __importDefault(require("../../Errors/SerializationError"));
const Binary_1 = require("../Binary");

@@ -15,6 +17,9 @@ const big_integer_1 = __importDefault(require("big-integer"));

deserialize(state) {
let n = Binary_1.unpackInteger(state, this.size);
let n = Binary_1.varint_decode(state);
if (!this.unsigned) {
n = Binary_1.zigzag_decode(n, this.size);
n = Binary_1.zigzag_decode(n);
}
if (n.greaterOrEquals(big_integer_1.default(2).pow(this.size * 8 - (this.unsigned ? 0 : 1)))) {
throw new DeserializationError_1.default("number '" + n.toString() + "' too large for given type");
}
if (this.size <= 6) {

@@ -27,8 +32,11 @@ return n.toJSNumber();

let n = big_integer_1.default(data);
if (n.greaterOrEquals(big_integer_1.default(2).pow(this.size * 8 - (this.unsigned ? 0 : 1)))) {
throw new SerializationError_1.default("number '" + n.toString() + "' too large for given type");
}
if (!this.unsigned) {
n = Binary_1.zigzag_encode(n, this.size);
n = Binary_1.zigzag_encode(n);
}
return Binary_1.packInteger(n, this.size);
return Binary_1.varint_encode(n);
}
}
exports.default = VariableIntegerParser;

@@ -10,3 +10,3 @@ "use strict";

deserialize(state) {
const length = Binary_1.unpackInteger(state).toJSNumber();
const length = Binary_1.varint_decode(state).toJSNumber();
state.position += length;

@@ -20,5 +20,5 @@ const data = state.data.slice(state.position - length, state.position);

serialize(data) {
return Binary_1.concat_byte_arrays([Binary_1.packInteger(data.length), data]);
return Binary_1.concat_byte_arrays([Binary_1.varint_encode(data.length), data]);
}
}
exports.default = VariableParser;

@@ -12,3 +12,3 @@ {

"license": "MIT",
"version": "0.2.1",
"version": "0.3.0",
"main": "build/index.js",

@@ -15,0 +15,0 @@ "types": "build/index.d.ts",

# AtomicAssets JavaScript
JS Library to read data from the atomicassets NFT standard
JS Library to read data from the atomicassets NFT standard.
Contract / General Documentation can be found on [https://github.com/pinknetworkx/atomicassets-contracts/wiki](https://github.com/pinknetworkx/atomicassets-contracts/wiki)
## Usage

@@ -17,2 +19,4 @@

Web library can be found in the `dist` folder
```javascript

@@ -29,11 +33,18 @@ // standard import

// contract: account name where the contract is deployed
// coreToken: Core token of the blockchain
// precision: Precision of the core token
// options:
// - fetch: either node-fetch module or the browser equivalent
// - rateLimit: defines how much requests per second can be made to not exceed the rate limit of the node
const api = new RpcApi("https://wax.pink.gg", "atomicassets", {fetch, rateLimit: 4});
const api = new RpcApi("https://wax.pink.gg", "atomicassets", "WAX", 8, {fetch, rateLimit: 4});
// fetch preset data
const preset = await api.getPreset("13456720837456");
// create the action to mint an asset
const action = api.action.mintasset(
[{actor: "pinknetworkx", permission: "active"}],
"pinknetworkx", preset.id, "pinknetworkx", {"attr1": 10}, {"attr2": 20}
)
/* YOUR CODE HERE */
// stop fetch queue
api.queue.stop();
```

@@ -47,2 +58,4 @@

More information can be found [here](https://github.com/pinknetworkx/atomicassets-contracts/wiki/Serialization)
```javascript

@@ -89,6 +102,13 @@ import {serialize, deserialize, ObjectSchema} from "atomicassets"

### RpcActionGenerator
Both APIs have a `action` attribute which contains a helper class to construct contract actions
which can be pushed on chain with eosjs. See an example on top of the page.
Detailed information about each action can be found [here](https://github.com/pinknetworkx/atomicassets-contracts/wiki/Actions)
### RpcApi
This api only uses native nodeos api calls to fetch data about NFTs.
It is recommended to use the Explorer API for production or application which need fast load times.
It is recommended to use the Explorer API for production or applications which require fast load times.

@@ -99,20 +119,22 @@ #### Methods

* `async get_asset(owner: string, id: string, cache: boolean = true): Promise<RpcAsset>`
* `async getAsset(owner: string, id: string, cache: boolean = true): Promise<RpcAsset>`
* gets data about a specific asset owned by owner
* `async get_preset(id: number, cache: boolean = true): Promise<RpcPreset>`
* `async getPreset(id: string, cache: boolean = true): Promise<RpcPreset>`
* gets a specific preset by id
* `async get_scheme(name: string, cache: boolean = true): Promise<RpcScheme>`
* `async getScheme(name: string, cache: boolean = true): Promise<RpcScheme>`
* get a scheme by its name
* `async get_offer(id: number, cache: boolean = true): Promise<RpcOffer>`
* `async getCollection(name: string, cache: boolean = true): Promise<RpcCollection>`
* gets an offer by its id
* `async get_account_offers(account: string, cache: boolean = true): Promise<RpcOffer[]>`
* `async getOffer(id: string, cache: boolean = true): Promise<RpcOffer>`
* gets an offer by its id
* `async getAccountOffers(account: string, cache: boolean = true): Promise<RpcOffer[]>`
* get all offers which are sent or received by an account
* `async get_account_assets(account: string, cache: boolean = true): Promise<RpcAsset[]>`
* gets the inventory of a specific account
* `async getAccountAssets(account: string, cache: boolean = true): Promise<RpcAsset[]>`
* gets the complete inventory of a specific account (may take long for bigger inventories)
#### Classes
#### Types
These classes represent table rows of the contract and consist of getter methods
which return the deserialized data.
The method `toObject` returns a JavaScript object representation of the class.
The method `toObject` returns a JavaScript object representation of the class.

@@ -123,4 +145,5 @@ ##### RpcAsset

* `async backedTokens(): Promise<string>`
* `async immutableData(): Promise<object | string>`
* `async mutableData(): Promise<object | string>`
* `async immutableData(): Promise<object>`
* `async mutableData(): Promise<object>`
* `async data(): Promise<object>`
* `async toObject(): Promise<object>`

@@ -132,4 +155,4 @@

* `async scheme(): Promise<RpcScheme>`
* `async immutableData(): Promise<object | string>`
* `async mutableData(): Promise<object | string>`
* `async immutableData(): Promise<object>`
* `async mutableData(): Promise<object>`
* `async isTransferable(): Promise<boolean>`

@@ -136,0 +159,0 @@ * `async isBurnable(): Promise<boolean>`

@@ -0,3 +1,3 @@

import bigInt from "big-integer";
import {deserialize} from "../../Serialization";
import {hex_encode} from "../../Serialization/Binary";
import {AssetRow} from "./Cache";

@@ -50,3 +50,3 @@ import RpcApi from "./index";

public async backedTokens(): Promise<string> {
return (await this._data).backed_core_tokens;
return bigInt((await this._data).backed_core_amount).divide(this.api.precision).toString();
}

@@ -59,13 +59,5 @@

const data = await this._data;
const pdata = await preset.immutableData();
let pdata = await preset.immutableData();
if(typeof pdata === "string") {
pdata = {};
}
try {
return Object.assign({}, pdata, deserialize(data.immutable_serialized_data, await scheme.format()));
} catch (e) {
return hex_encode(data.idata);
}
return Object.assign({}, pdata, deserialize(data.immutable_serialized_data, await scheme.format()));
}

@@ -78,13 +70,12 @@

const data = await this._data;
const pdata = await preset.mutableData();
let pdata = await preset.mutableData();
if(typeof pdata === "string") {
pdata = {};
}
return Object.assign({}, pdata, deserialize(data.mutable_serialized_data, await scheme.format()));
}
try {
return Object.assign({}, pdata, deserialize(data.mutable_serialized_data, await scheme.format()));
} catch (e) {
return hex_encode(data.mdata);
}
public async data(): Promise<object> {
const mutableData = await this.mutableData();
const immutableData = await this.immutableData();
return Object.assign({}, mutableData, immutableData);
}

@@ -91,0 +82,0 @@

import {byte_vector_to_int} from "../../Serialization/Binary";
export type AssetRow = any;
export type PresetRow = any;
export type SchemeRow = any;
export type CollectionRow = any;
export type OfferRow = any;
export type ConfigRow = any;
export type SchemeFormat = Array<{name: string, type: string}>;
export type AssetRow = {
id: string, preset_id: string, ram_payer: string, backed_core_amount: string,
immutable_serialized_data: Uint8Array, mutable_serialized_data: Uint8Array,
};
export type PresetRow = {
scheme_name: string, collection_name: string,
transferable: boolean, burnable: boolean,
max_supply: number, issued_supply: number,
mutable_serialized_data: Uint8Array,
immutable_serialized_data: Uint8Array,
};
export type SchemeRow = {scheme_name: string, author: string, format: SchemeFormat};
export type CollectionRow = {collection_name: string, author: string, authorized_accounts: string[], notify_accounts: string[], serialized_data: Uint8Array};
export type OfferRow = {
id: string, offer_sender: string, offer_recipient: string,
sender_asset_ids: string[], recipient_asset_ids: string[],
memo: string,
};
export type ConfigRow = {offer_counter: string, collection_format: SchemeFormat};
export default class RpcCache {
private readonly assets: {[id: string]: {data: AssetRow, expiration: number}} = {};
private readonly presets: {[id: string]: {data: PresetRow, expiration: number}} = {};
private readonly schemes: {[id: string]: {data: SchemeRow, expiration: number}} = {};
private readonly collections: {[id: string]: {data: CollectionRow, expiration: number}} = {};
private readonly offers: {[id: string]: {data: OfferRow, expiration: number}} = {};
private readonly assets: {[id: string]: {data: AssetRow, expiration: number, updated: number}} = {};
private readonly presets: {[id: string]: {data: PresetRow, expiration: number, updated: number}} = {};
private readonly schemes: {[id: string]: {data: SchemeRow, expiration: number, updated: number}} = {};
private readonly collections: {[id: string]: {data: CollectionRow, expiration: number, updated: number}} = {};
private readonly offers: {[id: string]: {data: OfferRow, expiration: number, updated: number}} = {};
public asset(assetID: string, data?: AssetRow): AssetRow | null {
public asset(assetID: string, data?: any | null | false): AssetRow | undefined {
if(data) {
data.mutable_serialized_data = new Uint8Array(data.mutable_serialized_data);
data.immutable_serialized_data = new Uint8Array(data.immutable_serialized_data);
data.backed_core_tokens = data.backed_core_tokens.replace(" WAX", "");
}

@@ -27,3 +41,3 @@

public preset(presetID: number, data?: PresetRow): PresetRow | null {
public preset(presetID: string, data?: any | null | false): PresetRow | undefined {
if(data) {

@@ -39,17 +53,27 @@ data.mutable_serialized_data = new Uint8Array(data.mutable_serialized_data);

public scheme(scheme: string, data?: SchemeRow): SchemeRow | null {
public scheme(scheme: string, data?: any | null | false): SchemeRow | undefined {
return this.access<SchemeRow>(scheme, this.schemes, data);
}
public collection(collection: string, data?: CollectionRow): CollectionRow | null {
public collection(collection: string, data?: any | null | false): CollectionRow | undefined {
return this.access<CollectionRow>(collection, this.collections, data);
}
public offer(offerID: string, data?: OfferRow): OfferRow | null {
public offer(offerID: string, data?: any | null | false): OfferRow | undefined {
return this.access<OfferRow>(offerID, this.offers, data);
}
private access<T>(identifier: string | number, cache: {[id: string]: {expiration: number, data: T}}, data?: T): T | undefined {
private access<T>(
identifier: string | number,
cache: {[id: string]: {expiration: number, updated: number, data: T}},
data?: T | null | false,
): T | undefined {
if(data === null) {
delete cache[String(identifier)];
return undefined;
}
if(data) {
cache[String(identifier)] = {expiration: Date.now() + 15 * 60 * 1000, data};
cache[String(identifier)] = {expiration: Date.now() + 15 * 60 * 1000, updated: Date.now(), data};

@@ -63,4 +87,9 @@ return data;

// if data is false then only return cache if it is not older than 5 seconds
if(data === false && Date.now() - cache[String(identifier)].updated > 5 * 1000) {
return undefined;
}
return cache[String(identifier)].data;
}
}

@@ -0,1 +1,2 @@

import {ObjectSchema} from "../../Schema";
import {deserialize} from "../../Serialization";

@@ -40,3 +41,3 @@ import {CollectionRow} from "./Cache";

public async data(): Promise<any> {
return deserialize((await this._data).serialized_data, (await this.api.config()).collection_format);
return deserialize((await this._data).serialized_data, ObjectSchema((await this.api.config()).collection_format));
}

@@ -43,0 +44,0 @@

@@ -0,5 +1,7 @@

import ActionGenerator from "../../Actions/Generator";
import RpcActionGenerator from "../../Actions/Rpc";
import RpcError from "../../Errors/RpcError";
import {ObjectSchema} from "../../Schema";
import RpcAsset from "./Asset";
import RpcCache, {ConfigRow} from "./Cache";
import RpcCollection from "./Collection";
import RpcOffer from "./Offer";

@@ -16,5 +18,8 @@ import RpcPreset from "./Preset";

public readonly cache: RpcCache;
public readonly action: ActionGenerator;
public readonly endpoint: string;
public readonly contract: string;
public readonly coreToken: string;
public readonly precision: number;

@@ -26,5 +31,7 @@ private readonly fetchBuiltin: Fetch;

constructor(endpoint: string, contract: string, args: ApiArgs = {rateLimit: 4}) {
constructor(endpoint: string, contract: string, coreToken: string, precision: number, args: ApiArgs = {rateLimit: 4}) {
this.endpoint = endpoint;
this.contract = contract;
this.coreToken = coreToken;
this.precision = precision;

@@ -39,6 +46,7 @@ if (args.fetch) {

this.cache = new RpcCache();
this.action = new RpcActionGenerator(this);
this._config = new Promise((async (resolve, reject) => {
try {
const resp = await this.get_table_rows({
const resp = await this.getTableRows({
code: this.contract, scope: this.contract, table: "config",

@@ -51,6 +59,3 @@ });

const data = resp.rows[0];
data.collection_format = ObjectSchema(data.collection_format);
return resolve(data);
return resolve(resp.rows[0]);
} catch (e) {

@@ -62,23 +67,47 @@ reject(e);

public async config() {
public async config(): Promise<ConfigRow> {
return await this._config;
}
public async get_asset(owner: string, id: string, cache: boolean = true): Promise<RpcAsset> {
public async getAsset(owner: string, id: string, cache: boolean = true): Promise<RpcAsset> {
if(!cache) {
this.cache.asset(id, null);
}
return new RpcAsset(this, owner, id, undefined, undefined, cache);
}
public async get_preset(id: number, cache: boolean = true): Promise<RpcPreset> {
public async getPreset(id: string, cache: boolean = true): Promise<RpcPreset> {
if(!cache) {
this.cache.preset(id, null);
}
return new RpcPreset(this, id, undefined, undefined, undefined, cache);
}
public async get_scheme(name: string, cache: boolean = true): Promise<RpcScheme> {
public async getCollection(name: string, cache: boolean = true) {
if(!cache) {
this.cache.collection(name, null);
}
return new RpcCollection(this, name, undefined, cache);
}
public async getScheme(name: string, cache: boolean = true): Promise<RpcScheme> {
if(!cache) {
this.cache.scheme(name, null);
}
return new RpcScheme(this, name, undefined, cache);
}
public async get_offer(id: number, cache: boolean = true): Promise<RpcOffer> {
public async getOffer(id: string, cache: boolean = true): Promise<RpcOffer> {
if(!cache) {
this.cache.offer(id, null);
}
return new RpcOffer(this, id, undefined, undefined, undefined, cache);
}
public async get_account_offers(account: string, cache: boolean = true): Promise<RpcOffer[]> {
public async getAccountOffers(account: string, cache: boolean = true): Promise<RpcOffer[]> {
return (await this.queue.account_offers(account)).map((offer) => {

@@ -89,3 +118,3 @@ return new RpcOffer(this, offer.id, offer, undefined, undefined, cache);

public async get_account_assets(account: string, cache: boolean = true): Promise<RpcAsset[]> {
public async getAccountAssets(account: string, cache: boolean = true): Promise<RpcAsset[]> {
return (await this.queue.account_assets(account)).map((asset) => {

@@ -96,7 +125,7 @@ return new RpcAsset(this, account, asset.id, asset, undefined, cache);

public async get_table_rows({
public async getTableRows({
code, scope, table, table_key = "", lower_bound = "", upper_bound = "",
index_position = 1, key_type = "",
}: any): Promise<any> {
return await this.fetch_rpc("/v1/chain/get_table_rows", {
return await this.fetchRpc("/v1/chain/get_table_rows", {
code, scope, table, table_key,

@@ -108,8 +137,6 @@ lower_bound, upper_bound, index_position,

public async fetch_rpc(path: string, body: any) {
public async fetchRpc(path: string, body: any) {
let response;
let json;
// console.log(path, body);
try {

@@ -116,0 +143,0 @@ response = await this.fetchBuiltin(this.endpoint + path, {

@@ -6,3 +6,3 @@ import RpcAsset from "./Asset";

export default class RpcOffer {
public readonly id: number;
public readonly id: string;

@@ -16,3 +16,3 @@ // tslint:disable-next-line:variable-name

constructor(private readonly api: RpcApi, id: number, data?: OfferRow, senderAssets?: RpcAsset[], receiverAssets?: RpcAsset[], cache: boolean = true) {
constructor(private readonly api: RpcApi, id: string, data?: OfferRow, senderAssets?: RpcAsset[], receiverAssets?: RpcAsset[], cache: boolean = true) {
this.id = id;

@@ -38,5 +38,5 @@

const row: OfferRow = await this._data;
const inventory: AssetRow[] = await this.api.queue.account_assets(await row.offer_sender, cache);
const inventory: AssetRow[] = await this.api.queue.account_assets(row.offer_sender, cache);
let offerAssets = inventory.filter((element) => {
const offerAssets = inventory.filter((element) => {
return row.sender_asset_ids.indexOf(element.id) >= 0;

@@ -49,5 +49,3 @@ });

offerAssets = offerAssets.map((asset) => new RpcAsset(this.api, asset.owner, asset.id, asset));
return resolve(offerAssets);
return resolve(offerAssets.map((asset) => new RpcAsset(this.api, row.offer_sender, asset.id, asset)));
} catch (e) {

@@ -65,5 +63,5 @@ return reject(e);

const row: OfferRow = await this._data;
const inventory: AssetRow[] = await this.api.queue.account_assets(await row.offer_recipient, cache);
let offerAssets = inventory.filter((element) => {
const inventory: AssetRow[] = await this.api.queue.account_assets(row.offer_recipient, cache);
const offerAssets = inventory.filter((element) => {
return row.recipient_asset_ids.indexOf(element.id) >= 0;

@@ -76,5 +74,3 @@ });

offerAssets = offerAssets.map((asset) => new RpcAsset(this.api, asset.owner, asset.id, asset));
return resolve(offerAssets);
return resolve(offerAssets.map((asset) => new RpcAsset(this.api, row.offer_recipient, asset.id, asset)));
} catch (e) {

@@ -81,0 +77,0 @@ return reject(e);

@@ -9,3 +9,3 @@ import {deserialize} from "../../Serialization";

export default class RpcPreset {
public readonly id: number;
public readonly id: string;

@@ -20,3 +20,3 @@ // tslint:disable-next-line:variable-name

public constructor(private readonly api: RpcApi, id: number, data?: PresetRow, scheme?: RpcScheme, collection?: RpcCollection, cache: boolean = true) {
public constructor(private readonly api: RpcApi, id: string, data?: PresetRow, scheme?: RpcScheme, collection?: RpcCollection, cache: boolean = true) {
this.id = id;

@@ -73,20 +73,12 @@

public async immutableData(): Promise<object | string> {
public async immutableData(): Promise<object> {
const scheme = await this._scheme;
try {
return deserialize((await this._data).immutable_serialized_data, await scheme.format());
} catch (e) {
return hex_encode((await this._data).idata);
}
return deserialize((await this._data).immutable_serialized_data, await scheme.format());
}
public async mutableData(): Promise<object | string> {
public async mutableData(): Promise<object> {
const scheme = await this._scheme;
try {
return deserialize((await this._data).mutable_serialized_data, await scheme.format());
} catch (e) {
return hex_encode((await this._data).mdata);
}
return deserialize((await this._data).mutable_serialized_data, await scheme.format());
}

@@ -93,0 +85,0 @@

@@ -20,3 +20,3 @@ import RpcError from "../../Errors/RpcError";

public async preset(id: number, useCache: boolean = true): Promise<PresetRow> {
public async preset(id: string, useCache: boolean = true): Promise<PresetRow> {
return this.fetch_single_row(this.api.contract, "presets", id, this.api.cache.preset.bind(this.api.cache), useCache);

@@ -33,3 +33,3 @@ }

public async offer(id: number, useCache: boolean = true): Promise<OfferRow> {
public async offer(id: string, useCache: boolean = true): Promise<OfferRow> {
return this.fetch_single_row(this.api.contract, "offers", id, this.api.cache.offer.bind(this.api.cache), useCache);

@@ -72,4 +72,4 @@ }

indexPosition = 1, keyType = "",
) {
let data = useCache ? cache(match) : null;
): Promise<any> {
let data = cache(match, useCache ? undefined : false);

@@ -82,3 +82,3 @@ return new Promise((resolve, reject) => {

this.elements.push(async () => {
data = useCache ? cache(match) : null;
data = cache(match, useCache ? undefined : false);

@@ -94,3 +94,3 @@ if(data) {

try {
const resp = await this.api.get_table_rows({
const resp = await this.api.getTableRows({
code: this.api.contract, table, scope,

@@ -123,3 +123,3 @@ limit: 1, lower_bound: match, upper_bound: match,

this.elements.push(async () => {
const resp: { more: boolean, rows: any[] } = await this.api.get_table_rows({
const resp: { more: boolean, rows: any[] } = await this.api.getTableRows({
code: this.api.contract, scope, table,

@@ -126,0 +126,0 @@ lower_bound: lowerBound, upper_bound: upperBound, limit: 100,

@@ -35,2 +35,6 @@ import {ISchema, ObjectSchema} from "../../Schema";

public async rawFormat() {
return (await this._data).format;
}
public async toObject() {

@@ -37,0 +41,0 @@ return {

import SchemaError from "../Errors/SchemaError";
import {concat_byte_arrays, packInteger, unpackInteger} from "../Serialization/Binary";
import {concat_byte_arrays, varint_decode, varint_encode} from "../Serialization/Binary";
import SerializationState from "../Serialization/State";

@@ -15,3 +15,3 @@ import {ISchema, MappingAttribute} from "./index";

while(state.position < state.data.length) {
const identifier = unpackInteger(state);
const identifier = varint_decode(state);

@@ -40,7 +40,7 @@ if(identifier.equals(0)) {

data.push(packInteger(i + this.reserved));
data.push(varint_encode(i + this.reserved));
data.push(attribute.value.serialize(object[attribute.name]));
}
data.push(packInteger(0));
data.push(varint_encode(0));

@@ -47,0 +47,0 @@ return concat_byte_arrays(data);

@@ -1,2 +0,2 @@

import {concat_byte_arrays, packInteger, unpackInteger} from "../Serialization/Binary";
import {concat_byte_arrays, varint_decode, varint_encode} from "../Serialization/Binary";
import SerializationState from "../Serialization/State";

@@ -9,3 +9,3 @@ import {ISchema} from "./index";

public deserialize(state: SerializationState): any {
const length = unpackInteger(state).toJSNumber();
const length = varint_decode(state).toJSNumber();
const array: any[] = [];

@@ -21,3 +21,3 @@

public serialize(array: any[]): Uint8Array {
const data: Uint8Array[] = [packInteger(array.length)];
const data: Uint8Array[] = [varint_encode(array.length)];

@@ -24,0 +24,0 @@ for(const element of array) {

@@ -8,3 +8,3 @@ import DeserializationError from "../Errors/DeserializationError";

export function packInteger(input: any, maxSize: number = 8): Uint8Array {
export function varint_encode(input: any): Uint8Array {
const bytes: number[] = [];

@@ -17,30 +17,14 @@ let n = bigInt(input);

if(maxSize > 8) {
throw new SerializationError("cant pack integer greater than 64bit");
}
for(let i = 0; true; i++) {
const byte = n.and(0x7F);
if(n.greaterOrEquals(bigInt(2).pow(maxSize * 8))) {
throw new SerializationError("integer too large");
}
n = n.shiftRight(7);
for(let i = 0; i < maxSize + 1; i++) {
let byte;
if(i === maxSize) {
byte = n.and(0xFF);
n = n.shiftRight(8);
if(n.equals(0)) {
bytes.push(byte.toJSNumber());
} else {
byte = n.and(0x7F);
n = n.shiftRight(7);
if(n.equals(0)) {
bytes.push(byte.toJSNumber());
break;
}
break;
}
bytes.push(byte.toJSNumber() + 128);
}
bytes.push(byte.toJSNumber() + 128);
}

@@ -51,10 +35,6 @@

export function unpackInteger(state: SerializationState, maxSize: number = 8): BigInteger {
export function varint_decode(state: SerializationState): BigInteger {
let result: BigInteger = bigInt(0);
if(maxSize > 8) {
throw new DeserializationError("cant unpack integer greater than 64bit");
}
for(let i = 0; i < maxSize + 1; i++) {
for(let i = 0; true; i++) {
if(state.position >= state.data.length) {

@@ -67,3 +47,3 @@ throw new DeserializationError("failed to unpack integer");

if(byte.lesser(128) || i === maxSize) {
if(byte.lesser(128)) {
result = result.plus(byte.shiftLeft(7 * i));

@@ -80,3 +60,3 @@

export function signInteger(input: any, size: number): BigInteger {
export function integer_sign(input: any, size: number): BigInteger {
const n = bigInt(input);

@@ -95,3 +75,3 @@

export function unsignInteger(input: any, size: number): BigInteger {
export function integer_unsign(input: any, size: number): BigInteger {
const n = bigInt(input);

@@ -110,22 +90,20 @@

export function zigzag_encode(input: any, size = 8): BigInteger {
export function zigzag_encode(input: any): BigInteger {
const n = bigInt(input);
const result = n.shiftRight(size * 8 - 1).xor(n.shiftLeft(1));
if(result.greaterOrEquals(bigInt(2).pow(size * 8))) {
throw new Error("zigzag encode: number too big");
if (n.lesserOrEquals(0)) {
return n.plus(1).multiply(-2).plus(1);
} else {
return n.multiply(2);
}
return result;
}
export function zigzag_decode(input: any, size = 8): BigInteger {
export function zigzag_decode(input: any): BigInteger {
const n = bigInt(input);
const result = n.shiftRight(1).xor(bigInt(-1).multiply(n.and(1)));
if(result.greaterOrEquals(bigInt(2).pow((size * 8) - 1))) {
throw new Error("zigzag encode: number too big");
if (n.mod(2).equals(0)) {
return n.divmod(2).quotient;
} else {
return n.divmod(2).quotient.multiply(-1).minus(1);
}
return result;
}

@@ -132,0 +110,0 @@

import {ISchema} from "../Schema";
import MappingSchema from "../Schema/MappingSchema";
import {concat_byte_arrays, packInteger} from "./Binary";
import {concat_byte_arrays, varint_encode} from "./Binary";
import SerializationState from "./State";

@@ -19,3 +19,3 @@

if(schema instanceof MappingSchema) {
data = concat_byte_arrays([data, packInteger(0)]);
data = concat_byte_arrays([data, varint_encode(0)]);
}

@@ -22,0 +22,0 @@

@@ -1,2 +0,2 @@

import {signInteger, unsignInteger} from "../Binary";
import {integer_sign, integer_unsign} from "../Binary";
import SerializationState from "../State";

@@ -17,3 +17,3 @@ import FixedParser from "./FixedParser";

n = unsignInteger(n, this.size);
n = integer_unsign(n, this.size);

@@ -31,3 +31,3 @@ if(this.size <= 6) {

n = signInteger(n, this.size);
n = integer_sign(n, this.size);

@@ -34,0 +34,0 @@ for(let i = 0; i < this.size; i++) {

@@ -1,2 +0,4 @@

import {packInteger, signInteger, unpackInteger, unsignInteger, zigzag_decode, zigzag_encode} from "../Binary";
import DeserializationError from "../../Errors/DeserializationError";
import SerializationError from "../../Errors/SerializationError";
import {varint_decode, varint_encode, zigzag_decode, zigzag_encode} from "../Binary";
import SerializationState from "../State";

@@ -11,8 +13,12 @@ import {ITypeParser} from "./index";

public deserialize(state: SerializationState): number | string {
let n = unpackInteger(state, this.size);
let n = varint_decode(state);
if(!this.unsigned) {
n = zigzag_decode(n, this.size);
n = zigzag_decode(n);
}
if(n.greaterOrEquals(bigInt(2).pow(this.size * 8 - (this.unsigned ? 0 : 1)))) {
throw new DeserializationError("number '" + n.toString() + "' too large for given type");
}
if(this.size <= 6) {

@@ -28,8 +34,12 @@ return n.toJSNumber();

if(n.greaterOrEquals(bigInt(2).pow(this.size * 8 - (this.unsigned ? 0 : 1)))) {
throw new SerializationError("number '" + n.toString() + "' too large for given type");
}
if(!this.unsigned) {
n = zigzag_encode(n, this.size);
n = zigzag_encode(n);
}
return packInteger(n, this.size);
return varint_encode(n);
}
}
import DeserializationError from "../../Errors/DeserializationError";
import {concat_byte_arrays, packInteger, unpackInteger} from "../Binary";
import {concat_byte_arrays, varint_decode, varint_encode} from "../Binary";
import SerializationState from "../State";

@@ -8,3 +8,3 @@ import {ITypeParser} from "./index";

public deserialize(state: SerializationState): any {
const length = unpackInteger(state).toJSNumber();
const length = varint_decode(state).toJSNumber();
state.position += length;

@@ -22,4 +22,4 @@

public serialize(data: any): Uint8Array {
return concat_byte_arrays([packInteger(data.length), data]);
return concat_byte_arrays([varint_encode(data.length), data]);
}
}

@@ -9,5 +9,5 @@ import { expect } from "chai";

hex_encode, int_to_byte_vector,
packInteger,
signInteger,
unpackInteger, zigzag_decode, zigzag_encode,
integer_sign,
varint_decode,
varint_encode, zigzag_decode, zigzag_encode,
} from "../src/Serialization/Binary";

@@ -18,11 +18,11 @@ import {prepare} from "../src/Serialization/State";

it("sign negative integer", () => {
expect(Number(signInteger(BigInt(-534), 2))).to.equal(65002);
expect(Number(integer_sign(BigInt(-534), 2))).to.equal(65002);
});
it("sign positive integer", () => {
expect(Number(signInteger(BigInt(534), 2))).to.equal(534);
expect(Number(integer_sign(BigInt(534), 2))).to.equal(534);
});
it("pack integer below 127", () => {
expect(packInteger(BigInt(5))).to.deep.equal(new Uint8Array([0b00000101]));
expect(varint_encode(BigInt(5))).to.deep.equal(new Uint8Array([0b00000101]));
});

@@ -33,7 +33,7 @@

expect(unpackInteger(prepare(data)).toJSNumber()).to.equal(5);
expect(varint_decode(prepare(data)).toJSNumber()).to.equal(5);
});
it("pack 2 bytes", () => {
expect(packInteger(BigInt(230))).to.deep.equal(new Uint8Array([0b11100110, 0b00000001]));
expect(varint_encode(BigInt(230))).to.deep.equal(new Uint8Array([0b11100110, 0b00000001]));
});

@@ -44,7 +44,7 @@

expect(unpackInteger(prepare(data)).toJSNumber()).to.equal(230);
expect(varint_decode(prepare(data)).toJSNumber()).to.equal(230);
});
it("pack max integer", () => {
expect(packInteger(BigInt(4294867286), 4)).to.deep.equal(new Uint8Array([0b11010110, 0b11110010, 0b11111001, 0b11111111, 0b00001111]));
expect(varint_encode(BigInt(4294867286))).to.deep.equal(new Uint8Array([0b11010110, 0b11110010, 0b11111001, 0b11111111, 0b00001111]));
});

@@ -55,3 +55,3 @@

expect(unpackInteger(prepare(data), 4).toJSNumber()).to.equal(4294867286);
expect(varint_decode(prepare(data)).toJSNumber()).to.equal(4294867286);
});

@@ -58,0 +58,0 @@

@@ -8,3 +8,3 @@ import { expect } from "chai";

describe("RPC API", () => {
const api = new RpcApi("https://testnet.wax.pink.gg", "atomicassets", {
const api = new RpcApi("https://testnet.wax.pink.gg", "atomicassets", "WAX", 8, {
fetch, rateLimit: 4,

@@ -14,8 +14,8 @@ });

const exampleAsset = {
owner: "karlkarlkarl",
id: "1099511627782",
owner: "leonleonleon",
id: "1099511627780",
};
it("fetch asset 1099511627782", async () => {
const asset = await api.get_asset(exampleAsset.owner, exampleAsset.id);
it("fetch asset " + exampleAsset.id, async () => {
const asset = await api.getAsset(exampleAsset.owner, exampleAsset.id);

@@ -28,3 +28,3 @@ const result = await asset.toObject();

it("test caching", async () => {
const asset = await api.get_asset(exampleAsset.owner, exampleAsset.id);
const asset = await api.getAsset(exampleAsset.owner, exampleAsset.id);

@@ -37,3 +37,3 @@ const result = await asset.toObject();

it("fetch offers ", async () => {
const offers = await api.get_account_offers(exampleAsset.owner);
const offers = await api.getAccountOffers(exampleAsset.owner);

@@ -40,0 +40,0 @@ const result = await Promise.all(offers.map(async (offer) => await offer.toObject()));

@@ -5,3 +5,3 @@ import { expect } from "chai";

import {deserialize, serialize} from "../src/Serialization";
import {base58_decode, concat_byte_arrays, hex_decode, packInteger, signInteger, zigzag_encode} from "../src/Serialization/Binary";
import {base58_decode, concat_byte_arrays, hex_decode, varint_encode, zigzag_encode} from "../src/Serialization/Binary";

@@ -44,4 +44,4 @@ describe("Basic Serialization", () => {

const serializedName = concat_byte_arrays([
packInteger(4),
packInteger(rawObject.name.length),
varint_encode(4),
varint_encode(rawObject.name.length),
new Uint8Array(encoder.encode(rawObject.name)),

@@ -51,4 +51,4 @@ ]);

const serializedImage = concat_byte_arrays([
packInteger(5),
packInteger(base58_decode(rawObject.img).length),
varint_encode(5),
varint_encode(base58_decode(rawObject.img).length),
base58_decode(rawObject.img),

@@ -58,9 +58,9 @@ ]);

const serializedRarity = concat_byte_arrays([
packInteger(6),
packInteger(zigzag_encode(534, 2), 2),
packInteger(7),
packInteger(534, 2),
packInteger(8),
packInteger(zigzag_encode(534, 8), 8),
packInteger(9),
varint_encode(6),
varint_encode(zigzag_encode(534)),
varint_encode(7),
varint_encode(534),
varint_encode(8),
varint_encode(zigzag_encode(534)),
varint_encode(9),
new Uint8Array([0x2, 0x16].reverse()),

@@ -70,9 +70,9 @@ ]);

const serializedDepth = concat_byte_arrays([
packInteger(10),
packInteger(zigzag_encode(-1000000, 4), 4),
packInteger(11),
packInteger(1000000, 4),
packInteger(12),
packInteger(zigzag_encode(-1000000, 8), 8),
packInteger(13),
varint_encode(10),
varint_encode(zigzag_encode(-1000000)),
varint_encode(11),
varint_encode(1000000),
varint_encode(12),
varint_encode(zigzag_encode(-1000000)),
varint_encode(13),
new Uint8Array([0b11111111, 0b11110000, 0b10111101, 0b11000000].reverse()),

@@ -82,3 +82,3 @@ ]);

const serializedWear = concat_byte_arrays([
packInteger(14),
varint_encode(14),
new Uint8Array([0x3f, 0x40, 0, 0].reverse()),

@@ -88,3 +88,3 @@ ]);

const serializedTradeable = concat_byte_arrays([
packInteger(15),
varint_encode(15),
new Uint8Array([1]),

@@ -94,3 +94,3 @@ ]);

const serializedShare = concat_byte_arrays([
packInteger(16),
varint_encode(16),
hex_decode("4090010000000000").reverse(),

@@ -97,0 +97,0 @@ ]);

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc