From 9e82a8793a3972ab7435171fa6d56185da884e60 Mon Sep 17 00:00:00 2001 From: MickeyWang <1244134672@qq.com> Date: Thu, 2 Aug 2018 21:56:46 +0800 Subject: [PATCH] fix identity keystore saltbase64;add pk compare in multisign --- src/identity.ts | 4 +-- src/sdk/index.ts | 44 +++++++++++++++++++++++---- src/transaction/transactionBuilder.ts | 20 +++++++++++- 3 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/identity.ts b/src/identity.ts index abba1aa3..0297967d 100644 --- a/src/identity.ts +++ b/src/identity.ts @@ -38,7 +38,7 @@ export class ControlData { */ encryptedKey: PrivateKey; /** - * Addresso of control data + * Address of control data */ address: Address; /** @@ -97,7 +97,7 @@ export class Identity { identity.lock = false; // control - const control = new ControlData('1', encryptedPrivateKey, Address.fromOntid(identity.ontid), salt); + const control = new ControlData('1', encryptedPrivateKey, Address.fromOntid(identity.ontid), saltBase64); identity.controls.push(control); diff --git a/src/sdk/index.ts b/src/sdk/index.ts index f528bbc2..76b34f27 100644 --- a/src/sdk/index.ts +++ b/src/sdk/index.ts @@ -48,7 +48,8 @@ import { Transaction } from '../transaction/transaction'; import { buildRestfulParam, sendRawTxRestfulUrl, - signTransaction + signTransaction, + signTx } from '../transaction/transactionBuilder'; import { generateMnemonic, hexstr2str, isBase64, now, reverseHex, sendBackResult2Native, str2hexstr, StringReader } from '../utils'; @@ -64,7 +65,7 @@ const HDKey = require('@ont-community/hdkey-secp256r1'); // neo contract const CONTRACT_HASH = 'ceab719b8baa2310f232ee0d277c061704541cfb'; // neo node -const NEO_NODE = 'http://52.224.162.48:10332'; +const NEO_NODE = 'http://neonode1.ont.network'; // neo abi // tslint:disable-next-line:max-line-length const NEP5_ABI = '{"hash":"0x5bb169f915c916a5e30a3c13a5e0cd228ea26826","entrypoint":"Main","functions":[{"name":"Name","parameters":[],"returntype":"String"},{"name":"Symbol","parameters":[],"returntype":"String"},{"name":"Decimals","parameters":[],"returntype":"Integer"},{"name":"Main","parameters":[{"name":"operation","type":"String"},{"name":"args","type":"Array"}],"returntype":"Any"},{"name":"Init","parameters":[],"returntype":"Boolean"},{"name":"TotalSupply","parameters":[],"returntype":"Integer"},{"name":"Transfer","parameters":[{"name":"from","type":"ByteArray"},{"name":"to","type":"ByteArray"},{"name":"value","type":"Integer"}],"returntype":"Boolean"},{"name":"BalanceOf","parameters":[{"name":"address","type":"ByteArray"}],"returntype":"Integer"}],"events":[{"name":"transfer","parameters":[{"name":"arg1","type":"ByteArray"},{"name":"arg2","type":"ByteArray"},{"name":"arg3","type":"Integer"}],"returntype":"Void"}]}'; @@ -1125,7 +1126,12 @@ export class SDK { if (M < 2 || pks.length < M || pks.length > 12) { error = ERROR_CODE.INVALID_PARAMS; } - const address = Address.fromMultiPubKeys(M, pubs).toBase58(); + let address = ''; + try { + address = Address.fromMultiPubKeys(M, pubs).toBase58(); + } catch (err) { + error = ERROR_CODE.INVALID_PARAMS; + } if (callback) { const result = { error, @@ -1183,6 +1189,7 @@ export class SDK { address: string, salt: string, password: string, + allRelatedPks: string, requiredSignatureNum: string, txDada: string, callback: string) { @@ -1200,12 +1207,14 @@ export class SDK { } return result; } + const M = parseInt(requiredSignatureNum, 10); const tx = Transaction.deserialize(txDada); - signTransaction(tx, privateKey); - const sigData = tx.sigs[0].sigData[0]; + const pubs = JSON.parse(allRelatedPks); + const pks = pubs.map((p: string) => new PublicKey(p)); + signTx(tx, M, pks, privateKey); const result = { error: ERROR_CODE.SUCCESS, - sigData + signedHash: tx.serialize() }; callback && sendBackResult2Native(JSON.stringify(result), callback); return tx; @@ -1285,4 +1294,27 @@ export class SDK { }); } + static sendTransaction(txData: string, callback: string) { + const restClient = new RestClient(`http://${SDK.SERVER_NODE}:${SDK.REST_PORT}`); + return restClient.sendRawTransaction(txData).then((res) => { + const obj = { + error: ERROR_CODE.SUCCESS, + result: res + }; + if (callback) { + sendBackResult2Native(JSON.stringify(obj), callback); + } + return obj; + }).catch((err) => { + const result = { + error: err.Error, + result: '' + }; + if (callback) { + sendBackResult2Native(JSON.stringify(result), callback); + } + return result; + }); + } + } diff --git a/src/transaction/transactionBuilder.ts b/src/transaction/transactionBuilder.ts index 8d65aeb0..7f6b27c3 100644 --- a/src/transaction/transactionBuilder.ts +++ b/src/transaction/transactionBuilder.ts @@ -28,6 +28,7 @@ import { import opcode from './opcode'; import DeployCode from './payload/deployCode'; import InvokeCode from './payload/invokeCode'; +import { comparePublicKeys } from './program'; import { buildSmartContractParam, pushHexString, pushInt } from './scriptBuilder'; import { Transaction, TxType } from './transaction'; import { TxSignature } from './txSignature'; @@ -94,6 +95,23 @@ export const addSign = (tx: Transaction, privateKey: PrivateKey, schema?: Signat tx.sigs.push(signature); }; +const equalPks = (pks1: PublicKey[], pks2: PublicKey[]): boolean => { + if (pks1 === pks2) { + return true; + } + pks1.sort(comparePublicKeys); + pks2.sort(comparePublicKeys); + if (pks1.length !== pks2.length) { + return false; + } + for (let i = 0; i < pks1.length ; i++) { + if (pks1[i].key !== pks2[i].key) { + return false; + } + } + return true; +}; + /** * Signs the transaction with multiple signatures with multi-sign keys. * @@ -117,7 +135,7 @@ export const signTx = (tx: Transaction, M: number, pubKeys: PublicKey[], } // tslint:disable-next-line:prefer-for-of for (let i = 0; i < tx.sigs.length; i++) { - if (tx.sigs[i].pubKeys === pubKeys) { + if (equalPks(tx.sigs[i].pubKeys, pubKeys)) { if (tx.sigs[i].sigData.length + 1 > pubKeys.length) { throw new Error('Too many sigData'); }