Skip to content

Commit

Permalink
Fix XBR implementation (crossbario#531)
Browse files Browse the repository at this point in the history
* WIP: snapshot

* more work towards xbr fix

* some sanity restoration

* selling is now working

* more improvements

* some progress towards buyer

* fix buyer

* a bit const

* minor tweak

* make SimpleBuyer a ES6 class

* make SimpleSeller a ES6 class
  • Loading branch information
om26er authored Sep 4, 2020
1 parent 2f42d2e commit e93e16c
Show file tree
Hide file tree
Showing 8 changed files with 593 additions and 520 deletions.
2 changes: 2 additions & 0 deletions packages/autobahn-xbr/lib/autobahn-xbr.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var ethereum = require('./ethereum.js');
var eip712 = require('./eip712.js');
exports.sign_eip712_data = eip712.sign_eip712_data;
exports.recover_eip712_signer = eip712.recover_eip712_signer;
exports.create_market_member_login = eip712.create_market_member_login;

exports.SimpleBuyer = require('./buyer.js').SimpleBuyer;
exports.SimpleSeller = require('./seller.js').SimpleSeller;
Expand Down Expand Up @@ -69,3 +70,4 @@ exports.pack_uint256 = util.pack_uint256;
exports.unpack_uint256 = util.unpack_uint256;
exports.with_0x = util.with_0x;
exports.without_0x = util.without_0x;
exports.XBR = util.XBR;
359 changes: 176 additions & 183 deletions packages/autobahn-xbr/lib/buyer.js

Large diffs are not rendered by default.

138 changes: 97 additions & 41 deletions packages/autobahn-xbr/lib/eip712.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
//
///////////////////////////////////////////////////////////////////////////////

var assert = require('assert');
const assert = require('assert');

var w3_utils = require("web3-utils");
var eth_sig_utils = require("eth-sig-util");
var eth_util = require("ethereumjs-util");
const eth_sig_utils = require("eth-sig-util");
const eth_util = require("ethereumjs-util");

var web3 = require('web3');
var BN = web3.utils.BN;
const web3 = require('web3');
const BN = web3.utils.BN;

// the XBR token has 18 decimals
const decimals = new BN('1000000000000000000');
Expand All @@ -27,66 +26,123 @@ const decimals = new BN('1000000000000000000');
const verifying_adr = '0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B';


function _create_eip712_data (verifying_adr, channel_adr, channel_seq, balance, is_final) {
const _EIP712_MSG = {
function _create_eip712_data (chain_id, verifying_contract, close_at, market_oid, channel_oid, channel_seq, balance,
is_final) {
return {
'types': {
'EIP712Domain': [
{'name': 'name', 'type': 'string'},
{'name': 'version', 'type': 'string'},
{'name': 'chainId', 'type': 'uint256'},
{'name': 'verifyingContract', 'type': 'address'},
],
'ChannelClose': [
// The channel contract address.
{'name': 'channel_adr', 'type': 'address'},

// Channel off-chain transaction sequence number.
{'name': 'channel_seq', 'type': 'uint32'},

// Balance remaining in after the transaction.
{'name': 'balance', 'type': 'uint256'},

// Transaction is marked as final.
{'name': 'is_final', 'type': 'bool'},
{
'name': 'name',
'type': 'string'
},
{
'name': 'version',
'type': 'string'
},
],
'EIP712ChannelClose': [{
'name': 'chainId',
'type': 'uint256'
}, {
'name': 'verifyingContract',
'type': 'address'
}, {
'name': 'closeAt',
'type': 'uint256'
}, {
'name': 'marketId',
'type': 'bytes16'
}, {
'name': 'channelId',
'type': 'bytes16'
}, {
'name': 'channelSeq',
'type': 'uint32'
}, {
'name': 'balance',
'type': 'uint256'
}, {
'name': 'isFinal',
'type': 'bool'
}]
},
'primaryType': 'ChannelClose',
'primaryType': 'EIP712ChannelClose',
'domain': {
'name': 'XBR',
'version': '1',
'chainId': 1,
'verifyingContract': verifying_adr,
},
'message': {
'channel_adr': channel_adr,
'channel_seq': channel_seq,
'chainId': chain_id,
'verifyingContract': verifying_contract,
'closeAt': close_at,
'marketId': market_oid,
'channelId': channel_oid,
'channelSeq': channel_seq,
'balance': balance,
'is_final': is_final,
},
'isFinal': is_final
}
}
return _EIP712_MSG;
}


function sign_eip712_data (eth_privkey, channel_adr, channel_seq, balance, is_final) {
assert.equal((typeof channel_seq === "number") && Math.floor(channel_seq) === channel_seq && channel_seq > 0, true);
function sign_eip712_data(eth_privkey, chain_id, verifying_contract, close_at, market_oid, channel_oid, channel_seq,
balance, is_final) {
assert.equal(BN.isBN(balance), true);
assert.equal(typeof is_final === "boolean", true);
balance = '0x' + balance.toString('hex');
const msg = _create_eip712_data(verifying_adr, channel_adr, channel_seq, balance, is_final);
const msg = _create_eip712_data(chain_id, verifying_contract, close_at, market_oid, channel_oid, channel_seq,
balance, is_final);
const sig = eth_sig_utils.signTypedData(eth_privkey, {data: msg});
return eth_util.toBuffer(sig);
}


function recover_eip712_signer (channel_adr, channel_seq, balance, is_final, signature) {
const msg = _create_eip712_data(verifying_adr, channel_adr, channel_seq, balance, is_final);
const signer = eth_sig_utils.recoverTypedSignature({msg, signature});
return w3_utils.toChecksumAddress(signer);
function recover_eip712_signer(chain_id, verifying_contract, close_at, market_oid, channel_oid, channel_seq, balance,
is_final, signature) {
const msg = _create_eip712_data(chain_id, verifying_contract, close_at, market_oid, channel_oid, channel_seq,
balance, is_final);
return eth_sig_utils.recoverTypedSignature({data: msg, sig: signature});
}


function create_market_member_login(member, clientPubKey) {
return {
'types': {
'EIP712Domain': [
{
'name': 'name',
'type': 'string'
},
{
'name': 'version',
'type': 'string'
},
],
'EIP712MarketMemberLogin': [
{
'name': 'member',
'type': 'address'
},
{
'name': 'client_pubkey',
'type': 'bytes32',
},
]
},
'primaryType': 'EIP712MarketMemberLogin',
'domain': {
'name': 'XBR',
'version': '1',
},
'message': {
'member': member,
'client_pubkey': clientPubKey,
}
}
}


exports.sign_eip712_data = sign_eip712_data;
exports.recover_eip712_signer = recover_eip712_signer;
exports.decimals = decimals;
exports.verifying_adr = verifying_adr;
exports.create_market_member_login = create_market_member_login;
Loading

0 comments on commit e93e16c

Please sign in to comment.