Skip to content

Commit

Permalink
Merge pull request #79 from MickWang/master
Browse files Browse the repository at this point in the history
fix identity keystore saltbase64;add pk compare in multisign
  • Loading branch information
MickWang authored Aug 2, 2018
2 parents 98ca8c7 + 9e82a87 commit 65b6f04
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class ControlData {
*/
encryptedKey: PrivateKey;
/**
* Addresso of control data
* Address of control data
*/
address: Address;
/**
Expand Down Expand Up @@ -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);

Expand Down
44 changes: 38 additions & 6 deletions src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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"}]}';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1183,6 +1189,7 @@ export class SDK {
address: string,
salt: string,
password: string,
allRelatedPks: string,
requiredSignatureNum: string,
txDada: string,
callback: string) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
});
}

}
20 changes: 19 additions & 1 deletion src/transaction/transactionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { Transfer } from './transfer';
Expand Down Expand Up @@ -90,6 +91,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.
*
Expand All @@ -113,7 +131,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');
}
Expand Down

0 comments on commit 65b6f04

Please sign in to comment.