Skip to content

Commit

Permalink
fix: 🐛 Fix a few of bugs about crypto and transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
imfly authored and imfly committed Jun 6, 2020
1 parent c9b6f08 commit 71fbddd
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 592 deletions.
2 changes: 1 addition & 1 deletion examples/fun-tests/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default {
send: "10000000",
vote: "100000000",
secondSignature: "500000000",
delegate: "2500000000",
delegate: "10000000000",
multiSignature: "500000000",
dappRegistration: "2500000000",
dappWithdrawal: "10000000",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ddn",
"version": "3.4.0",
"version": "3.4.15",
"repository": {
"type": "git",
"url": "git+https://github.com/ddnlink/ddn.git"
Expand Down
2 changes: 1 addition & 1 deletion packages/asset-dapp/src/dapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ class Dapp extends Asset.Base {
}
}

let delegatesArr = []
if (!dapp.delegates) {
throw new Error("Have no dapp delegates");
} else {
let delegatesArr = []
delegatesArr = typeof dapp.delegates === 'string' ? dapp.delegates.split(',') : dapp.delegates;
if (delegatesArr.length < 5 ||
delegatesArr.length > this.config.settings.delegateNumber) {
Expand Down
6 changes: 4 additions & 2 deletions packages/asset-dapp/test/unit/dapps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ beforeAll(done => {
debug('signatures', body);
node.expect(err).be.not.ok;

// node.expect(body).to.have.property("success").to.be.true;
node.expect(body).to.have.property("success").to.be.true;
node.expect(body).to.have.property("transaction").that.is.an("object");
done();
});
Expand Down Expand Up @@ -295,7 +295,9 @@ describe("PUT /dapps", () => {
// });
// });

it("Category is number, Using invalid Category, Should fail", done => {
it("Category is number, Using invalid Category, Should fail", async done => {
await node.onNewBlockAsync();

node.api.put("/dapps")
.set("Accept", "application/json")
.set("version", node.version)
Expand Down
24 changes: 11 additions & 13 deletions packages/crypto/src/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ function generateAddress(publicKey, tokenPrefix) {
* @param {string} publicKey 公钥
*/
function verifyBytes(bytes, signature, publicKey) {
// 保证字节类型
if (!(bytes instanceof Buffer)) {
bytes = Buffer.from(bytes);
}

const hash = createHash(bytes);

const signatureBuffer = Buffer.from(signature, "hex");
Expand All @@ -163,23 +168,17 @@ function verifyHash(hash, signature, publicKey) {

// 验证签名
// todo: 本方法并没有被实际使用,请参考 peer/kernal/transaction/transaction.js的 verifySignature 重构
async function verify(transaction, signature, senderPublicKey) {
// let remove = 64;

// // 如果有二次签名就先减掉
if (transaction.signature) {
async function verify(transaction, senderPublicKey) {
if (!transaction.signature) {
return false;
}

// const bytes = await getBytes(transaction);
// const data2 = Buffer.allocUnsafe(bytes.length - remove);

// for (let i = 0; i < data2.length; i++) {
// data2[i] = bytes[i];
// }
if (!senderPublicKey) {
senderPublicKey = transaction.senderPublicKey;
}

const bytes = await getBytes(transaction, true, true);
return verifyBytes(bytes, signature, senderPublicKey)
return verifyBytes(bytes, transaction.signature, senderPublicKey)
}

/**
Expand All @@ -192,7 +191,6 @@ async function verifySecondSignature(transaction, publicKey) {
return false;
}
const bytes = await getBytes(transaction, false, true);

return verifyBytes(bytes, transaction.sign_signature, publicKey)
}

Expand Down
4 changes: 1 addition & 3 deletions packages/crypto/test/naci.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,12 @@ describe('NaCI', () => {
})

it("#createHash should be ok, and return a Buffer, Uint8Array too.", (done) => {
const buf = Buffer.from('test');
const buf = Buffer.from('test'); // 转化一下
const hash1 = DdnCrypto.createHash(buf);
const hash2 = DdnCrypto.createHash('test');

debug(hash1);
expect(hash1 instanceof Buffer).be.true;
expect(hash1 instanceof Uint8Array).be.true;
expect(hash2).be.not.ok;
done();
})
})
4 changes: 2 additions & 2 deletions packages/node-sdk/src/constants/constants.ddn.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default {
send: "10000000",
vote: "10000000",
secondSignature: "500000000",
delegate: "2500000000",
delegate: "10000000000",
multiSignature: "500000000",
dappRegistration: "2500000000",
dappWithdrawal: "10000000",
Expand Down Expand Up @@ -82,7 +82,7 @@ export default {
send: "10000000",
vote: "10000000",
secondSignature: "500000000",
delegate: "2500000000",
delegate: "10000000000",
multisignature: "500000000",
dappRegistration: "2500000000",
dappWithdrawal: "10000000",
Expand Down
2 changes: 1 addition & 1 deletion packages/node-sdk/test/unit/nodeSdk.transaction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe("transaction.js", () => {

it("should have sign_signature as hex string", () => {
expect(trs.sign_signature).to.be.a("string")
// .and.match(() => {
// .to.match(() => {
// try {
// Buffer.from(trs.sign_signature, "hex");
// } catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion packages/peer/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default {
send: "10000000",
vote: "100000000",
secondSignature: "500000000",
delegate: "2500000000",
delegate: "10000000000",
multiSignature: "500000000",
dappRegistration: "2500000000",
dappWithdrawal: "10000000",
Expand Down
7 changes: 4 additions & 3 deletions packages/peer/test/unit/1.delegates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ describe("PUT /delegates to regist with funds", () => {
debug("blank username, fail", JSON.stringify(body));
node.expect(err).be.not.ok;
node.expect(body).to.have.property("success").to.be.false;
node.expect(body).to.have.property("error");
node.expect(body).to.have.property("error").to.match(/^Username is undefined/);
done();
});
});
Expand Down Expand Up @@ -839,8 +839,9 @@ describe("GET /delegates/voters", () => {
.end((err, { body }) => {
debug("get votes no publicKey fail", JSON.stringify(body));
node.expect(err).be.not.ok;
node.expect(body).to.have.property("success");
node.expect(body).to.have.property("error");
node.expect(body).to.have.property("success").be.true;
node.expect(body).to.have.property("accounts");
node.expect(body.accounts.length).to.be.equal(0);

done();
});
Expand Down
1 change: 0 additions & 1 deletion packages/peer/test/unit/peers.delegates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ describe("Registering a delegate", () => {
node.expect(err).to.be.not.ok;
account2.username = node.randomDelegateName().toLowerCase();
const transaction = await node.ddn.delegate.createDelegate(account2.username, account2.password);
// console.log(transaction);

node.peer.post("/transactions")
.set("Accept", "application/json")
Expand Down
56 changes: 28 additions & 28 deletions packages/peer/test/unit/peers.votes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import node from "@ddn/node-sdk/lib/test";
import DdnUtils from '@ddn/utils';
import Debug from "debug";

const debug = new Debug('peer');
const debug = new Debug('debug');

const account = node.randomAccount();
const voterAccount = node.randomAccount();
Expand Down Expand Up @@ -193,6 +193,32 @@ describe("POST /peer/transactions", () => {
});
});

// 不能投给普通用户
it("Voting for an common user. Should be fail", async done => {
const transaction = await node.ddn.vote.createVote([`+${account.publicKey}`], account.password);
node.onNewBlock(err => {
node.expect(err).to.be.not.ok;
node.peer.post("/transactions")
.set("Accept", "application/json")
.set("version", node.version)
.set("nethash", node.config.nethash)
.set("port", node.config.port)
.send({
transaction
})
.expect("Content-Type", /json/)
.expect(200)
.end((err, { body }) => {
node.expect(err).to.be.not.ok;

debug("Voting for an common user, fail", body);
node.expect(body).to.have.property("success").to.be.false;
node.expect(body).to.have.property("error").to.equal('Delegate not found');
done();
});
});
});

// Not right test, because sometimes new block comes and we don't have time to vote
it("Registering a new delegate. Should be ok", done => {
node.api.post("/accounts/open")
Expand Down Expand Up @@ -256,32 +282,6 @@ describe("POST /peer/transactions", () => {
});
});

// 不能投给普通用户
it("Voting for an common user. Should be fail", async done => {
const transaction = await node.ddn.vote.createVote([`+${account.publicKey}`], account.password);
node.onNewBlock(err => {
node.expect(err).to.be.not.ok;
node.peer.post("/transactions")
.set("Accept", "application/json")
.set("version", node.version)
.set("nethash", node.config.nethash)
.set("port", node.config.port)
.send({
transaction
})
.expect("Content-Type", /json/)
.expect(200)
.end((err, { body }) => {
node.expect(err).to.be.not.ok;

debug(body);
node.expect(body).to.have.property("success").to.be.false;
node.expect(body).to.have.property("error").to.equal('Delegate not found');
done();
});
});
});

// 只有受托人才能接受投票
it("Voting for a delegate. Should be ok", async done => {
const transaction = await node.ddn.vote.createVote([`+${delegate2_pubKey}`], account.password);
Expand All @@ -300,7 +300,7 @@ describe("POST /peer/transactions", () => {
.end((err, { body }) => {
node.expect(err).to.be.not.ok;

debug(body);
debug("Voting for a delegate ok", body);
node.expect(body).to.have.property("success").to.be.true;
done();
});
Expand Down
Loading

0 comments on commit 71fbddd

Please sign in to comment.