Skip to content

Commit

Permalink
Merge dfd5060 into 3fb420f
Browse files Browse the repository at this point in the history
  • Loading branch information
debris committed May 21, 2015
2 parents 3fb420f + dfd5060 commit ba97d67
Show file tree
Hide file tree
Showing 16 changed files with 1,127 additions and 62 deletions.
319 changes: 297 additions & 22 deletions dist/web3-light.js

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions dist/web3-light.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web3-light.min.js

Large diffs are not rendered by default.

321 changes: 298 additions & 23 deletions dist/web3.js

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions dist/web3.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web3.min.js

Large diffs are not rendered by default.

100 changes: 100 additions & 0 deletions example/icap.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<!doctype>
<html>

<head>
<script type="text/javascript" src="../dist/web3.js"></script>
<script type="text/javascript">

var web3 = require('web3');
var BigNumber = require('bignumber.js');
web3.setProvider(new web3.providers.HttpProvider("http://localhost:8545"));
var from = web3.eth.coinbase;
web3.eth.defaultAccount = from;

window.onload = function () {
var abi = [{
"anonymous" : false,
"inputs" : [
{
"indexed" : true,
"name" : "_from",
"type" : "address"
},
{
"indexed" : false,
"name" : "_id",
"type" : "bytes32"
},
{
"indexed" : false,
"name" : "_value",
"type" : "uint256"
}
],
"name" : "Deposit",
"type" : "event"
}];

var ExchangeInterface = web3.eth.contract(abi).at(web3.eth.namereg.addr('XREG'));
var filter = ExchangeInterface.Deposit({}, {fromBlock: 0});
filter.watch(function (err, event) {
console.log(event);
displayDeposit("block: " + event.blockNumber + " user: " + event.args._id + "\nvalue : " + event.args._value.toString(10));
});
};

function displayDeposit(text) {
var node = document.createElement('li');
var textnode = document.createTextNode(text);
node.appendChild(textnode);
document.getElementById('deposits').appendChild(node);
}

function validateValue() {
try {
var value = document.getElementById('value').value;
var bnValue = new BigNumber(value);
document.getElementById('valueValidation').innerText = bnValue.toString(10);
} catch (err) {
document.getElementById('valueValidation').innerText = 'Value is incorrect, cannot parse';
}
};

function validateIBAN() {
var iban = document.getElementById('iban').value;
if (!web3.isIBAN(iban)) {
return document.getElementById('ibanValidation').innerText = 'IBAN number is incorrect';
}
var institution = iban.substr(7, 4);
var address = web3.eth.namereg.addr(institution);
document.getElementById('ibanValidation').innerText = 'IBAN number correct, exchange address: ' + address;
};

function transfer() {
var value = new BigNumber(document.getElementById('value').value);
var iban = document.getElementById('iban').value;
web3.eth.icapTransfer(iban, from, value);
};

</script>
</head>
<body>
<h1>ICAP</h1>
<h3>Transfer</h3>
<div>
<text>Destination IBAN (eg. XE81ETHXREGGAVOFYORK): </text>
<input type="text" id="iban" onkeyup='validateIBAN()'></input>
<text id="ibanValidation"></text>
</div>
<div>
<text>Value: </text>
<input type="text" id="value" onkeyup='validateValue()'></input>
<text id="valueValidation"></text>
</div>
<div>
<button id="transfer" type="button" onClick="transfer()">Transfer!</button>
</div>
<h3>"XREG" Exchange deposits</h3>
<ul id="deposits"></ul>
</body>
</html>
102 changes: 102 additions & 0 deletions example/namereg.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<!doctype>
<html>

<head>
<script type="text/javascript" src="../dist/web3.js"></script>
<script type="text/javascript">

var web3 = require('web3');
web3.setProvider(new web3.providers.HttpProvider("http://localhost:8545"));
var from = web3.eth.coinbase;
web3.eth.defaultAccount = from;

window.onload = function () {
var filter = web3.eth.namereg.Changed();
filter.watch(function (err, event) {
// live update all fields
onAddressKeyUp();
onNameKeyUp();
onRegisterOwnerKeyUp();
});
};

function registerOwner() {
var name = document.getElementById('registerOwner').value;
web3.eth.namereg.reserve(name);
document.getElementById('nameAvailability').innerText += ' Registering name in progress, please wait...';
};

function changeAddress() {
var name = document.getElementById('registerOwner').value;
var address = document.getElementById('newAddress').value;
web3.eth.namereg.setAddress(name, address, true);
document.getElementById('currentAddress').innerText += ' Changing address in progress. Please wait.';
};

function onRegisterOwnerKeyUp() {
var name = document.getElementById('registerOwner').value;
var owner = web3.eth.namereg.owner(name)
document.getElementById('currentAddress').innerText = web3.eth.namereg.addr(name);
if (owner !== '0x0000000000000000000000000000000000000000') {
if (owner === from) {
document.getElementById('nameAvailability').innerText = "This name is already owned by you " + owner;
} else {
document.getElementById('nameAvailability').innerText = "This name is not available. It's already registered by " + owner;
}
return;
}
document.getElementById('nameAvailability').innerText = "This name is available. You can register it.";
};

function onAddressKeyUp() {
var address = document.getElementById('address').value;
document.getElementById('nameOf').innerText = web3.eth.namereg.name(address);
};

function onNameKeyUp() {
var name = document.getElementById('name').value;
document.getElementById('addressOf').innerText = web3.eth.namereg.addr(name);
};

</script>
</head>
<body>
<i>This example shows only part of namereg functionalities. Namereg contract is available <a href="https://github.com/ethereum/dapp-bin/blob/master/GlobalRegistrar/contract.sol">here</a>
</i>
<h1>Namereg</h1>
<h3>Search for name</h3>
<div>
<text>Address: </text>
<input type="text" id="address" onkeyup='onAddressKeyUp()'></input>
<text>Name: </text>
<text id="nameOf"></text>
</div>
<h3>Search for address</h3>
<div>
<text>Name: </text>
<input type="text" id="name" onkeyup='onNameKeyUp()'></input>
<text>Address: </text>
<text id="addressOf"></text>
</div>
<h3>Register name</h3>
<div>
<text>Check if name is available: </text>
<input type="text" id="registerOwner" onkeyup='onRegisterOwnerKeyUp()'></input>
<text id='nameAvailability'></text>
</div>
<div>
<button id="registerOwnerButton" type="button" onClick="registerOwner()">Register!</button>
</div>
<h3></h3>
<i>If you own the name, you can also change the address it points to</i>
<div>
<text>Address: </text>
<input type="text" id="newAddress"></input>
<button id="changeAddress" type="button" onClick="changeAddress()">Change address!</button>
<text>Current address :</text>
<text id="currentAddress"></text>
</div>

</body>
</html>

2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ var web3 = require('./lib/web3');
web3.providers.HttpProvider = require('./lib/web3/httpprovider');
web3.providers.QtSyncProvider = require('./lib/web3/qtsync');
web3.eth.contract = require('./lib/web3/contract');
web3.eth.namereg = require('./lib/web3/namereg');
web3.eth.icapTransfer = require('./lib/web3/transfer');

// dont override global variable
if (typeof window !== 'undefined' && typeof window.web3 === 'undefined') {
Expand Down
15 changes: 14 additions & 1 deletion lib/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,18 @@ var isJson = function (str) {
}
};

/**
* This method should be called to check if string is valid ethereum IBAN number
* Supports direct and indirect IBANs
*
* @method isIBAN
* @param {String}
* @return {Boolean}
*/
var isIBAN = function (iban) {
return /^XE[0-9]{2}(ETH[0-9A-Z]{13}|[0-9A-Z]{30})$/.test(iban);
};

module.exports = {
padLeft: padLeft,
toHex: toHex,
Expand All @@ -470,6 +482,7 @@ module.exports = {
isObject: isObject,
isBoolean: isBoolean,
isArray: isArray,
isJson: isJson
isJson: isJson,
isIBAN: isIBAN
};

1 change: 1 addition & 0 deletions lib/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ web3.toBigNumber = utils.toBigNumber;
web3.toWei = utils.toWei;
web3.fromWei = utils.fromWei;
web3.isAddress = utils.isAddress;
web3.isIBAN = utils.isIBAN;
web3.sha3 = sha3;
web3.createBatch = function () {
return new Batch();
Expand Down
4 changes: 2 additions & 2 deletions lib/web3/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ SolidityFunction.prototype.unpackOutput = function (output) {
* @return {String} output bytes
*/
SolidityFunction.prototype.call = function () {
var args = Array.prototype.slice.call(arguments);
var args = Array.prototype.slice.call(arguments).filter(function (a) {return a !== undefined; });
var callback = this.extractCallback(args);
var payload = this.toPayload(args);

Expand All @@ -117,7 +117,7 @@ SolidityFunction.prototype.call = function () {
* @param {Object} options
*/
SolidityFunction.prototype.sendTransaction = function () {
var args = Array.prototype.slice.call(arguments);
var args = Array.prototype.slice.call(arguments).filter(function (a) {return a !== undefined; });
var callback = this.extractCallback(args);
var payload = this.toPayload(args);

Expand Down
108 changes: 108 additions & 0 deletions lib/web3/icap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
This file is part of ethereum.js.
ethereum.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ethereum.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file icap.js
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2015
*/

var utils = require('../utils/utils');

/**
* This prototype should be used to extract necessary information from iban address
*
* @param {String} iban
*/
var ICAP = function (iban) {
this._iban = iban;
};

/**
* Should be called to check if icap is correct
*
* @method isValid
* @returns {Boolean} true if it is, otherwise false
*/
ICAP.prototype.isValid = function () {
return utils.isIBAN(this._iban);
};

/**
* Should be called to check if iban number is direct
*
* @method isDirect
* @returns {Boolean} true if it is, otherwise false
*/
ICAP.prototype.isDirect = function () {
return this._iban.length === 34;
};

/**
* Should be called to check if iban number if indirect
*
* @method isIndirect
* @returns {Boolean} true if it is, otherwise false
*/
ICAP.prototype.isIndirect = function () {
return this._iban.length === 20;
};

/**
* Should be called to get iban checksum
* Uses the mod-97-10 checksumming protocol (ISO/IEC 7064:2003)
*
* @method checksum
* @returns {String} checksum
*/
ICAP.prototype.checksum = function () {
return this._iban.substr(2, 2);
};

/**
* Should be called to get institution identifier
* eg. XREG
*
* @method institution
* @returns {String} institution identifier
*/
ICAP.prototype.institution = function () {
return this.isIndirect() ? this._iban.substr(7, 4) : '';
};

/**
* Should be called to get client identifier within institution
* eg. GAVOFYORK
*
* @method client
* @returns {String} client identifier
*/
ICAP.prototype.client = function () {
return this.isIndirect() ? this._iban.substr(11) : '';
};

/**
* Should be called to get client direct address
*
* @method address
* @returns {String} client direct address
*/
ICAP.prototype.address = function () {
return this.isDirect() ? this._iban.substr(4) : '';
};

module.exports = ICAP;

Loading

0 comments on commit ba97d67

Please sign in to comment.