Merge pull request #53 from maraoz/feature/browserify-Script

browserify Script
This commit is contained in:
Jeff Garzik 2014-02-17 21:56:49 -05:00
commit 6556988920
11 changed files with 185 additions and 15 deletions

View File

@ -7,7 +7,14 @@ module.exports.bignum = require('bignum');
module.exports.base58 = require('base58-native');
module.exports.EncodedData = require('./util/EncodedData');
module.exports.VersionedData = require('./util/VersionedData');
module.exports.Address= require('./Address');
module.exports.Address = require('./Address');
module.exports.config = require('./config');
module.exports.log = require('./util/log');
module.exports.Opcode = require('./Opcode');
module.exports.util = require('./util/util');
module.exports.Script = require('./Script');
module.exports.SINKey = require('./SINKey');
//module.exports.Transaction = require('./Transaction');
if (typeof process.versions === 'undefined') {

View File

@ -1,5 +1,5 @@
// load modules needed for testing in the browser
var fs = require('fs');
//var fs = require('fs');

View File

@ -1,7 +0,0 @@
var SINKey = require('./SINKey').class();
var sk = new SINKey();
sk.generate();
console.dir(sk.storeObj());

View File

@ -20,6 +20,9 @@
<script src="test.EncodedData.js"></script>
<script src="test.VersionedData.js"></script>
<script src="test.Address.js"></script>
<script src="test.Opcode.js"></script>
<script src="test.Script.js"></script>
<script src="test.misc.js"></script>
<script>
mocha.run();
</script>

View File

@ -16,14 +16,14 @@ describe('Address', function() {
Address = AddressModule.class();
should.exist(Address);
});
it('should be able to create Address object', function() {
it('should be able to create instance', function() {
var a = new Address('1KfyjCgBSMsLqiCbakfSdeoBUqMqLUiu3T');
should.exist(a);
});
it('should validate correctly', function() {
var a = new Address("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa");
var m = new Address("32QBdjycLwbDTuGafUwaU5p5GxzSLPYoF6");
var b = new Address("11111111111111111111111111122222234");
var a = new Address('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa');
var m = new Address('32QBdjycLwbDTuGafUwaU5p5GxzSLPYoF6');
var b = new Address('11111111111111111111111111122222234');
a.validate.bind(a).should.not.throw(Error);
m.validate.bind(m).should.not.throw(Error);
b.validate.bind(b).should.throw(Error);

43
test/test.Opcode.js Normal file
View File

@ -0,0 +1,43 @@
'use strict';
var chai = require('chai');
var bitcore = require('../bitcore');
var should = chai.should();
var OpcodeModule = bitcore.Opcode;
var Opcode;
describe('Opcode', function() {
it('should initialze the main object', function() {
should.exist(OpcodeModule);
});
it('should be able to create class', function() {
Opcode = OpcodeModule.class();
should.exist(Opcode);
});
it('should be able to create instance', function() {
var oc = new Opcode();
should.exist(oc);
});
it.skip('should be able to create some constants', function() {
// TODO: test works in node but not in browser
for (var i in Opcode.map) {
console.log('var '+i + ' = ' + Opcode.map[i] + ';');
eval('var '+i + ' = ' + Opcode.map[i] + ';');
console.log(eval(i));
}
should.exist(OP_VER);
should.exist(OP_HASH160);
should.exist(OP_RETURN);
should.exist(OP_EQUALVERIFY);
should.exist(OP_CHECKSIG);
should.exist(OP_CHECKMULTISIG);
});
});

30
test/test.SIN.js Normal file
View File

@ -0,0 +1,30 @@
'use strict';
var chai = require('chai');
var bitcore = require('../bitcore');
var should = chai.should();
var SINKeyModule = bitcore.SINKey;
var SINKey;
describe('SINKey', function() {
it('should initialze the main object', function() {
should.exist(SINKeyModule);
});
it('should be able to create class', function() {
SINKey = SINKeyModule.class();
should.exist(SINKey);
});
it('should be able to create instance', function() {
var sk = new SINKey();
sk.generate();
should.exist(sk.created);
should.exist(sk.privKey.private);
should.exist(sk.privKey.public);
should.exist(sk.privKey.compressed);
});
});

40
test/test.Script.js Normal file
View File

@ -0,0 +1,40 @@
'use strict';
var chai = require('chai');
var bitcore = require('../bitcore');
var should = chai.should();
var ScriptModule = bitcore.Script;
var Address = bitcore.Address.class();
var Script;
describe('Script', function() {
it('should initialze the main object', function() {
should.exist(ScriptModule);
});
it('should be able to create class', function() {
Script = ScriptModule.class();
should.exist(Script);
});
it('should be able to create instance', function() {
var s = new Script();
should.exist(s);
});
it('should be able to create Script from Address', function() {
var addr = new Address('1J57QmkaQ6JohJoQyaUJwngJ2vTQ3C6gHi');
var script = Script.createPubKeyHashOut(addr.payload());
should.exist(script);
script.isPubkeyHash().should.be.true;
});
it('isP2SH should work', function() {
var addr = new Address('1J57QmkaQ6JohJoQyaUJwngJ2vTQ3C6gHi');
var script = Script.createPubKeyHashOut(addr.payload());
script.isP2SH().should.be.false;
});
});

28
test/test.Transaction.js Normal file
View File

@ -0,0 +1,28 @@
'use strict';
var chai = require('chai');
var bitcore = require('../bitcore');
var should = chai.should();
var TransactionModule = bitcore.Transaction;
var Transaction;
describe.skip('Transaction', function() {
it('should initialze the main object', function() {
should.exist(TransactionModule);
});
it('should be able to create class', function() {
Transaction = TransactionModule.class();
should.exist(Transaction);
});
it('should be able to create instance', function() {
var t = new Transaction();
should.exist(t);
});
});

24
test/test.misc.js Normal file
View File

@ -0,0 +1,24 @@
'use strict';
var chai = require('chai');
var bitcore = require('../bitcore');
var should = chai.should();
describe('Miscelaneous stuff', function() {
it('should initialze the config object', function() {
should.exist(bitcore.config);
});
it('should initialze the log object', function() {
should.exist(bitcore.log);
});
it('should initialze the util object', function() {
should.exist(bitcore.util);
});
});

View File

@ -1,4 +1,5 @@
require('buffertools');
'use strict';
var crypto = require('crypto');
var bignum = require('bignum');
var Binary = require('binary');
@ -327,7 +328,8 @@ var varStrBuf = exports.varStrBuf = function varStrBuf(s) {
exports.NULL_HASH = new Buffer(32).fill(0);
exports.EMPTY_BUFFER = new Buffer(0);
exports.ZERO_VALUE = new Buffer(8).fill(0);
INT64_MAX = new Buffer('ffffffffffffffff', 'hex');
var INT64_MAX = new Buffer('ffffffffffffffff', 'hex');
exports.INT64_MAX = INT64_MAX;
// How much of Bitcoin's internal integer coin representation
// makes 1 BTC