Wrapped Bitcoin and Base58 in platform-neutral closures.

This commit is contained in:
Stefan Thomas 2011-09-26 21:28:13 +01:00
parent fa97237a96
commit 7715c41925
2 changed files with 146 additions and 133 deletions

View file

@ -1,5 +1,5 @@
(function () {
var B58 = Bitcoin.Base58 = {
(function (Bitcoin) {
Bitcoin.Base58 = {
alphabet: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",
base: BigInteger.valueOf(58),
@ -61,4 +61,8 @@
return bytes;
}
};
})();
var B58 = Bitcoin.Base58;
})(
'undefined' != typeof Bitcoin ? Bitcoin : module.exports
);

View file

@ -1,8 +1,16 @@
var Bitcoin = {};
(function (exports) {
var Bitcoin = exports;
if ('object' !== typeof module) {
Bitcoin.EventEmitter = EventEmitter;
}
})(
'object' === typeof module ? module.exports : (window.Bitcoin = {})
);
function makeKeypair()
{
/*
function makeKeypair()
{
// Generate private key
var n = ecparams.getN();
var n1 = n.subtract(BigInteger.ONE);
@ -15,10 +23,10 @@ function makeKeypair()
var publicPoint = G.multiply(privateKey);
return {priv: privateKey, pubkey: publicPoint};
};
};
function serializeTransaction(tx)
{
function serializeTransaction(tx)
{
var buffer = [];
buffer = buffer.concat(Crypto.util.wordsToBytes([parseInt(tx.version)]));
buffer = buffer.concat(numToVarInt(tx.ins.length));
@ -44,17 +52,17 @@ function serializeTransaction(tx)
buffer = buffer.concat(Crypto.util.wordsToBytes([parseInt(tx.lock_time)]));
return buffer;
};
};
var OP_CODESEPARATOR = 171;
var OP_CODESEPARATOR = 171;
var SIGHASH_ALL = 1;
var SIGHASH_NONE = 2;
var SIGHASH_SINGLE = 3;
var SIGHASH_ANYONECANPAY = 80;
var SIGHASH_ALL = 1;
var SIGHASH_NONE = 2;
var SIGHASH_SINGLE = 3;
var SIGHASH_ANYONECANPAY = 80;
function hashTransactionForSignature(scriptCode, tx, inIndex, hashType)
{
function hashTransactionForSignature(scriptCode, tx, inIndex, hashType)
{
// TODO: We need to actually deep copy here
var txTmp = tx;
@ -94,15 +102,15 @@ function hashTransactionForSignature(scriptCode, tx, inIndex, hashType)
console.log(buffer);
return Crypto.SHA256(Crypto.SHA256(buffer, {asBytes: true}), {asBytes: true});
};
};
function verifyTransactionSignature(tx) {
function verifyTransactionSignature(tx) {
var hash = hashTransactionForSignature([], tx, 0, 0);
return Crypto.util.bytesToHex(hash);
};
};
function numToVarInt(i)
{
function numToVarInt(i)
{
// TODO: THIS IS TOTALLY UNTESTED!
if (i < 0xfd) {
// unsigned char
@ -117,9 +125,9 @@ function numToVarInt(i)
// unsigned long long (LE)
return [0xff].concat(Crypto.util.wordsToBytes([i >>> 32, i]));
}
};
};
var testTx = {
var testTx = {
"version":"1",
"lock_time":"0",
"block": {
@ -143,10 +151,10 @@ var testTx = {
"value":"25937000000",
"script":"dqkUQ82gJ0O5vOBg6yK5/yorLLV5zLKIrA=="
}]
};
};
/* TODO: Make this stuff into test cases ;)
$(function () {
TODO: Make this stuff into test cases ;)
$(function () {
var key = new Bitcoin.ECKey(Crypto.util.hexToBytes("5c0b98e524ad188ddef35dc6abba13c34a351a05409e5d285403718b93336a4a"));
key = new Bitcoin.ECKey(Crypto.util.hexToBytes("180cb41c7c600be951b5d3d0a7334acc7506173875834f7a6c4c786a28fcbb19"));
//console.log(key.getBitcoinAddress().toString());
@ -162,5 +170,6 @@ $(function () {
//console.log(Bitcoin.ECDSA.verify(Crypto.util.hexToBytes("230aba77ccde46bb17fcb0295a92c0cc42a6ea9f439aaadeb0094625f49e6ed8"), Crypto.util.hexToBytes("3046022100a3ee5408f0003d8ef00ff2e0537f54ba09771626ff70dca1f01296b05c510e85022100d4dc70a5bb50685b65833a97e536909a6951dd247a2fdbde6688c33ba6d6407501"),Crypto.util.hexToBytes("04a19c1f07c7a0868d86dbb37510305843cc730eb3bea8a99d92131f44950cecd923788419bfef2f635fad621d753f30d4b4b63b29da44b4f3d92db974537ad5a4")));
//console.log(Bitcoin.ECDSA.verify(Crypto.util.hexToBytes("c2c75bb77d7a5acddceb1d45ceef58e7451fd0d3abc9d4c16df7848eefafe00d"), Crypto.util.hexToBytes("3045022100ff9362dadcbf1f6ef954bc8eb27144bbb4f49abd32be1eb04c311151dcf4bcf802205112c2ca6a25aefb8be98bf460c5a9056c01253f31e118d80b81ec9604e3201a01"),Crypto.util.hexToBytes("04fe62ce7892ec209310c176ef7f06565865e286e8699e884603657efa9aa51086785099d544d4e04f1f7b4b065205c1783fade8daf4ba1e0d1962292e8eb722cd")));
});
//*/
});
//
*/