Skip to content

Commit

Permalink
grant flow by account and user
Browse files Browse the repository at this point in the history
  • Loading branch information
wilmveel committed Apr 4, 2016
1 parent 0c1516e commit d3a357e
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/contracts/Grant.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ contract Grant {
user = msg.sender;
}

function state() constant returns (address b, address a){
a = app;
b = user;
}

}
5 changes: 4 additions & 1 deletion src/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module.exports = function(privateKey){
if(!privateKey)
privateKey = crypto.randomBytes(32);

var address = ethereumjsUtil.privateToAddress(privateKey);

var web3 = new Web3();

var signer = require('./signer')(privateKey);
Expand All @@ -21,9 +23,10 @@ module.exports = function(privateKey){
});

web3.setProvider(hookedWeb3Provider);
web3.eth.defaultAccount = ethereumjsUtil.bufferToHex(address);

return {
address: ethereumjsUtil.privateToAddress(privateKey),
address: ethereumjsUtil.bufferToHex(address),
eth: web3.eth
}

Expand Down
2 changes: 1 addition & 1 deletion src/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module.exports = function watch(web3) {
var filter = web3.eth.filter("latest");
filter.watch(function (error, log) {
web3.eth.getTransaction(transaction, function (err, t) {
console.log("watch", t);
if (t.blockHash) {
console.log("transaction", t);
filter.stopWatching();
callback();
}
Expand Down
46 changes: 37 additions & 9 deletions test/grantFlow.spec.js → test/grantByAccount.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('GrantFlow', function () {
appWallet = new Wallet();
var transaction = web3.eth.sendTransaction({
from: web3.eth.coinbase,
to: ethereumjsUtil.bufferToHex(appWallet.address),
to: appWallet.address,
value: web3.toWei(1, "ether")
}
)
Expand All @@ -50,7 +50,7 @@ describe('GrantFlow', function () {
userWallet = new Wallet();
var transaction = web3.eth.sendTransaction({
from: web3.eth.coinbase,
to: ethereumjsUtil.bufferToHex(userWallet.address),
to: userWallet.address,
value: web3.toWei(1, "ether")
}
)
Expand All @@ -63,15 +63,14 @@ describe('GrantFlow', function () {

});

it('app creates grant contract', function (done) {
it('should create grant contract by app', function (done) {

var abi = compiled.Grant.info.abiDefinition;
var code = compiled.Grant.code;

appWallet.eth.contract(abi).new({
gas: 5000000000000000000000,
data: code,
from: ethereumjsUtil.bufferToHex(appWallet.address)
data: code
}, function (err, contract) {
if (err) {
console.log("Contract creation error", err);
Expand All @@ -84,7 +83,7 @@ describe('GrantFlow', function () {
});
});

it('app creates grant contract', function (done) {
it('should authorize grant contract by user', function (done) {

var abi = compiled.Grant.info.abiDefinition;
var code = compiled.Grant.code;
Expand All @@ -94,9 +93,38 @@ describe('GrantFlow', function () {
console.log("Contract creation error", err);
done(err);
} else if (contract.address) {
console.log("Contract Created", contract.address);
grantContract = contract.address;
done();
console.log("Contract Fetch", contract.address);
contract.authorize({
gas: 5000000000000000000000
}, function (err, transaction) {
console.log(err, transaction);
watch(transaction, function (err, res) {
console.log(err, res);
done();
});
});
}
});
});


it('should get state of grant contract', function (done) {

var abi = compiled.Grant.info.abiDefinition;
var code = compiled.Grant.code;

web3.eth.contract(abi).at(grantContract, function (err, contract) {
if (err) {
console.log("Contract creation error", err);
done(err);
} else if (contract.address) {
console.log("Contract Fetch", contract.address);
contract.state(function (err, res) {
console.log('state', res);
assert.equal(userWallet.address, res[0]);
assert.equal(appWallet.address, res[1]);
done();
});
}
});
});
Expand Down
154 changes: 154 additions & 0 deletions test/grantByUser.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
var assert = require('assert');

var Web3 = require('web3');
var ethereumjsUtil = require('ethereumjs-util');

var Watch = require('../src/watch');
var Wallet = require('../src/wallet');

var contracts = require('../src/contracts');

describe('GrantFlow', function () {

this.timeout(1000000);

var web3 = new Web3();
var watch = new Watch(web3);

var defaultProvider = new web3.providers.HttpProvider("http://128.199.53.68:8545")

web3.setProvider(defaultProvider);
web3.eth.defaultAccount = web3.eth.coinbase;

var compiled = web3.eth.compile.solidity(contracts);

var appWallet;
var userWallet;

var grantContract;
var userContract;

it('should create a app wallet and transfer 1 ether', function (done) {

appWallet = new Wallet();
var transaction = web3.eth.sendTransaction({
from: web3.eth.coinbase,
to: appWallet.address,
value: web3.toWei(1, "ether")
}
)
console.log(transaction);

watch(transaction, function (err, res) {
console.log(err, res);
done();
})

});

it('should create a user wallet and transfer 1 ether', function (done) {

userWallet = new Wallet();
var transaction = web3.eth.sendTransaction({
from: web3.eth.coinbase,
to: userWallet.address,
value: web3.toWei(1, "ether")
}
)
console.log(transaction);

watch(transaction, function (err, res) {
console.log(err, res);
done();
})

});

it('should create grant contract by app', function (done) {

var abi = compiled.Grant.info.abiDefinition;
var code = compiled.Grant.code;

appWallet.eth.contract(abi).new({
gas: 5000000000000000000000,
data: code
}, function (err, contract) {
if (err) {
console.log("Contract creation error", err);
done(err);
} else if (contract.address) {
console.log("Contract Created", contract.address);
grantContract = contract.address;
done();
}
});
});

it('should create user contract by user', function (done) {

var abi = compiled.User.info.abiDefinition;
var code = compiled.User.code;

userWallet.eth.contract(abi).new({
gas: 5000000000000000000000,
data: code,
}, function (err, contract) {
if (err) {
console.log("Contract creation error", err);
done(err);
} else if (contract.address) {
console.log("Contract Created", contract.address);
userContract = contract.address;
done();
}
});
});

it('should authorize grant contract by user contract', function (done) {

var abi = compiled.User.info.abiDefinition;
var code = compiled.User.code;

userWallet.eth.contract(abi).at(userContract, function (err, contract) {
if (err) {
console.log("Contract creation error", err);
done(err);
} else if (contract.address) {
console.log("Contract Fetch", contract.address);
contract.authorize(grantContract, {
gas: 5000000000000000000000
}, function (err, transaction) {
console.log(err, transaction);
watch(transaction, function (err, res) {
console.log(err, res);
done();
});
});
}
});
});


it('should get state of grant contract', function (done) {

var abi = compiled.Grant.info.abiDefinition;
var code = compiled.Grant.code;

web3.eth.contract(abi).at(grantContract, function (err, contract) {
if (err) {
console.log("Contract creation error", err);
done(err);
} else if (contract.address) {
console.log("Contract Fetch", contract.address);
contract.state(function (err, res) {
console.log('state', res);
assert.equal(userContract, res[0]);
assert.equal(appWallet.address, res[1]);
done();
});
}
});
});


});

0 comments on commit d3a357e

Please sign in to comment.