@waves/marshall
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -1,8 +0,8 @@ | ||
{"total": {"lines":{"total":472,"covered":443,"skipped":0,"pct":93.86},"statements":{"total":562,"covered":529,"skipped":0,"pct":94.13},"functions":{"total":103,"covered":98,"skipped":0,"pct":95.15},"branches":{"total":214,"covered":163,"skipped":0,"pct":76.17}} | ||
,"/Users/siem/IdeaProjects/tx-parse-serialize/src/index.ts": {"lines":{"total":35,"covered":33,"skipped":0,"pct":94.29},"functions":{"total":10,"covered":10,"skipped":0,"pct":100},"statements":{"total":38,"covered":36,"skipped":0,"pct":94.74},"branches":{"total":12,"covered":10,"skipped":0,"pct":83.33}} | ||
,"/Users/siem/IdeaProjects/tx-parse-serialize/src/parse.ts": {"lines":{"total":54,"covered":52,"skipped":0,"pct":96.3},"functions":{"total":6,"covered":6,"skipped":0,"pct":100},"statements":{"total":69,"covered":67,"skipped":0,"pct":97.1},"branches":{"total":28,"covered":25,"skipped":0,"pct":89.29}} | ||
{"total": {"lines":{"total":486,"covered":455,"skipped":0,"pct":93.62},"statements":{"total":578,"covered":542,"skipped":0,"pct":93.77},"functions":{"total":105,"covered":100,"skipped":0,"pct":95.24},"branches":{"total":230,"covered":174,"skipped":0,"pct":75.65}} | ||
,"/Users/siem/IdeaProjects/tx-parse-serialize/src/index.ts": {"lines":{"total":37,"covered":35,"skipped":0,"pct":94.59},"functions":{"total":10,"covered":10,"skipped":0,"pct":100},"statements":{"total":40,"covered":38,"skipped":0,"pct":95},"branches":{"total":14,"covered":12,"skipped":0,"pct":85.71}} | ||
,"/Users/siem/IdeaProjects/tx-parse-serialize/src/parse.ts": {"lines":{"total":58,"covered":56,"skipped":0,"pct":96.55},"functions":{"total":6,"covered":6,"skipped":0,"pct":100},"statements":{"total":74,"covered":71,"skipped":0,"pct":95.95},"branches":{"total":32,"covered":28,"skipped":0,"pct":87.5}} | ||
,"/Users/siem/IdeaProjects/tx-parse-serialize/src/parsePrimitives.ts": {"lines":{"total":68,"covered":62,"skipped":0,"pct":91.18},"functions":{"total":25,"covered":24,"skipped":0,"pct":96},"statements":{"total":97,"covered":90,"skipped":0,"pct":92.78},"branches":{"total":42,"covered":22,"skipped":0,"pct":52.38}} | ||
,"/Users/siem/IdeaProjects/tx-parse-serialize/src/serialize.ts": {"lines":{"total":40,"covered":37,"skipped":0,"pct":92.5},"functions":{"total":6,"covered":6,"skipped":0,"pct":100},"statements":{"total":45,"covered":42,"skipped":0,"pct":93.33},"branches":{"total":22,"covered":18,"skipped":0,"pct":81.82}} | ||
,"/Users/siem/IdeaProjects/tx-parse-serialize/src/serialize.ts": {"lines":{"total":43,"covered":38,"skipped":0,"pct":88.37},"functions":{"total":6,"covered":6,"skipped":0,"pct":100},"statements":{"total":48,"covered":43,"skipped":0,"pct":89.58},"branches":{"total":26,"covered":21,"skipped":0,"pct":80.77}} | ||
,"/Users/siem/IdeaProjects/tx-parse-serialize/src/serializePrimitives.ts": {"lines":{"total":39,"covered":39,"skipped":0,"pct":100},"functions":{"total":21,"covered":21,"skipped":0,"pct":100},"statements":{"total":53,"covered":53,"skipped":0,"pct":100},"branches":{"total":15,"covered":13,"skipped":0,"pct":86.67}} | ||
,"/Users/siem/IdeaProjects/tx-parse-serialize/src/txSchemas.ts": {"lines":{"total":78,"covered":76,"skipped":0,"pct":97.44},"functions":{"total":16,"covered":12,"skipped":0,"pct":75},"statements":{"total":88,"covered":84,"skipped":0,"pct":95.45},"branches":{"total":12,"covered":11,"skipped":0,"pct":91.67}} | ||
,"/Users/siem/IdeaProjects/tx-parse-serialize/src/txSchemas.ts": {"lines":{"total":82,"covered":80,"skipped":0,"pct":97.56},"functions":{"total":18,"covered":14,"skipped":0,"pct":77.78},"statements":{"total":93,"covered":89,"skipped":0,"pct":95.7},"branches":{"total":18,"covered":14,"skipped":0,"pct":77.78}} | ||
,"/Users/siem/IdeaProjects/tx-parse-serialize/src/txToJson.ts": {"lines":{"total":72,"covered":69,"skipped":0,"pct":95.83},"functions":{"total":9,"covered":9,"skipped":0,"pct":100},"statements":{"total":80,"covered":76,"skipped":0,"pct":95},"branches":{"total":59,"covered":52,"skipped":0,"pct":88.14}} | ||
@@ -12,3 +12,3 @@ ,"/Users/siem/IdeaProjects/tx-parse-serialize/src/libs/Utf8ArrayToStr.ts": {"lines":{"total":22,"covered":14,"skipped":0,"pct":63.64},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":22,"covered":14,"skipped":0,"pct":63.64},"branches":{"total":12,"covered":4,"skipped":0,"pct":33.33}} | ||
,"/Users/siem/IdeaProjects/tx-parse-serialize/src/libs/utils.ts": {"lines":{"total":5,"covered":5,"skipped":0,"pct":100},"functions":{"total":4,"covered":4,"skipped":0,"pct":100},"statements":{"total":9,"covered":9,"skipped":0,"pct":100},"branches":{"total":2,"covered":1,"skipped":0,"pct":50}} | ||
,"/Users/siem/IdeaProjects/tx-parse-serialize/test/exampleTxs.ts": {"lines":{"total":16,"covered":16,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":16,"covered":16,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}} | ||
,"/Users/siem/IdeaProjects/tx-parse-serialize/test/exampleTxs.ts": {"lines":{"total":17,"covered":17,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":17,"covered":17,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}} | ||
} |
import { ILongFactory } from "./txSchemas"; | ||
export declare namespace binary { | ||
function serializeTx<LONG = string | number>(tx: any, longFactory?: ILongFactory<LONG>): Uint8Array; | ||
/** | ||
* This function cannot transactions without version | ||
*/ | ||
function parseTx<LONG = string>(bytes: Uint8Array, longFactory?: ILongFactory<LONG>): any; | ||
function serializeOrder<LONG = string | number>(ord: any, longFactory?: ILongFactory<LONG>): Uint8Array; | ||
/** | ||
* This function cannot parse OrderV1, which doesn't have version field | ||
*/ | ||
function parseOrder<LONG = string>(bytes: Uint8Array, longFactory?: ILongFactory<LONG>): any; | ||
@@ -7,0 +13,0 @@ } |
@@ -26,2 +26,5 @@ "use strict"; | ||
binary.serializeTx = serializeTx; | ||
/** | ||
* This function cannot transactions without version | ||
*/ | ||
function parseTx(bytes, longFactory) { | ||
@@ -34,7 +37,12 @@ var _a = parse_1.parseHeader(bytes), type = _a.type, version = _a.version; | ||
function serializeOrder(ord, longFactory) { | ||
return serialize_1.serializerFromSchema(txSchemas_1.orderSchemaV0, longFactory)(ord); | ||
var version = ord.version; | ||
var schema = version == 2 ? txSchemas_1.orderSchemaV2 : txSchemas_1.orderSchemaV0; | ||
return serialize_1.serializerFromSchema(schema, longFactory)(ord); | ||
} | ||
binary.serializeOrder = serializeOrder; | ||
/** | ||
* This function cannot parse OrderV1, which doesn't have version field | ||
*/ | ||
function parseOrder(bytes, longFactory) { | ||
return parse_1.parserFromSchema(txSchemas_1.orderSchemaV0, longFactory)(bytes).value; | ||
return parse_1.parserFromSchema(txSchemas_1.orderSchemaV2, longFactory)(bytes).value; | ||
} | ||
@@ -41,0 +49,0 @@ binary.parseOrder = parseOrder; |
@@ -42,2 +42,8 @@ "use strict"; | ||
else if (schema.type === 'object') { | ||
if (schema.optional) { | ||
var exists = bytes[cursor] === 1; | ||
cursor += 1; | ||
if (!exists) | ||
return { value: undefined, shift: 1 }; | ||
} | ||
//we don't need object length to parse it since we have schema of all its fields | ||
@@ -44,0 +50,0 @@ if (schema.withLength) |
@@ -26,5 +26,5 @@ "use strict"; | ||
var txSchemas_1 = require("./txSchemas"); | ||
// FixMe: currently longfactory does nothing. Maybe we should remove ot Altogether | ||
// FixMe: currently longfactory does nothing. Maybe we should remove it altogether | ||
exports.serializerFromSchema = function (schema, lf) { return function (obj) { | ||
var result = Uint8Array.from([]); | ||
//let result = Uint8Array.from([]); | ||
var serializer, itemBytes; | ||
@@ -34,6 +34,15 @@ if (schema.type === 'array') { | ||
itemBytes = utils_1.concat.apply(void 0, __spread(obj.map(function (item) { return serializer((item)); }))); | ||
result = utils_1.concat(result, serializePrimitives_1.SHORT(obj.length), itemBytes); | ||
return utils_1.concat(serializePrimitives_1.SHORT(obj.length), itemBytes); | ||
} | ||
else if (schema.type === 'object') { | ||
var objBytes_1 = Uint8Array.from([]); | ||
if (schema.optional) { | ||
if (obj == null) { | ||
objBytes_1 = utils_1.concat(objBytes_1, [0]); | ||
return objBytes_1; | ||
} | ||
else { | ||
objBytes_1 = utils_1.concat(objBytes_1, [1]); | ||
} | ||
} | ||
schema.schema.forEach(function (field) { | ||
@@ -45,4 +54,4 @@ serializer = exports.serializerFromSchema(field, lf); | ||
if (schema.withLength) | ||
result = utils_1.concat(result, serializePrimitives_1.SHORT(objBytes_1.length)); | ||
result = utils_1.concat(result, objBytes_1); | ||
objBytes_1 = utils_1.concat(serializePrimitives_1.SHORT(objBytes_1.length), objBytes_1); | ||
return objBytes_1; | ||
} | ||
@@ -58,6 +67,6 @@ else if (schema.type === 'anyOf') { | ||
itemBytes = serializer(obj[schema.valueField || 'value']); | ||
result = utils_1.concat(result, serializePrimitives_1.BYTE(typeCode), itemBytes); | ||
return utils_1.concat(serializePrimitives_1.BYTE(typeCode), itemBytes); | ||
} | ||
else if (schema.type === 'primitive' || schema.type === undefined) { | ||
result = utils_1.concat(result, schema.toBytes(obj)); | ||
return schema.toBytes(obj); | ||
} | ||
@@ -74,3 +83,3 @@ else if (schema.type === 'dataTxField') { | ||
itemBytes = serializer(obj.value); | ||
result = utils_1.concat(result, keyBytes, serializePrimitives_1.BYTE(typeCode), itemBytes); | ||
return utils_1.concat(keyBytes, serializePrimitives_1.BYTE(typeCode), itemBytes); | ||
} | ||
@@ -80,4 +89,3 @@ else { | ||
} | ||
return result; | ||
}; }; | ||
//# sourceMappingURL=serialize.js.map |
@@ -27,12 +27,2 @@ "use strict"; | ||
var utils_1 = require("./libs/utils"); | ||
//ToDo: perfomance issue | ||
// function stringToUint8Array(str: string) { | ||
// const result: number[] = [] | ||
// const unescaped = unescape(encodeURIComponent(str)) | ||
// | ||
// for (let c of unescaped){ | ||
// result.push(c.charCodeAt(0)) | ||
// } | ||
// return Uint8Array.from(result) | ||
// } | ||
var stringToUint8Array = function (str) { | ||
@@ -79,6 +69,6 @@ return Uint8Array.from(__spread(unescape(encodeURIComponent(str))).map(function (c) { return c.charCodeAt(0); })); | ||
} | ||
l = Long.fromNumber(value, true); | ||
l = Long.fromNumber(value); | ||
} | ||
else { | ||
l = Long.fromString(value.toString(), true); | ||
l = Long.fromString(value.toString()); | ||
} | ||
@@ -85,0 +75,0 @@ return Uint8Array.from(l.toBytesBE()); |
@@ -30,2 +30,3 @@ export declare enum TRANSACTION_TYPE { | ||
withLength?: boolean; | ||
optional?: boolean; | ||
schema: TSchema[]; | ||
@@ -257,4 +258,6 @@ }; | ||
const functionCall: TObject; | ||
const payment: TObject; | ||
} | ||
export declare const orderSchemaV0: TObject; | ||
export declare const orderSchemaV2: TObject; | ||
/** | ||
@@ -369,2 +372,3 @@ * Maps transaction types to schemas object. Schemas are written by keys. 0 - no version, n - version n | ||
type: "object"; | ||
optional?: boolean | undefined; | ||
schema: TSchema[]; | ||
@@ -371,0 +375,0 @@ })[]; |
@@ -164,3 +164,3 @@ "use strict"; | ||
txFields.functionCall = { | ||
name: 'function', | ||
name: 'call', | ||
type: 'object', | ||
@@ -180,2 +180,11 @@ schema: [ | ||
}; | ||
txFields.payment = { | ||
name: 'payment', | ||
optional: true, | ||
type: 'object', | ||
schema: [ | ||
txFields.amount, | ||
txFields.optionalAssetId | ||
] | ||
}; | ||
})(txFields = exports.txFields || (exports.txFields = {})); | ||
@@ -213,2 +222,34 @@ exports.orderSchemaV0 = { | ||
}; | ||
exports.orderSchemaV2 = { | ||
name: 'orderSchemaV2', | ||
type: 'object', | ||
schema: [ | ||
txFields.version, | ||
txFields.senderPublicKey, | ||
__assign({}, txFields.senderPublicKey, { name: 'matcherPublicKey' }), | ||
{ | ||
name: 'assetPair', | ||
type: 'object', | ||
schema: [ | ||
txFields.base58Option32('amountAsset'), | ||
txFields.base58Option32('priceAsset') | ||
] | ||
}, | ||
{ | ||
name: 'orderType', | ||
toBytes: function (type) { return serializePrimitives_1.BYTE(type === 'sell' ? 1 : 0); }, | ||
fromBytes: function (bytes, start) { | ||
if (start === void 0) { start = 0; } | ||
return parsePrimitives_1.P_BYTE(bytes, start).value === 1 ? | ||
{ value: 'sell', shift: 1 } : | ||
{ value: 'buy', shift: 1 }; | ||
} | ||
}, | ||
txFields.longField('price'), | ||
txFields.longField('amount'), | ||
txFields.timestamp, | ||
txFields.longField('expiration'), | ||
txFields.longField('matcherFee') | ||
] | ||
}; | ||
var aliasSchemaV2 = { | ||
@@ -266,2 +307,3 @@ name: 'aliasSchemaV2', | ||
txFields.functionCall, | ||
txFields.payment, | ||
txFields.fee, | ||
@@ -268,0 +310,0 @@ txFields.timestamp, |
{ | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"name": "@waves/marshall", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -1,2 +0,2 @@ | ||
import {schemasByTypeMap, ILongFactory, orderSchemaV0,} from "./txSchemas"; | ||
import {schemasByTypeMap, ILongFactory, orderSchemaV0, orderSchemaV2,} from "./txSchemas"; | ||
import {serializerFromSchema} from "./serialize"; | ||
@@ -14,2 +14,5 @@ import {parseHeader, parserFromSchema} from "./parse"; | ||
/** | ||
* This function cannot transactions without version | ||
*/ | ||
export function parseTx<LONG = string>(bytes: Uint8Array, longFactory?: ILongFactory<LONG>) { | ||
@@ -23,7 +26,12 @@ const {type, version} = parseHeader(bytes); | ||
export function serializeOrder<LONG = string | number>(ord: any, longFactory?: ILongFactory<LONG>): Uint8Array { | ||
return serializerFromSchema(orderSchemaV0, longFactory)(ord); | ||
const { version } = ord; | ||
const schema = version == 2 ? orderSchemaV2 : orderSchemaV0; | ||
return serializerFromSchema(schema, longFactory)(ord); | ||
} | ||
/** | ||
* This function cannot parse OrderV1, which doesn't have version field | ||
*/ | ||
export function parseOrder<LONG = string>(bytes: Uint8Array, longFactory?: ILongFactory<LONG>) { | ||
return parserFromSchema(orderSchemaV0, longFactory)(bytes).value; | ||
return parserFromSchema(orderSchemaV2, longFactory)(bytes).value; | ||
} | ||
@@ -30,0 +38,0 @@ } |
@@ -23,2 +23,8 @@ import {byteToStringWithLength, P_BYTE, P_LONG, P_SHORT, TParser} from "./parsePrimitives"; | ||
else if (schema.type === 'object') { | ||
if(schema.optional){ | ||
const exists = bytes[cursor] === 1; | ||
cursor +=1; | ||
if (!exists) return {value: undefined, shift: 1} | ||
} | ||
//we don't need object length to parse it since we have schema of all its fields | ||
@@ -25,0 +31,0 @@ if(schema.withLength) cursor += 2; |
@@ -5,5 +5,5 @@ import {BYTE, LEN, SHORT, TSerializer} from "./serializePrimitives"; | ||
// FixMe: currently longfactory does nothing. Maybe we should remove ot Altogether | ||
// FixMe: currently longfactory does nothing. Maybe we should remove it altogether | ||
export const serializerFromSchema = <LONG = string | number>(schema: TSchema, lf?: ILongFactory<LONG>): TSerializer<any> => (obj: any) => { | ||
let result = Uint8Array.from([]); | ||
//let result = Uint8Array.from([]); | ||
@@ -16,6 +16,16 @@ let serializer: TSerializer<any>, | ||
itemBytes = concat(...obj.map((item: any) => serializer((item)))); | ||
result = concat(result, SHORT(obj.length), itemBytes); | ||
return concat(SHORT(obj.length), itemBytes); | ||
} | ||
else if (schema.type === 'object') { | ||
let objBytes = Uint8Array.from([]) | ||
let objBytes = Uint8Array.from([]); | ||
if (schema.optional){ | ||
if(obj == null){ | ||
objBytes = concat(objBytes,[0]); | ||
return objBytes | ||
}else { | ||
objBytes = concat(objBytes, [1]) | ||
} | ||
} | ||
schema.schema.forEach(field => { | ||
@@ -26,4 +36,4 @@ serializer = serializerFromSchema(field, lf); | ||
}); | ||
if (schema.withLength) result = concat(result, SHORT(objBytes.length)) | ||
result = concat(result, objBytes) | ||
if (schema.withLength) objBytes = concat(SHORT(objBytes.length), objBytes) | ||
return objBytes | ||
} | ||
@@ -39,6 +49,6 @@ else if (schema.type === 'anyOf') { | ||
itemBytes = serializer(obj[schema.valueField || 'value']); | ||
result = concat(result, BYTE(typeCode), itemBytes); | ||
return concat(BYTE(typeCode), itemBytes); | ||
} | ||
else if (schema.type === 'primitive' || schema.type === undefined) { | ||
result = concat(result, schema.toBytes(obj)); | ||
return schema.toBytes(obj); | ||
} | ||
@@ -55,3 +65,3 @@ else if (schema.type === 'dataTxField') { | ||
itemBytes = serializer(obj.value); | ||
result = concat(result, keyBytes, BYTE(typeCode), itemBytes) | ||
return concat(keyBytes, BYTE(typeCode), itemBytes) | ||
} else { | ||
@@ -61,3 +71,2 @@ throw new Error(`Serializer Error: Unknown schema type: ${schema!.type}`) | ||
return result | ||
}; |
@@ -6,12 +6,2 @@ import base58 from "./libs/base58"; | ||
//ToDo: perfomance issue | ||
// function stringToUint8Array(str: string) { | ||
// const result: number[] = [] | ||
// const unescaped = unescape(encodeURIComponent(str)) | ||
// | ||
// for (let c of unescaped){ | ||
// result.push(c.charCodeAt(0)) | ||
// } | ||
// return Uint8Array.from(result) | ||
// } | ||
const stringToUint8Array = (str: string) => | ||
@@ -73,5 +63,5 @@ Uint8Array.from([...unescape(encodeURIComponent(str))].map(c => c.charCodeAt(0))); | ||
} | ||
l = Long.fromNumber(value, true) | ||
l = Long.fromNumber(value) | ||
} else { | ||
l = Long.fromString(value.toString(), true) | ||
l = Long.fromString(value.toString()) | ||
} | ||
@@ -78,0 +68,0 @@ return Uint8Array.from(l.toBytesBE()) |
@@ -51,2 +51,3 @@ import { | ||
withLength?: boolean; | ||
optional?: boolean; | ||
schema: TSchema[]; | ||
@@ -230,3 +231,3 @@ } | ||
export const functionCall: TObject = { | ||
name: 'function', | ||
name: 'call', | ||
type: 'object', | ||
@@ -246,2 +247,12 @@ schema: [ | ||
} | ||
export const payment: TObject = { | ||
name: 'payment', | ||
optional: true, | ||
type: 'object', | ||
schema: [ | ||
amount, | ||
optionalAssetId | ||
] | ||
} | ||
} | ||
@@ -278,2 +289,32 @@ | ||
export const orderSchemaV2: TObject = { | ||
name: 'orderSchemaV2', | ||
type: 'object', | ||
schema: [ | ||
txFields.version, | ||
txFields.senderPublicKey, | ||
{...txFields.senderPublicKey, name: 'matcherPublicKey'}, | ||
{ | ||
name: 'assetPair', | ||
type: 'object', | ||
schema: [ | ||
txFields.base58Option32('amountAsset'), | ||
txFields.base58Option32('priceAsset') | ||
] | ||
}, | ||
{ | ||
name: 'orderType', | ||
toBytes: (type: string) => BYTE(type === 'sell' ? 1 : 0), | ||
fromBytes: (bytes: Uint8Array, start = 0) => P_BYTE(bytes, start).value === 1 ? | ||
{value: 'sell', shift: 1} : | ||
{value: 'buy', shift: 1} | ||
}, | ||
txFields.longField('price'), | ||
txFields.longField('amount'), | ||
txFields.timestamp, | ||
txFields.longField('expiration'), | ||
txFields.longField('matcherFee') | ||
] | ||
}; | ||
const aliasSchemaV2 = { | ||
@@ -334,2 +375,3 @@ name: 'aliasSchemaV2', | ||
txFields.functionCall, | ||
txFields.payment, | ||
txFields.fee, | ||
@@ -336,0 +378,0 @@ txFields.timestamp, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
398994
3442