Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/support_protobuf_txs #73

Open
wants to merge 7 commits into
base: stagenet
Choose a base branch
from
Open
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
295 changes: 203 additions & 92 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@waves/signature-adapter",
"version": "5.12.0",
"version": "5.12.0-beta.2",
"types": "dist/index.d.ts",
"main": "dist/index.js",
"license": "MIT",
Expand All @@ -14,15 +14,15 @@
"@waves/data-entities": "^2.0.3",
"@waves/ledger": "^3.6.1",
"@waves/money-like-to-node": "0.0.10",
"@waves/ts-types": "^0.2.0",
"@waves/waves-transactions": "^3.25.1",
"@waves/ts-types": "^0.3.2",
"@waves/waves-transactions": "^4.0.0-beta.19",
"ramda": "^0.25.0"
},
"scripts": {
"test": "jest",
"compile": "tsc",
"browserify": "node_modules/.bin/browserify dist/index.js -s wavesSignatureAdapter -u @waves/waves-crypto -u @waves/bignumber -u @waves/waves-transactions -u @waves/data-entities -u @waves/marshall -u @waves/ledger -o dist/signature-adapter.js",
"build": "npm run compile && npm run browserify && npm run uglify",
"build": "npm run compile && npm run browserify",
"uglify": "node_modules/.bin/uglifyjs dist/signature-adapter.js -o dist/signature-adapter.min.js",
"postpublish": "git push origin HEAD",
"postversion": "npm publish",
Expand Down
2 changes: 1 addition & 1 deletion src/Signable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class Signable {
}

const version = this._forSign.data.version;

if (!availableVersions.includes(version)) {
throw new SignError(`Can\'t sign data with type "${this.type}" and version "${version}"`, ERRORS.VERSION_IS_NOT_SUPPORTED);
}
Expand Down
46 changes: 23 additions & 23 deletions src/adapters/LedgerAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Adapter } from './Adapter';
import { AdapterType } from '../config';
import { WavesLedger } from '@waves/ledger';
import { SIGN_TYPE } from '../prepareTx';

import { SIGN_TYPE } from '../prepareTx/constants';

export class LedgerAdapter extends Adapter {

Expand Down Expand Up @@ -85,29 +84,30 @@ export class LedgerAdapter extends Adapter {
public getPrivateKey() {
return Promise.reject('No private key');
}

public getSignVersions(): Record<SIGN_TYPE, Array<number>> {
return {
[SIGN_TYPE.AUTH]: [1],
[SIGN_TYPE.MATCHER_ORDERS]: [1],
[SIGN_TYPE.WAVES_CONFIRMATION]: [1],
[SIGN_TYPE.CREATE_ORDER]: [1, 2, 3],
[SIGN_TYPE.CANCEL_ORDER]: [1],
[SIGN_TYPE.COINOMAT_CONFIRMATION]: [1],
[SIGN_TYPE.ISSUE]: [2],
[SIGN_TYPE.TRANSFER]: [2],
[SIGN_TYPE.REISSUE]: [2],
[SIGN_TYPE.BURN]: [2],
[SIGN_TYPE.EXCHANGE]: [0,1,2],
[SIGN_TYPE.LEASE]: [2],
[SIGN_TYPE.CANCEL_LEASING]: [2],
[SIGN_TYPE.CREATE_ALIAS]: [2],
[SIGN_TYPE.MASS_TRANSFER]: [1],
[SIGN_TYPE.DATA]: [1],
[SIGN_TYPE.SET_SCRIPT]: [1],
[SIGN_TYPE.SPONSORSHIP]: [1],
[SIGN_TYPE.SET_ASSET_SCRIPT]: [1],
[SIGN_TYPE.SCRIPT_INVOCATION]: [1]
[SIGN_TYPE.AUTH]: [1],
[SIGN_TYPE.MATCHER_ORDERS]: [1],
[SIGN_TYPE.WAVES_CONFIRMATION]: [1],
[SIGN_TYPE.CREATE_ORDER]: [1, 2, 3],
[SIGN_TYPE.CANCEL_ORDER]: [1],
[SIGN_TYPE.COINOMAT_CONFIRMATION]: [1],
[SIGN_TYPE.ISSUE]: [2],
[SIGN_TYPE.TRANSFER]: [2],
[SIGN_TYPE.REISSUE]: [2],
[SIGN_TYPE.BURN]: [2],
[SIGN_TYPE.EXCHANGE]: [0,1,2],
[SIGN_TYPE.LEASE]: [2],
[SIGN_TYPE.CANCEL_LEASING]: [2],
[SIGN_TYPE.CREATE_ALIAS]: [2],
[SIGN_TYPE.MASS_TRANSFER]: [1],
[SIGN_TYPE.DATA]: [1],
[SIGN_TYPE.SET_SCRIPT]: [1],
[SIGN_TYPE.SPONSORSHIP]: [1],
[SIGN_TYPE.SET_ASSET_SCRIPT]: [1],
[SIGN_TYPE.SCRIPT_INVOCATION]: [1],
[SIGN_TYPE.UPDATE_ASSET_INFO]: []
};
}

Expand Down
31 changes: 16 additions & 15 deletions src/adapters/PrivateKeyAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,25 @@ export class PrivateKeyAdapter extends Adapter {
return {
[SIGN_TYPE.AUTH]: [1],
[SIGN_TYPE.MATCHER_ORDERS]: [1],
[SIGN_TYPE.WAVES_CONFIRMATION]: [1],
[SIGN_TYPE.CREATE_ORDER]: [1, 2, 3],
[SIGN_TYPE.CANCEL_ORDER]: [0, 1],
[SIGN_TYPE.COINOMAT_CONFIRMATION]: [1],
[SIGN_TYPE.ISSUE]: [2],
[SIGN_TYPE.TRANSFER]: [2],
[SIGN_TYPE.REISSUE]: [2],
[SIGN_TYPE.BURN]: [2],
[SIGN_TYPE.EXCHANGE]: [0, 1, 2],
[SIGN_TYPE.LEASE]: [2],
[SIGN_TYPE.CANCEL_LEASING]: [2],
[SIGN_TYPE.CREATE_ALIAS]: [2],
[SIGN_TYPE.MASS_TRANSFER]: [1],
[SIGN_TYPE.DATA]: [1],
[SIGN_TYPE.SET_SCRIPT]: [1],
[SIGN_TYPE.SPONSORSHIP]: [1],
[SIGN_TYPE.SET_ASSET_SCRIPT]: [1],
[SIGN_TYPE.SCRIPT_INVOCATION]: [1],
[SIGN_TYPE.WAVES_CONFIRMATION]: [1],
[SIGN_TYPE.TRANSFER]: [2, 3],
[SIGN_TYPE.ISSUE]: [2, 3],
[SIGN_TYPE.REISSUE]: [2, 3],
[SIGN_TYPE.BURN]: [2, 3],
[SIGN_TYPE.EXCHANGE]: [0, 1, 2, 3],
[SIGN_TYPE.LEASE]: [2, 3],
[SIGN_TYPE.CANCEL_LEASING]: [2, 3],
[SIGN_TYPE.CREATE_ALIAS]: [2, 3],
[SIGN_TYPE.MASS_TRANSFER]: [1, 2],
[SIGN_TYPE.DATA]: [1, 2],
[SIGN_TYPE.SET_SCRIPT]: [1, 2],
[SIGN_TYPE.SPONSORSHIP]: [1, 2],
[SIGN_TYPE.SET_ASSET_SCRIPT]: [1, 2],
[SIGN_TYPE.SCRIPT_INVOCATION]: [1, 2],
[SIGN_TYPE.UPDATE_ASSET_INFO]: []
};
}

Expand Down
29 changes: 15 additions & 14 deletions src/adapters/SeedAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,21 @@ export class SeedAdapter extends Adapter {
[SIGN_TYPE.CANCEL_ORDER]: [0, 1],
[SIGN_TYPE.COINOMAT_CONFIRMATION]: [1],
[SIGN_TYPE.WAVES_CONFIRMATION]: [1],
[SIGN_TYPE.ISSUE]: [2],
[SIGN_TYPE.TRANSFER]: [2],
[SIGN_TYPE.REISSUE]: [2],
[SIGN_TYPE.BURN]: [2],
[SIGN_TYPE.EXCHANGE]: [0, 1, 2],
[SIGN_TYPE.LEASE]: [2],
[SIGN_TYPE.CANCEL_LEASING]: [2],
[SIGN_TYPE.CREATE_ALIAS]: [2],
[SIGN_TYPE.MASS_TRANSFER]: [1],
[SIGN_TYPE.DATA]: [1],
[SIGN_TYPE.SET_SCRIPT]: [1],
[SIGN_TYPE.SPONSORSHIP]: [1],
[SIGN_TYPE.SET_ASSET_SCRIPT]: [1],
[SIGN_TYPE.SCRIPT_INVOCATION]: [1],
[SIGN_TYPE.TRANSFER]: [2, 3],
[SIGN_TYPE.ISSUE]: [2, 3],
[SIGN_TYPE.REISSUE]: [2, 3],
[SIGN_TYPE.BURN]: [2, 3],
[SIGN_TYPE.EXCHANGE]: [0, 1, 2, 3],
[SIGN_TYPE.LEASE]: [2, 3],
[SIGN_TYPE.CANCEL_LEASING]: [2, 3],
[SIGN_TYPE.CREATE_ALIAS]: [2, 3],
[SIGN_TYPE.MASS_TRANSFER]: [1, 2],
[SIGN_TYPE.DATA]: [1, 2],
[SIGN_TYPE.SET_SCRIPT]: [1, 2],
[SIGN_TYPE.SPONSORSHIP]: [1, 2],
[SIGN_TYPE.SET_ASSET_SCRIPT]: [1, 2],
[SIGN_TYPE.SCRIPT_INVOCATION]: [1, 2],
[SIGN_TYPE.UPDATE_ASSET_INFO]: []
};
}

Expand Down
43 changes: 22 additions & 21 deletions src/adapters/WavesKeeperAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@ import { SIGN_TYPE, TSignData } from '../prepareTx';
import { isValidAddress } from '../prepareTx/fieldValidator';
import equals = require('ramda/src/equals');

const DEFAULT_TX_VERSIONS = {
[SIGN_TYPE.AUTH]: [1],
[SIGN_TYPE.MATCHER_ORDERS]: [1],
[SIGN_TYPE.CREATE_ORDER]: [1, 2, 3],
[SIGN_TYPE.CANCEL_ORDER]: [1],
[SIGN_TYPE.COINOMAT_CONFIRMATION]: [1],
[SIGN_TYPE.WAVES_CONFIRMATION]: [1],
[SIGN_TYPE.ISSUE]: [2],
[SIGN_TYPE.TRANSFER]: [2],
[SIGN_TYPE.REISSUE]: [2],
[SIGN_TYPE.BURN]: [2],
[SIGN_TYPE.EXCHANGE]: [0,1,2],
[SIGN_TYPE.LEASE]: [2],
[SIGN_TYPE.CANCEL_LEASING]: [2],
[SIGN_TYPE.CREATE_ALIAS]: [2],
[SIGN_TYPE.MASS_TRANSFER]: [1],
[SIGN_TYPE.DATA]: [1],
[SIGN_TYPE.SET_SCRIPT]: [1],
[SIGN_TYPE.SPONSORSHIP]: [1],
[SIGN_TYPE.SET_ASSET_SCRIPT]: [1],
[SIGN_TYPE.SCRIPT_INVOCATION]: [1]
const DEFAULT_TX_VERSIONS = {
[SIGN_TYPE.AUTH]: [1],
[SIGN_TYPE.MATCHER_ORDERS]: [1],
[SIGN_TYPE.CREATE_ORDER]: [1, 2, 3],
[SIGN_TYPE.CANCEL_ORDER]: [1],
[SIGN_TYPE.COINOMAT_CONFIRMATION]: [1],
[SIGN_TYPE.WAVES_CONFIRMATION]: [1],
[SIGN_TYPE.TRANSFER]: [2, 3],
[SIGN_TYPE.ISSUE]: [2, 3],
[SIGN_TYPE.REISSUE]: [2, 3],
[SIGN_TYPE.BURN]: [2, 3],
[SIGN_TYPE.EXCHANGE]: [0, 1, 2, 3],
[SIGN_TYPE.LEASE]: [2, 3],
[SIGN_TYPE.CANCEL_LEASING]: [2, 3],
[SIGN_TYPE.CREATE_ALIAS]: [2, 3],
[SIGN_TYPE.MASS_TRANSFER]: [1, 2],
[SIGN_TYPE.DATA]: [1, 2],
[SIGN_TYPE.SET_SCRIPT]: [1, 2],
[SIGN_TYPE.SPONSORSHIP]: [1, 2],
[SIGN_TYPE.SET_ASSET_SCRIPT]: [1, 2],
[SIGN_TYPE.SCRIPT_INVOCATION]: [1, 2],
[SIGN_TYPE.UPDATE_ASSET_INFO]: []
};

export class WavesKeeperAdapter extends Adapter {
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SeedAdapter } from './adapters/SeedAdapter';
import { LedgerAdapter } from './adapters/LedgerAdapter';
import { WavesKeeperAdapter } from './adapters';
import {PrivateKeyAdapter} from "./adapters/PrivateKeyAdapter";
import { SIGN_TYPE } from './prepareTx';

export const enum AdapterType {
Seed = 'seed',
Expand All @@ -25,3 +26,4 @@ export const adapterList = [
WavesKeeperAdapter,
PrivateKeyAdapter
];

19 changes: 17 additions & 2 deletions src/prepareTx/constants.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { IAdapterSignMethods } from './interfaces';
import { libs } from '@waves/waves-transactions';
import { libs, protoPerialize } from '@waves/waves-transactions';
import * as wavesTransactions from '@waves/waves-transactions';
import { toNode as mlToNode } from '@waves/money-like-to-node';
import { Money } from '@waves/data-entities';
import { prepare } from './prepare';
import processors = prepare.processors;
import { Money } from '@waves/data-entities';

const { LEN, SHORT, STRING, LONG, BASE58_STRING } = libs.marshall.serializePrimitives;
const { binary } = libs.marshall;
const { txToProtoBytes } = protoPerialize

const toNode = (data: any, convert?: Function) => {
const r = mlToNode(data);
Expand Down Expand Up @@ -172,6 +173,7 @@ export const SIGN_TYPES: Record<SIGN_TYPE, ITypesMap> = {
[SIGN_TYPE.TRANSFER]: {
getBytes: {
2: binary.serializeTx,
3: txToProtoBytes
},
toNode: (data, networkByte: number) => (toNode({
...data,
Expand All @@ -183,6 +185,7 @@ export const SIGN_TYPES: Record<SIGN_TYPE, ITypesMap> = {
[SIGN_TYPE.ISSUE]: {
getBytes: {
2: binary.serializeTx,
3: txToProtoBytes
},
toNode: data => toNode(
{
Expand All @@ -197,6 +200,7 @@ export const SIGN_TYPES: Record<SIGN_TYPE, ITypesMap> = {
[SIGN_TYPE.REISSUE]: {
getBytes: {
2: binary.serializeTx,
3: txToProtoBytes
},
toNode: data => {
const quantity = data.amount || data.quantity;
Expand All @@ -207,6 +211,7 @@ export const SIGN_TYPES: Record<SIGN_TYPE, ITypesMap> = {
[SIGN_TYPE.BURN]: {
getBytes: {
2: binary.serializeTx,
3: txToProtoBytes
},
toNode: data => {
const quantity = data.amount || data.quantity;
Expand All @@ -219,6 +224,7 @@ export const SIGN_TYPES: Record<SIGN_TYPE, ITypesMap> = {
0: (data) => binary.serializeTx({ ...data, version: 1 }),
1: binary.serializeTx,
2: binary.serializeTx,
3: txToProtoBytes
},
toNode: data => {
const tx = toNode(data);
Expand All @@ -235,6 +241,7 @@ export const SIGN_TYPES: Record<SIGN_TYPE, ITypesMap> = {
[SIGN_TYPE.LEASE]: {
getBytes: {
2: binary.serializeTx,
3: txToProtoBytes
},
toNode: (data, networkByte: number) => (toNode({
...data,
Expand All @@ -245,13 +252,15 @@ export const SIGN_TYPES: Record<SIGN_TYPE, ITypesMap> = {
[SIGN_TYPE.CANCEL_LEASING]: {
getBytes: {
2: binary.serializeTx,
3: txToProtoBytes
},
toNode: data => toNode(data, wavesTransactions.cancelLease),
adapter: 'signTransaction'
},
[SIGN_TYPE.CREATE_ALIAS]: {
getBytes: {
2: binary.serializeTx,
3: txToProtoBytes
},
toNode: data => ({ ...toNode(data, wavesTransactions.alias), chainId: data.chainId }),
adapter: 'signTransaction'
Expand All @@ -260,6 +269,7 @@ export const SIGN_TYPES: Record<SIGN_TYPE, ITypesMap> = {
getBytes: {
0: binary.serializeTx,
1: binary.serializeTx,
2: txToProtoBytes
},
toNode: (data, networkByte: number) => (toNode({
...data,
Expand All @@ -276,6 +286,7 @@ export const SIGN_TYPES: Record<SIGN_TYPE, ITypesMap> = {
getBytes: {
0: binary.serializeTx,
1: binary.serializeTx,
2: txToProtoBytes
},
toNode: data => toNode(data, wavesTransactions.data),
adapter: 'signTransaction'
Expand All @@ -284,6 +295,7 @@ export const SIGN_TYPES: Record<SIGN_TYPE, ITypesMap> = {
getBytes: {
0: binary.serializeTx,
1: binary.serializeTx,
2: txToProtoBytes
},
toNode: data => toNode(
{
Expand All @@ -298,6 +310,7 @@ export const SIGN_TYPES: Record<SIGN_TYPE, ITypesMap> = {
getBytes: {
0: binary.serializeTx,
1: binary.serializeTx,
2: txToProtoBytes
},
toNode: data => toNode(data, wavesTransactions.sponsorship),
adapter: 'signTransaction'
Expand All @@ -306,6 +319,7 @@ export const SIGN_TYPES: Record<SIGN_TYPE, ITypesMap> = {
getBytes: {
0: binary.serializeTx,
1: binary.serializeTx,
2: txToProtoBytes
},
toNode: data => toNode({
...data,
Expand All @@ -319,6 +333,7 @@ export const SIGN_TYPES: Record<SIGN_TYPE, ITypesMap> = {
getBytes: {
0: binary.serializeTx,
1: binary.serializeTx,
2: txToProtoBytes
},
toNode: (data, networkByte: number) => (toNode({
...data,
Expand Down