WIF encode/decode

This commit is contained in:
Kagami Hiiragi 2014-12-29 22:59:18 +03:00
parent 2d1f7ad35a
commit b2b660827c
4 changed files with 100 additions and 4 deletions

View File

@ -60,9 +60,9 @@ With the help of browserify `bitmessage` provides different implementations for
- [ ] Message
- [ ] encode
- [ ] decode
- [ ] WIF
- [ ] encode
- [ ] decode
- [x] WIF
- [x] encode
- [x] decode
- [ ] Parse PyBitmessage configs
- [ ] decode keys.dat
- [ ] decode knownnodes.dat

View File

@ -1,5 +1,5 @@
/**
* Main Bitmessage module. Just reexports all main submodules.
* Bitmessage library entry point. Just reexports common submodules.
* @module bitmessage
*/
@ -9,3 +9,4 @@
// replace it with other library with the same API.
exports.Int64 = require("int64-native");
exports.Address = require("./address");
exports.wif = require("./wif");

56
lib/wif.js Normal file
View File

@ -0,0 +1,56 @@
/**
* Implement WIF encoding/decoding.
* Reference: <https://en.bitcoin.it/wiki/Wallet_import_format>
* @module bitmessage/wif
*/
"use strict";
require("es6-promise").polyfill();
var assert = require("assert");
var bufferEqual = require("buffer-equal");
var bs58 = require("bs58");
var bmcrypto = require("./crypto");
// Compute the WIF checksum for the given data.
function getchecksum(data) {
return bmcrypto.sha256(data).then(bmcrypto.sha256).then(function(dhash) {
return dhash.slice(0, 4);
});
}
/**
* Decode WIF encoded private key.
* @param {string} input - Input data
* @param {Promise.<Buffer,undefined>} A promise than contain private
* key when fulfilled
*/
exports.decode = function(input) {
var bytes;
try {
bytes = bs58.decode(input);
assert(bytes[0] === 0x80, "Bad WIF");
} catch(e) {
return Promise.reject(e);
}
var data = new Buffer(bytes.slice(0, -4));
var checksum = new Buffer(bytes.slice(-4));
return getchecksum(data).then(function(realchecksum) {
assert(bufferEqual(checksum, realchecksum), "Bad checkum");
return data.slice(1);
});
};
/**
* Convert private key to a WIF.
* @param {Buffer} privateKey - A private key to encode
* @return {Promise.<string,undefined>} A promise that contains the
* encoded key when fulfilled
*/
exports.encode = function(privateKey) {
var data = Buffer.concat([new Buffer([0x80]), privateKey]);
return getchecksum(data).then(function(checksum) {
var bytes = Buffer.concat([data, checksum]);
return bs58.encode(bytes);
});
};

39
test.js
View File

@ -6,6 +6,7 @@ var allTests = typeof window === "undefined" ?
var bitmessage = require("./lib");
var Int64 = bitmessage.Int64;
var Address = bitmessage.Address;
var wif = bitmessage.wif;
var varint = require("./lib/varint");
var bmcrypto = require("./lib/crypto");
@ -55,6 +56,44 @@ describe("var_int", function() {
});
});
describe("WIF", function() {
var wifSign = "5JgQ79vTBusc61xYPtUEHYQ38AXKdDZgQ5rFp7Cbb4ZjXUKFZEV";
var wifEnc = "5K2aL8cnsEWHwHfHnUrPo8QdYyRfoYUBmhAnWY5GTpDLbeyusnE";
var signPrivateKey = Buffer("71c95d26c716a5e85e9af9efe26fb5f744dc98005a13d05d23ee92c77e038d9f", "hex");
var encPrivateKey = Buffer("9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex");
it("should decode", function() {
return wif.decode(wifSign)
.then(function(key1) {
expect(Buffer.isBuffer(key1)).to.be.true;
expect(key1.length).to.equal(32);
expect(key1.toString("hex")).to.equal(signPrivateKey.toString("hex"));
return wif.decode(wifEnc).then(function(key2) {
expect(Buffer.isBuffer(key2)).to.be.true;
expect(key2.length).to.equal(32);
expect(key2.toString("hex")).to.equal(encPrivateKey.toString("hex"));
return {
version: 4,
stream: 1,
signPrivateKey: key1,
encPrivateKey: key2,
};
});
}).then(Address.encode).then(function(str) {
expect(str).to.equal("BM-2cTux3PGRqHTEH6wyUP2sWeT4LrsGgy63z");
});
});
it("should encode", function() {
return wif.encode(signPrivateKey).then(function(wif1) {
expect(wif1).to.equal(wifSign);
return wif.encode(encPrivateKey);
}).then(function(wif2) {
expect(wif2).to.equal(wifEnc);
});
});
});
describe("Crypto", function() {
it("should implement SHA-512 hash", function() {
return bmcrypto.sha512(Buffer("test")).then(function(res) {