Skip to content

Commit

Permalink
Merge pull request #36 from a1300/finish_proposals_gateways
Browse files Browse the repository at this point in the history
feat(proposal): Finish proposal and gateway features, closes #35
  • Loading branch information
liangpeili authored Oct 8, 2018
2 parents 0702b51 + f190b62 commit c49e768
Show file tree
Hide file tree
Showing 6 changed files with 1,006 additions and 49 deletions.
4 changes: 2 additions & 2 deletions lib/transactions/gateway.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var crypto = require("./crypto.js")
var transaction = require('./transaction.js')

function registerMember(options, secret, secondSecret) {
function registerMember(gateway, memberPublicKey, secret, secondSecret) {
let keys = crypto.getKeys(secret);
return transaction.createTransactionEx({
type: 401,
fee: 100 * 1e8,
secret: secret,
secondSecret, secondSecret,
args: [options.gateway, keys.publicKey]
args: [gateway, memberPublicKey]
})
}

Expand Down
129 changes: 83 additions & 46 deletions lib/transactions/proposal.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,125 @@
var crypto = require("./crypto.js")
var transaction = require('./transaction.js')

/*function propose(options, secret, secondSecret) {
var keys = crypto.getKeys(secret);
function registerGateway(options, secret, secondSecret) {
// construct content
let content = {
name : options.gatewayName,
desc : options.gatewayDesc,
minimumMembers: options.minimumMembers || 3,
updateInterval: options.updateInterval || 8640,
currency: {
symbol : options.currencySymbol,
desc: options.currencyDesc,
precision : options.currencyPrecision
}
}

return transaction.createTransactionEx({
type: 300,
fee: 10 * 1e8,
secret: secret,
secondSecret, secondSecret,
args: [options.title, options.desc, null, null, options.endHeight]
type: 300,
fee: 10 * 1e8,
secret: secret,
secondSecret: secondSecret,
args: [
options.proposalTitle || 'title for gateway_register',
options.proposalDesc, 'gateway_register',
content,
options.proposalEndHeight
]
})
}*/
}

function registergateway(options, secret, secondSecret) {

let keys = crypto.getKeys(secret);
let currency = {
symbol : options.symbol,
desc: options.currencyDesc,
precision : options.precision
}
// construct this content
function initGateway(options, secret, secondSecret) {
// construct content
let content = {
name : options.name,
desc : options.desc,
minimumMembers: options.minimumMembers,
updateInterval: options.updateInterval,
currency: currency
gateway : options.gatewayName,
members: options.gatewayMembers
}

return transaction.createTransactionEx({
type: 300,
fee: 10 * 1e8,
secret: secret,
secondSecret, secondSecret,
args: [options.title, options.desc, 'gateway_register', content, options.endHeight]
secondSecret: secondSecret,
args: [
options.proposalTitle || 'title for gateway_init',
options.proposalDesc || 'desc for gateway_init',
'gateway_init',
content,
options.proposalEndHeight
]
})
}

function initgateway(options, secret, secondSecret) {

let keys = crypto.getKeys(secret);
// construct this content
function updateGatewayMember(options, secret, secondSecret) {
// construct content
let content = {
gateway : options.name,
members: options.members
gateway: options.gatewayName,
from: options.fromAddress,
to: options.toAddress
}

return transaction.createTransactionEx({
type: 300,
fee: 10 * 1e8,
secret: secret,
secondSecret, secondSecret,
args: ['xxxxxxxxxx', '', 'gateway_init', content, 500000]
secondSecret: secondSecret,
args: [
options.proposalTitle || 'title for gateway_update_member',
options.proposalDesc || 'desc for gateway_update_member',
'gateway_update_member',
content,
options.proposalEndHeight
]
})
}

function activate(options, secret, secondSecret) {
function revokeGateway(options, secret, secondSecret) {
// construct content
let content = {
gateway: options.gatewayName
}

return transaction.createTransactionEx({
type: 300,
fee: 10 * 1e8,
secret: secret,
secondSecret: secondSecret,
args: [
options.proposalTitle || 'title for gateway_revoke',
options.proposalDesc || 'desc for gateway_revoke',
'gateway_revoke',
content,
options.proposalEndHeight
]
})
}

let keys = crypto.getKeys(secret);
function activateProposal(tid, secret, secondSecret) {
return transaction.createTransactionEx({
type: 302,
fee: 0 * 1e8,
secret: secret,
secondSecret, secondSecret,
args: [options.tid]
secondSecret: secondSecret,
args: [tid]
})
}

function upvote(options, secret, secondSecret) {

let keys = crypto.getKeys(secret);
function upvoteProposal(tid, secret, secondSecret) {
return transaction.createTransactionEx({
type: 301,
fee: 1e7, // 0.1 * 1e8
secret: secret,
secondSecret, secondSecret,
args: [options.tid]
secondSecret: secondSecret,
args: [tid]
})
}

module.exports = {
//propose: propose
registergateway: registergateway,
initgateway: initgateway,
activate: activate,
upvote: upvote
registerGateway: registerGateway,
initGateway: initGateway,
updateGatewayMember: updateGatewayMember,
revokeGateway: revokeGateway,
activateProposal: activateProposal,
upvoteProposal: upvoteProposal
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"license": "MIT",
"types": "./types/index.d.ts",
"dependencies": {
"@types/node": "^10.9.4",
"@types/node": "=8.10.29",
"JSONStream": "=1.3.1",
"browserify-bignum": "=1.3.0-2",
"buffer": "=4.7.0",
Expand Down
104 changes: 104 additions & 0 deletions test/gateway.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
var Buffer = require("buffer/").Buffer;
var should = require("should");
var asch = require("../index.js");

describe("gateway.js", () => {
var gateway = asch.gateway;

it("should be ok", () => {
(gateway).should.be.ok;
});

it("should be object", () => {
(gateway).should.be.type("object");
});

describe("#registerMember", () => {
var registerMember;
var trs;

beforeEach(() => {
registerMember = asch.gateway.registerMember;
let publicKey = asch.crypto.getKeys("secret").publicKey;
trs = registerMember("name", publicKey, "secret");
});

afterEach(() => {
trs = null;
});

it("should have property registerMember", () => {
(gateway).should.have.property("registerMember");
});

it("should be function", () => {
(registerMember).should.be.type("function");
});

it("should create registerMember transaction", () => {
(trs).should.be.ok;
(trs).should.be.type("object");
});

describe("returned registerMember transaction", () => {
it("should have id as string", () => {
(trs.id).should.be.type("string");
});

it("should have type as number and equal 401", () => {
(trs.type).should.be.type("number").and.equal(401);
});

it("should have args as array with 2 items", () => {
(trs.args).should.be.an.Array().with.a.lengthOf(2);
});

it("should have gateway-name as first item", () => {
should(trs.args[0]).be.type("string").and.equal("name");
});

it("should have publicKey from new member as second item", () => {
should(trs.args[1]).be.type("string").and.equal("5d036a858ce89f844491762eb89e2bfbd50a4a0a0da658e4b2628b25b117ae09");
});

it("should have fee and equal 100 XAS", () => {
(trs.fee).should.be.type("number").and.equal(100 * 1e8);
});

it("should have senderPublicKey as hex string", () => {
(trs.senderPublicKey).should.be.type("string").and.match(function (given) {
try {
new Buffer(trs.senderPublicKey, "hex");
} catch (e) {
return false;
}

return true;
});
});

it("should have one signature as hex string in signatures array", () => {
(trs.signatures[0]).should.be.type("string").and.match(() => {
try {
new Buffer(trs.signatures[0], "hex")
} catch (e) {
return false;
}
return true;
})
});

it("should be signed correctly", () => {
var result = asch.crypto.verify(trs);

(result).should.be.ok;
});

it("should not be signed correctly now", () => {
trs.amount = 24242424;
var result = asch.crypto.verify(trs);
(result).should.be.not.ok;
});
});
});
});
Loading

0 comments on commit c49e768

Please sign in to comment.