Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
signature format update (v 0.12)
Browse files Browse the repository at this point in the history
  • Loading branch information
clbrge committed Aug 11, 2018
1 parent 5245c6c commit d0b3b91
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion contracts/RougeFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import "./RougeRegistry.sol";

contract RougeFactory is RougeRegistry {

bytes8 public version = '0.11.0';
string public version = '0.12.0';

// The Rouge Token contract address
RGETokenInterface public rge;
Expand Down
16 changes: 13 additions & 3 deletions contracts/SimpleRougeCampaign.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import "./RougeFactoryInterface.sol";

contract SimpleRougeCampaign {

bytes8 public version = '0.11.0';
string public version = '0.12.0';

// The Rouge Token contract address
RGETokenInterface public rge;
Expand Down Expand Up @@ -63,8 +63,18 @@ contract SimpleRougeCampaign {
}

// web3.eth.sign compat prefix XXX mv to lib
function prefixed(bytes32 _message) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", _message));
function getHexString(bytes32 value) internal pure returns (string) {
bytes memory result = new bytes(64);
string memory characterString = "0123456789abcdef";
bytes memory characters = bytes(characterString);
for (uint8 i = 0; i < 32; i++) {
result[i * 2] = characters[uint256((value[i] & 0xF0) >> 4)];
result[i * 2 + 1] = characters[uint256(value[i] & 0xF)];
}
return string(result);
}
function prefixed(bytes32 _hash) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n74Rouge ID: ", getHexString(_hash)));
}

bytes4 public scheme;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
],
"license": "AGPL-3.0-only",
"dependencies": {
"eth-sig-util": "^2.0.2",
"ethereumjs-abi": "^0.6.5",
"ethereumjs-util": "^5.2.0"
}
Expand Down
5 changes: 3 additions & 2 deletions test/CouponDemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,16 @@ contract('SimpleRougeCampaign(CouponDemo)', function(accounts) {

// call acquire with auth message and attestor signature
const auth1 = create_auth_hash('acceptAcquisition', campaign.address, bearer)
const sign1 = get_signature(auth1)
const sign1 = get_signature('Rouge ID: ' + auth1.substr(2))

await campaign.acquire(auth1, sign1.v, sign1.r, sign1.s, attestor, {from: bearer});

const acquired = await campaign.acquired.call();
assert.equal(acquired.toNumber(), 1, "check notes acquired after distributeNote");

// call acceptRedemption with auth message and attestor signature
const auth2 = create_auth_hash('acceptRedemption', campaign.address, bearer)
const sign2 = get_signature(auth2)
const sign2 = get_signature('Rouge ID: ' + auth2.substr(2))
await campaign.redeem(auth2, sign2.v, sign2.r, sign2.s, attestor, {from: bearer});

const redeemed = await campaign.redeemed.call();
Expand Down
6 changes: 4 additions & 2 deletions test/RougeFactory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const ethUtil = require('ethereumjs-util')

const RGEToken = artifacts.require("./TestRGEToken.sol");
const Factory = artifacts.require("./RougeFactory.sol");
const SimpleRougeCampaign = artifacts.require("./SimpleRougeCampaign.sol");
Expand Down Expand Up @@ -36,10 +38,10 @@ contract('RougeFactory', function(accounts) {
const issuer_balance_post = await rge.balanceOf.call(issuer);
assert.equal(issuer_balance_post.toNumber(), tokens, "issuer has receive tokens to create a campaign");

const estimate = await rge.newCampaign.estimateGas(issuance, deposit, {from: issuer, gas: 3000000});
const estimate = await rge.newCampaign.estimateGas(issuance, deposit, {from: issuer, gas: 3000778});
// console.log('Base estimate newCampaign => ', estimate)

const result = await rge.newCampaign(issuance, deposit, {from: issuer, gas: estimate + 60000, gasPrice: web3.toWei(1, "gwei")})
const result = await rge.newCampaign(issuance, deposit, {from: issuer, gas: estimate + 80000, gasPrice: web3.toWei(1, "gwei")})

assert.equal(result.receipt.cumulativeGasUsed, estimate, "cumulativeGasUsed correctly predict");

Expand Down
19 changes: 10 additions & 9 deletions test/SimpleRougeCampaign.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

const abi = require('ethereumjs-abi')
const BN = require('bn.js')
const ethUtil = require('ethereumjs-util')

const RGEToken = artifacts.require("./TestRGEToken.sol");
const Factory = artifacts.require("./RougeFactory.sol");
Expand Down Expand Up @@ -34,9 +35,9 @@ const create_auth_hash = function(msg, campaign, account) {

}

const get_signature = function(account, hash) {
const get_signature = function(account, msg) {

const signature = web3.eth.sign(account, hash).substr(2)
const signature = web3.eth.sign(account, ethUtil.bufferToHex(ethUtil.toBuffer(msg))).substr(2)
return {
r: '0x' + signature.slice(0, 64),
s: '0x' + signature.slice(64, 128),
Expand Down Expand Up @@ -96,9 +97,9 @@ contract('SimpleRougeCampaign', function(accounts) {
assert.equal(acquired.toNumber(), 1, "check notes acquired after distributeNote");

// call acceptRedemption with auth message and bearer signature
const auth2 = create_auth_hash('acceptRedemption', campaign.address, bearer)
const sign2 = get_signature(bearer, auth2)
await campaign.acceptRedemption(bearer, auth2, sign2.v, sign2.r, sign2.s, {from: issuer});
const auth = create_auth_hash('acceptRedemption', campaign.address, bearer)
const sign = get_signature(bearer, 'Rouge ID: ' + auth.substr(2))
await campaign.acceptRedemption(bearer, auth, sign.v, sign.r, sign.s, {from: issuer});

const redeemed = await campaign.redeemed.call();
assert.equal(redeemed.toNumber(), 1, "note(s) redeemed after confirmRedemption");
Expand Down Expand Up @@ -131,15 +132,15 @@ contract('SimpleRougeCampaign', function(accounts) {

// call acquire with auth message and issuer signature
const auth1 = create_auth_hash('acceptAcquisition', campaign.address, bearer)
const sign1 = get_signature(issuer, auth1)
const sign1 = get_signature(issuer, 'Rouge ID: ' + auth1.substr(2))
await campaign.acquire(auth1, sign1.v, sign1.r, sign1.s, issuer, {from: bearer});

const acquired = await campaign.acquired.call();
assert.equal(acquired.toNumber(), 1, "check notes acquired after distributeNote");

// call acceptRedemption with auth message and issuer signature
const auth2 = create_auth_hash('acceptRedemption', campaign.address, bearer)
const sign2 = get_signature(issuer, auth2)
const sign2 = get_signature(issuer, 'Rouge ID: ' + auth2.substr(2))
await campaign.redeem(auth2, sign2.v, sign2.r, sign2.s, issuer, {from: bearer});

const redeemed = await campaign.redeemed.call();
Expand Down Expand Up @@ -177,15 +178,15 @@ contract('SimpleRougeCampaign', function(accounts) {

// call acquire with auth message and attestor signature
const auth1 = create_auth_hash('acceptAcquisition', campaign.address, bearer)
const sign1 = get_signature(attestor, auth1)
const sign1 = get_signature(attestor, 'Rouge ID: ' + auth1.substr(2))
await campaign.acquire(auth1, sign1.v, sign1.r, sign1.s, attestor, {from: bearer});

const acquired = await campaign.acquired.call();
assert.equal(acquired.toNumber(), 1, "check notes acquired after distributeNote");

// call acceptRedemption with auth message and attestor signature
const auth2 = create_auth_hash('acceptRedemption', campaign.address, bearer)
const sign2 = get_signature(attestor, auth2)
const sign2 = get_signature(attestor, 'Rouge ID: ' + auth2.substr(2))
await campaign.redeem(auth2, sign2.v, sign2.r, sign2.s, attestor, {from: bearer});

const redeemed = await campaign.redeemed.call();
Expand Down

0 comments on commit d0b3b91

Please sign in to comment.