Skip to content

Commit

Permalink
Merge pull request #95 from wavesplatform/catch-serializer-error
Browse files Browse the repository at this point in the history
catch serializer error
  • Loading branch information
OlyaKanishcheva authored Oct 1, 2021
2 parents be12a7a + 93c1ae4 commit 77eee4a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 79 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@waves/signature-adapter",
"version": "6.1.1",
"version": "6.1.3",
"types": "dist/index.d.ts",
"main": "dist/index.js",
"license": "MIT",
Expand Down
90 changes: 47 additions & 43 deletions src/Signable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { TRANSACTION_TYPE_NUMBER } from './prepareTx';
const { base58Encode, blake2b, verifySignature } = libs.crypto;

export class Signable {

public readonly type: SIGN_TYPE;
private readonly _forSign: TSignData;
private readonly _adapter: Adapter;
Expand All @@ -28,75 +28,79 @@ export class Signable {
private _signPromise: Promise<string> | undefined;
private _preparedData: any;
private _proofs: Array<string> = [];


constructor(forSign: TSignData, adapter: Adapter) {
const networkCode = adapter.getNetworkByte();
this._forSign = { ...forSign };
this.type = forSign.type;
this._adapter = adapter;
const prepareMap = getValidateSchema(networkCode)[forSign.type];

if (!prepareMap) {
throw new SignError(`Can't find prepare api for tx type "${forSign.type}"!`, ERRORS.UNKNOWN_SIGN_TYPE);
}

this._forSign.data.timestamp = new Date(this._forSign.data.timestamp || Date.now()).getTime();

if (this._forSign.data.proofs) {
this._proofs = this._forSign.data.proofs.slice();
}

const availableVersions = adapter.getSignVersions()[forSign.type];

if (availableVersions.length === 0) {
throw new SignError(`Can\'t sign data with type ${this.type}`, ERRORS.NO_SUPPORTED_VERSIONS);
}

if (isEmpty(this._forSign.data.version)) {
this._forSign.data.version = last(availableVersions);
}

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);
}

if (!SIGN_TYPES[forSign.type as SIGN_TYPE].getBytes[version]) {
throw new SignError(`Can't find prepare api for tx type "${forSign.type}" with version ${version}!`, ERRORS.VERSION_IS_NOT_SUPPORTED);
}

this._signMethod = SIGN_TYPES[forSign.type].adapter;

try {
this._preparedData = prepare.signSchema(prepareMap)(this._forSign.data, true);
} catch (e) {
throw new SignError(e.message, ERRORS.VALIDATION_FAILED);
}
this._bytePromise = this.getSignData()

const bytePromise = this.getSignData()
.then(signData => SIGN_TYPES[forSign.type].getBytes[version](signData));

bytePromise.catch(() => null);

this._bytePromise = bytePromise;
}

public async getOrderFee(config: IFeeConfig, minOrderFee: BigNumber, hasMatcherScript: boolean, smartAssetIdList?: Array<string>) {
if (this._forSign.type === SIGN_TYPE.CREATE_ORDER) {
const currentFee = currentCreateOrderFactory(config, minOrderFee);
return currentFee(await this.getDataForApi(), hasMatcherScript, smartAssetIdList)
}
}

public async getFee(config: IFeeConfig, hasScript: boolean, smartAssetIdList?: Array<string>) {
const currentFee = currentFeeFactory(config);
const txData = await this.getSignData();
const bytes = await this.getBytes();
return currentFee(txData, bytes, hasScript, smartAssetIdList);
}

public getTxData(): TSignData['data'] {
return { ...this._forSign.data };
}

public async getSignData() {
const senderPublicKey = await this._adapter.getPublicKey();
const sender = await this._adapter.getAddress();
Expand All @@ -112,13 +116,13 @@ export class Signable {

return signData || dataForBytes;
}

public async getAssetIds(): Promise<Array<string>> {
const transaction = await this.getSignData();
const hash = Object.create(null);
hash[WAVES_ID] = true;
hash[normalizeAssetId(transaction.feeAssetId)] = true;

switch (transaction.type) {
case SIGN_TYPE.CREATE_ORDER:
hash[normalizeAssetId(transaction.matcherFeeAssetId)] = true;
Expand Down Expand Up @@ -146,10 +150,10 @@ export class Signable {
}
return Object.keys(hash);
}

public sign2fa(options: ISign2faOptions): Promise<Signable> {
const code = options.code;

return this._adapter.getAddress()
.then(address => {
return options.request({
Expand All @@ -160,49 +164,49 @@ export class Signable {
})
.then(signature => {
this._proofs.push(signature);

return this;
});
}

public addProof(signature: string): Signable {
if (this._proofs.indexOf(signature) !== -1) {
this._proofs.push(signature);
}

return this;
}

public getHash() {
return this._bytePromise.then(bytes => base58Encode(blake2b(bytes)));
}

public getId(): Promise<string> {
return this._bytePromise.then(bytes => {
const byteArr = Array.from(bytes);

if (bytes[0] === 10) {
bytes = new Uint8Array([byteArr[0], ...byteArr.slice(36, -16)])
}

return base58Encode(blake2b(bytes))
});
}

public sign(): Promise<Signable> {
this._makeSignPromise();
return (this._signPromise as Promise<string>).then(() => this);
}

public getSignature(): Promise<string> {
this._makeSignPromise();
return (this._signPromise as Promise<string>);
}

public getBytes() {
return this._bytePromise;
}

public getMyProofs(): Promise<Array<string>> {
return Promise.all([
this.getBytes(),
Expand All @@ -217,11 +221,11 @@ export class Signable {
});
});
}

public hasMySignature(): Promise<boolean> {
return this.getMyProofs().then(proofs => !!proofs.length);
}

public addMyProof(): Promise<string> {
return this.hasMySignature().then(hasMySignature => {
if (!hasMySignature) {
Expand All @@ -234,21 +238,21 @@ export class Signable {
}
});
}

public async getDataForApi(needSign = true) {
const data = await this.getSignData();
if (needSign) {
await this.addMyProof();
}
const proofs = (this._proofs || []).slice();

try {
return convert({ ...data, proofs }, (item) => new BigNumber(item as string));
} catch (e) {
return { ...data, proofs, signature: proofs[0] };
}
}

private _makeSignPromise(): Signable {
if (!this._signPromise) {
this._signPromise = this._bytePromise.then(bytes => {
Expand All @@ -265,14 +269,14 @@ export class Signable {
this._forSign
);
});

this._signPromise.catch(() => {
this._signPromise = undefined;
});
}
return this;
}

private _getAmountPrecision() {
const data = this._forSign.data as any;
if (data.type === TRANSACTION_TYPE_NUMBER.SCRIPT_INVOCATION) {
Expand All @@ -292,7 +296,7 @@ export class Signable {
const data = this._forSign.data as any;
return data.fee && data.fee.asset && data.fee.asset.precision ? data.fee.asset.precision : 0;
}

}

export interface ISign2faOptions {
Expand Down
Loading

0 comments on commit 77eee4a

Please sign in to comment.