remove whitespace, add js language identifiers

This commit is contained in:
Heath 2014-11-21 14:52:41 -05:00
parent 3e43d79b6e
commit fe4bbf3d72

View File

@ -1,8 +1,10 @@
#Address.js #Address.js
'use strict';
```js
var run = function() { 'use strict';
var run = function() {
// Replace '../bitcore' with 'bitcore' if you use this code elsewhere. // Replace '../bitcore' with 'bitcore' if you use this code elsewhere.
var bitcore = require('../bitcore'); var bitcore = require('../bitcore');
var Address = bitcore.Address; var Address = bitcore.Address;
@ -21,87 +23,97 @@
console.log(addr.data + ' is ' + (valid ? '' : 'not ') + 'valid'); console.log(addr.data + ' is ' + (valid ? '' : 'not ') + 'valid');
}); });
}; };
module.exports.run = run; module.exports.run = run;
if (require.main === module) { if (require.main === module) {
run(); run();
} }
```
#Armory.js #Armory.js
var Armory = require('../lib/Armory');
var Address = require('../lib/Address');
// Initial public key can be retrieved from paper backup ```js
var PublicX = '9df5 23e7 18b9 1f59 a790 2d46 999f 9357 ccf8 7208 24d4 3076 4516 b809 f7ab ce4e'; var Armory = require('../lib/Armory');
var PublicY = '66ba 5d21 4682 0dae 401d 9506 8437 2516 79f9 0c56 4186 cc50 07df c6d0 6989 1ff4'; var Address = require('../lib/Address');
var pubkey = '04' + PublicX.split(' ').join('') + PublicY.split(' ').join('');
// Chain code can be generated by entering paper backup // Initial public key can be retrieved from paper backup
// on brainwallet.org/#chains or by using Armory.fromSeed() below
var chaincode = '84ac14bc4b388b33da099a0b4ee3b507284d99e1476639e36e5ca5e6af86481e'; var PublicX = '9df5 23e7 18b9 1f59 a790 2d46 999f 9357 ccf8 7208 24d4 3076 4516 b809 f7ab ce4e';
var PublicY = '66ba 5d21 4682 0dae 401d 9506 8437 2516 79f9 0c56 4186 cc50 07df c6d0 6989 1ff4';
var pubkey = '04' + PublicX.split(' ').join('') + PublicY.split(' ').join('');
var armory = new Armory(chaincode, pubkey); // Chain code can be generated by entering paper backup
// on brainwallet.org/#chains or by using Armory.fromSeed() below
console.log('Deriving public keys for'); var chaincode = '84ac14bc4b388b33da099a0b4ee3b507284d99e1476639e36e5ca5e6af86481e';
console.log('------------------------');
console.log('Chain code: %s', chaincode);
console.log('Public key: %s', pubkey);
console.log('');
for (var i = 0; i < 5; i++) { var armory = new Armory(chaincode, pubkey);
console.log('Deriving public keys for');
console.log('------------------------');
console.log('Chain code: %s', chaincode);
console.log('Public key: %s', pubkey);
console.log('');
for (var i = 0; i < 5; i++) {
console.log(Address.fromPubKey(armory.pubkey).as('base58')); console.log(Address.fromPubKey(armory.pubkey).as('base58'));
armory = armory.next(); armory = armory.next();
} }
// Derive first public key and chain code from seed // Derive first public key and chain code from seed
var seed = [ var seed = [
'aagh hjfj sihk ietj giik wwai awtd uodh hnji', 'aagh hjfj sihk ietj giik wwai awtd uodh hnji',
'soss uaku egod utai itos fijj ihgi jhau jtoo' 'soss uaku egod utai itos fijj ihgi jhau jtoo'
]; ];
console.log(''); console.log('');
console.log(''); console.log('');
console.log('Deriving public keys for'); console.log('Deriving public keys for');
console.log('------------------------'); console.log('------------------------');
console.log('Seed: %s', seed.join(' ')); console.log('Seed: %s', seed.join(' '));
console.log(''); console.log('');
// skip first public key // skip first public key
var a = Armory.fromSeed(seed.join('\n')).next(); var a = Armory.fromSeed(seed.join('\n')).next();
for (var i = 0; i < 5; i++) { for (var i = 0; i < 5; i++) {
console.log(Address.fromPubKey(a.pubkey).as('base58')); console.log(Address.fromPubKey(a.pubkey).as('base58'));
a = a.next(); a = a.next();
} }
var mpk = '045a09a3286873a72f164476bde9d1d8e5c2bc044e35aa47eb6e798e325a86417f7c35b61d9905053533e0b4f2a26eca0330aadf21c638969e45aaace50e4c0c8784ac14bc4b388b33da099a0b4ee3b507284d99e1476639e36e5ca5e6af86481e'; var mpk = '045a09a3286873a72f164476bde9d1d8e5c2bc044e35aa47eb6e798e325a86417f7c35b61d9905053533e0b4f2a26eca0330aadf21c638969e45aaace50e4c0c8784ac14bc4b388b33da099a0b4ee3b507284d99e1476639e36e5ca5e6af86481e';
console.log(''); console.log('');
console.log(''); console.log('');
console.log('Deriving public keys for'); console.log('Deriving public keys for');
console.log('------------------------'); console.log('------------------------');
console.log('Master Public Key: %s', mpk); console.log('Master Public Key: %s', mpk);
console.log(''); console.log('');
// skip first public key // skip first public key
var b = Armory.fromMasterPublicKey(mpk).next(); var b = Armory.fromMasterPublicKey(mpk).next();
for (var i = 0; i < 5; i++) { for (var i = 0; i < 5; i++) {
console.log(Address.fromPubKey(b.pubkey).as('base58')); console.log(Address.fromPubKey(b.pubkey).as('base58'));
b = b.next(); b = b.next();
} }
```
#ConnectionTor.js #ConnectionTor.js
var Peer = require('../lib/Peer');
var Connection = require('../lib/Connection');
var dns = require('dns');
// get a peer from dns seed ```js
dns.resolve('dnsseed.bluematt.me', function(err, seeds) {
var Peer = require('../lib/Peer');
var Connection = require('../lib/Connection');
var dns = require('dns');
// get a peer from dns seed
dns.resolve('dnsseed.bluematt.me', function(err, seeds) {
// use the first peer // use the first peer
var peer = new Peer(seeds[0], 8333); var peer = new Peer(seeds[0], 8333);
@ -132,10 +144,15 @@
//console.log(err); //console.log(err);
}); });
}); });
```
#CreateAndSignTx-Multisig.js #CreateAndSignTx-Multisig.js
var run = function() {
```js
var run = function() {
bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore; bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore;
var networks = require('../networks'); var networks = require('../networks');
var WalletKey = bitcore.WalletKey; var WalletKey = bitcore.WalletKey;
@ -228,22 +245,26 @@
console.log('[this example originally generated TXID: 1eb388977b2de99562eb0fbcc661a100eaffed99c53bfcfebe5a087002039b83 on testnet]\n\n\thttp://test.bitcore.io/tx/1eb388977b2de99562eb0fbcc661a100eaffed99c53bfcfebe5a087002039b83'); console.log('[this example originally generated TXID: 1eb388977b2de99562eb0fbcc661a100eaffed99c53bfcfebe5a087002039b83 on testnet]\n\n\thttp://test.bitcore.io/tx/1eb388977b2de99562eb0fbcc661a100eaffed99c53bfcfebe5a087002039b83');
}; };
// This is just for browser & mocha compatibility // This is just for browser & mocha compatibility
if (typeof module !== 'undefined') { if (typeof module !== 'undefined') {
module.exports.run = run; module.exports.run = run;
if (require.main === module) { if (require.main === module) {
run(); run();
} }
} else { } else {
run(); run();
} }
////
//// ```
#CreateAndSignTx-PayToPubkeyHash.js #CreateAndSignTx-PayToPubkeyHash.js
var run = function() {
```js
var run = function() {
bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore; bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore;
var priv = 'cTgGUrcro89yUtKeG6gHBAS14r3qp25KwTTxG9d4kEzcFxecuZDm'; var priv = 'cTgGUrcro89yUtKeG6gHBAS14r3qp25KwTTxG9d4kEzcFxecuZDm';
@ -302,22 +323,26 @@
var txHex = tx.serialize().toString('hex'); var txHex = tx.serialize().toString('hex');
console.log('TX HEX IS: ', txHex); console.log('TX HEX IS: ', txHex);
}; };
// This is just for browser & mocha compatibility // This is just for browser & mocha compatibility
if (typeof module !== 'undefined') { if (typeof module !== 'undefined') {
module.exports.run = run; module.exports.run = run;
if (require.main === module) { if (require.main === module) {
run(); run();
} }
} else { } else {
run(); run();
} }
////
//// ```
#CreateAndSignTx-PayToScriptHash.js #CreateAndSignTx-PayToScriptHash.js
var run = function() {
```js
var run = function() {
bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore; bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore;
var networks = require('../networks'); var networks = require('../networks');
var WalletKey = bitcore.WalletKey; var WalletKey = bitcore.WalletKey;
@ -451,28 +476,31 @@
console.log('ret', ret); //TODO console.log('ret', ret); //TODO
process.exit(-1); process.exit(-1);
}); });
}; };
*/ */
}; };
// This is just for browser & mocha compatibility // This is just for browser & mocha compatibility
if (typeof module !== 'undefined') { if (typeof module !== 'undefined') {
module.exports.run = run; module.exports.run = run;
if (require.main === module) { if (require.main === module) {
run(); run();
} }
} else { } else {
run(); run();
} }
```
#CreateKey.js #CreateKey.js
'use strict';
```js
'use strict';
var run = function() { var run = function() {
// replace '../bitcore' with 'bitcore' if you use this code elsewhere. // replace '../bitcore' with 'bitcore' if you use this code elsewhere.
var bitcore = require('../bitcore'); var bitcore = require('../bitcore');
var networks = require('../networks'); var networks = require('../networks');
@ -510,17 +538,22 @@
print(wk2); print(wk2);
}; };
module.exports.run = run; module.exports.run = run;
if (require.main === module) { if (require.main === module) {
run(); run();
} }
```
#CreateScript.js #CreateScript.js
'use strict';
var run = function() { ```js
'use strict';
var run = function() {
// replace '../bitcore' with 'bitcore' if you use this code elsewhere. // replace '../bitcore' with 'bitcore' if you use this code elsewhere.
var bitcore = require('../bitcore'); var bitcore = require('../bitcore');
var networks = require('../networks'); var networks = require('../networks');
@ -588,15 +621,20 @@
var a = new Address(networks.livenet.P2SHVersion, hash); var a = new Address(networks.livenet.P2SHVersion, hash);
p('\tp2sh Addr: ' + a.toString()); p('\tp2sh Addr: ' + a.toString());
}; };
module.exports.run = run; module.exports.run = run;
if (require.main === module) { if (require.main === module) {
run(); run();
} }
```
#ECIES.js #ECIES.js
var run = function() {
```js
var run = function() {
bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore; bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore;
console.log('ECIES: Elliptic Curve Integrated Encryption Scheme'); console.log('ECIES: Elliptic Curve Integrated Encryption Scheme');
@ -614,34 +652,44 @@
var decrypted = bitcore.ECIES.decrypt(key.private, encrypted); var decrypted = bitcore.ECIES.decrypt(key.private, encrypted);
console.log('Decrypted (with private key): "' + decrypted.toString() + '"'); console.log('Decrypted (with private key): "' + decrypted.toString() + '"');
}; };
// This is just for browser & mocha compatibility // This is just for browser & mocha compatibility
if (typeof module !== 'undefined') { if (typeof module !== 'undefined') {
module.exports.run = run; module.exports.run = run;
if (require.main === module) { if (require.main === module) {
run(); run();
} }
} else { } else {
run(); run();
} }
```
#ElectrumMPK.js #ElectrumMPK.js
var Electrum = require('../lib/Electrum');
var Address = require('../lib/Address');
var mpk = '92eea4d2f5263651db9e3222caded1fd4c89772f79a7c03fb6afc00e9d2c9d2ed9b86c2c95fc1171e49163079dacb7f048b3c509a27a490e1df9e7128362d468'; ```js
mpk = new Electrum(mpk); var Electrum = require('../lib/Electrum');
var Address = require('../lib/Address');
var key0 = mpk.generatePubKey(0); var mpk = '92eea4d2f5263651db9e3222caded1fd4c89772f79a7c03fb6afc00e9d2c9d2ed9b86c2c95fc1171e49163079dacb7f048b3c509a27a490e1df9e7128362d468';
var addr0 = Address.fromPubKey(key0);
console.log(addr0.as('base58')); mpk = new Electrum(mpk);
var key0 = mpk.generatePubKey(0);
var addr0 = Address.fromPubKey(key0);
console.log(addr0.as('base58'));
```
#HierarchicalKey.js #HierarchicalKey.js
var run = function() {
```js
var run = function() {
bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore; bitcore = typeof(bitcore) === 'undefined' ? require('../bitcore') : bitcore;
var HierarchicalKey = bitcore.HierarchicalKey; var HierarchicalKey = bitcore.HierarchicalKey;
var Address = bitcore.Address; var Address = bitcore.Address;
@ -712,50 +760,65 @@
console.log('m/100 address: ' + Address.fromPubKey(hkey.derive('m/100').eckey.public).toString()); console.log('m/100 address: ' + Address.fromPubKey(hkey.derive('m/100').eckey.public).toString());
console.log(); console.log();
}; };
// This is just for browser & mocha compatibility // This is just for browser & mocha compatibility
if (typeof module !== 'undefined') { if (typeof module !== 'undefined') {
module.exports.run = run; module.exports.run = run;
if (require.main === module) { if (require.main === module) {
run(); run();
} }
} else { } else {
run(); run();
} }
```
#PayToScriptHashAddress.js #PayToScriptHashAddress.js
var bitcore = require('../bitcore');
var Address = bitcore.Address; ```js
var bitcoreUtil = bitcore.util;
var Script = bitcore.Script; var bitcore = require('../bitcore');
var network = bitcore.networks.livenet; var Address = bitcore.Address;
var bitcoreUtil = bitcore.util;
var Script = bitcore.Script;
var network = bitcore.networks.livenet;
var script = ''; // write down your script here var script = ''; // write down your script here
var s = Script.fromHumanReadable(script); var s = Script.fromHumanReadable(script);
var hash = bitcoreUtil.sha256ripe160(s.getBuffer()); var hash = bitcoreUtil.sha256ripe160(s.getBuffer());
var version = network.addressScript; var version = network.addressScript;
var addr = new Address(version, hash); var addr = new Address(version, hash);
var addrStr = addr.as('base58'); var addrStr = addr.as('base58');
// This outputs the "address" of thescript // This outputs the "address" of thescript
console.log(addrStr); console.log(addrStr);
```
#PeerDiscovery.js #PeerDiscovery.js
var PeerManager = require('../lib/PeerManager');
var peerman = new PeerManager();
peerman.discover({ ```js
var PeerManager = require('../lib/PeerManager');
var peerman = new PeerManager();
peerman.discover({
limit: 12 limit: 12
}).start(); }).start();
```
#PeerManager.js #PeerManager.js
'use strict';
var run = function() { ```js
'use strict';
var run = function() {
// Replace '../bitcore' with 'bitcore' if you use this code elsewhere. // Replace '../bitcore' with 'bitcore' if you use this code elsewhere.
var bitcore = require('../bitcore'); var bitcore = require('../bitcore');
var Peer = bitcore.Peer; var Peer = bitcore.Peer;
@ -794,17 +857,22 @@
}); });
peerman.start(); peerman.start();
}; };
module.exports.run = run; module.exports.run = run;
if (require.main === module) { if (require.main === module) {
run(); run();
} }
```
#Rpc.js #Rpc.js
'use strict';
var run = function() { ```js
'use strict';
var run = function() {
// Replace '../bitcore' with 'bitcore' if you use this code elsewhere. // Replace '../bitcore' with 'bitcore' if you use this code elsewhere.
var bitcore = require('../bitcore'); var bitcore = require('../bitcore');
var RpcClient = bitcore.RpcClient; var RpcClient = bitcore.RpcClient;
@ -828,17 +896,22 @@
} }
console.log(ret); console.log(ret);
}); });
}; };
module.exports.run = run; module.exports.run = run;
if (require.main === module) { if (require.main === module) {
run(); run();
} }
```
#Script.js #Script.js
'use strict';
var run = function() { ```js
'use strict';
var run = function() {
// Replace '../bitcore' with 'bitcore' if you use this code elsewhere. // Replace '../bitcore' with 'bitcore' if you use this code elsewhere.
var bitcore = require('../bitcore'); var bitcore = require('../bitcore');
var Address = bitcore.Address; var Address = bitcore.Address;
@ -882,17 +955,22 @@
var script = 'DUP HASH160 0x14 0x3744841e13b90b4aca16fe793a7f88da3a23cc71 EQUALVERIFY CHECKSIG'; var script = 'DUP HASH160 0x14 0x3744841e13b90b4aca16fe793a7f88da3a23cc71 EQUALVERIFY CHECKSIG';
var s = Script.fromHumanReadable(script); var s = Script.fromHumanReadable(script);
console.log(getAddrStr(s)[0]); // mkZBYBiq6DNoQEKakpMJegyDbw2YiNQnHT console.log(getAddrStr(s)[0]); // mkZBYBiq6DNoQEKakpMJegyDbw2YiNQnHT
}; };
module.exports.run = run; module.exports.run = run;
if (require.main === module) { if (require.main === module) {
run(); run();
} }
```
#SendTx.js #SendTx.js
'use strict';
var run = function() { ```js
'use strict';
var run = function() {
// Replace '../bitcore' with 'bitcore' if you use this code elsewhere. // Replace '../bitcore' with 'bitcore' if you use this code elsewhere.
var bitcore = require('../bitcore'); var bitcore = require('../bitcore');
var Peer = bitcore.Peer; var Peer = bitcore.Peer;
@ -982,7 +1060,7 @@
// finally, send transaction to the bitcoin network // finally, send transaction to the bitcoin network
conn.sendTx(tx); conn.sendTx(tx);
// for now, the network won't respond in any case // for now, the network will not respond in any case
// (transaction accepted, transaction rejected) // (transaction accepted, transaction rejected)
// in the future, we may listen to 'reject' message // in the future, we may listen to 'reject' message
// see https://gist.github.com/gavinandresen/7079034 // see https://gist.github.com/gavinandresen/7079034
@ -991,54 +1069,59 @@
peerman.start(); peerman.start();
}; };
module.exports.run = run; module.exports.run = run;
if (require.main === module) { if (require.main === module) {
run(); run();
} }
```
#SimpleP2Pmonitor.js #SimpleP2Pmonitor.js
/**
```js
/**
* This is a simple script that will display network messages. * This is a simple script that will display network messages.
* It users the Peer / Connection classes directly instead of * It users the Peer / Connection classes directly instead of
* relying on PeerManager. * relying on PeerManager.
*/ */
// replace by require('bitcore') if you use somewhere else // replace by require('bitcore') if you use somewhere else
var bitcore = require('../'); var bitcore = require('../');
//bitcore.config.logger = 'debug'; //bitcore.config.logger = 'debug';
var Peer = bitcore.Peer, var Peer = bitcore.Peer,
Connection = bitcore.Connection; Connection = bitcore.Connection;
var peer = new Peer('127.0.0.1', 8333); var peer = new Peer('127.0.0.1', 8333);
var socket = peer.createConnection(); var socket = peer.createConnection();
var con = new Connection(socket, peer); var con = new Connection(socket, peer);
con.on('error', function(msg) { con.on('error', function(msg) {
var peer = msg.peer, var peer = msg.peer,
err = msg.err; err = msg.err;
console.error('Error connecting to peer', peer.host + ':' + peer.port, '(' + err.message + ')'); console.error('Error connecting to peer', peer.host + ':' + peer.port, '(' + err.message + ')');
}); });
con.on('disconnect', function(msg) { con.on('disconnect', function(msg) {
console.log('disconnect: ', msg); console.log('disconnect: ', msg);
}); });
con.on('connect', function(msg) { con.on('connect', function(msg) {
console.log('Connected to %s', msg.peer.host + ':' + msg.peer.port); console.log('Connected to %s', msg.peer.host + ':' + msg.peer.port);
}); });
/* Listen P2P messages */ /* Listen P2P messages */
// Make a log function available to all listeners // Make a log function available to all listeners
// The log function is just like console.log except it prefixes // The log function is just like console.log except it prefixes
// messages with [host:port] // messages with [host:port]
function listen(event_name, fn) { function listen(event_name, fn) {
con.on(event_name, function(event) { con.on(event_name, function(event) {
fn(event, function() { fn(event, function() {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
@ -1048,35 +1131,39 @@
console.log.apply(console, args); console.log.apply(console, args);
}); });
}); });
} }
listen('getaddr', function(event, log) { listen('getaddr', function(event, log) {
log('Received message getaddr'); log('Received message getaddr');
log(event); log(event);
}); });
listen('verack', function(event, log) { listen('verack', function(event, log) {
log('Received message verack'); log('Received message verack');
}); });
listen('version', function(event, log) { listen('version', function(event, log) {
log('Received message version (%s)', event.message.version); log('Received message version (%s)', event.message.version);
}); });
listen('addr', function(event, log) { listen('addr', function(event, log) {
log('Received message addr (%s addresses)', event.message.addrs.length); log('Received message addr (%s addresses)', event.message.addrs.length);
}); });
listen('inv', function(event, log) { listen('inv', function(event, log) {
log('Received message inv (%s invs)', event.message.count); log('Received message inv (%s invs)', event.message.count);
console.log(event.message.invs); console.log(event.message.invs);
}); });
```
#VanityAddress.js #VanityAddress.js
'use strict';
```js
var run = function() { 'use strict';
var run = function() {
// Replace '../bitcore' with 'bitcore' if you use this code elsewhere. // Replace '../bitcore' with 'bitcore' if you use this code elsewhere.
var bitcore = require('../bitcore'); var bitcore = require('../bitcore');
var Key = bitcore.Key; var Key = bitcore.Key;
@ -1095,10 +1182,11 @@
console.log('Address: ' + a.toString()); console.log('Address: ' + a.toString());
console.log('Private Key: ' + k.private.toString('hex')); console.log('Private Key: ' + k.private.toString('hex'));
}; };
module.exports.run = run; module.exports.run = run;
if (require.main === module) { if (require.main === module) {
run(); run();
} }
```