bitcoinjs-lib/test/convert.js

64 lines
1.8 KiB
JavaScript
Raw Normal View History

2014-03-31 05:47:47 +02:00
var assert = require('assert')
2014-04-24 00:44:59 +02:00
var convert = require('../').convert
describe('convert', function() {
2014-03-31 05:47:47 +02:00
describe('bytesToHex', function() {
it('handles example 1', function() {
assert.equal(convert.bytesToHex([0, 1, 2, 255]), '000102ff')
})
2014-03-31 05:47:47 +02:00
})
2014-03-31 05:47:47 +02:00
describe('hexToBytes', function() {
it('handles example 1', function() {
assert.deepEqual(convert.hexToBytes('000102ff'), [0, 1, 2, 255])
})
2014-03-31 05:47:47 +02:00
})
it('converts from bytes to hex and back', function() {
var bytes = []
for (var i=0 ; i<256 ; ++i) {
bytes.push(i)
}
var hex = convert.bytesToHex(bytes)
assert.equal(hex.length, 512)
assert.deepEqual(convert.hexToBytes(hex), bytes)
})
describe('byte array and word array conversions', function(){
var bytes, wordArray
beforeEach(function(){
bytes = [
98, 233, 7, 177, 92, 191, 39, 213, 66, 83,
153, 235, 246, 240, 251, 80, 235, 184, 143, 24
]
wordArray = {
words: [1659439025, 1556031445, 1112775147, -151979184, -340226280],
sigBytes: 20
}
})
2014-03-31 05:47:47 +02:00
describe('bytesToWords', function() {
it('works', function() {
assert.deepEqual(convert.bytesToWordArray(bytes), wordArray)
})
})
2014-03-31 05:47:47 +02:00
describe('bytesToWords', function() {
it('works', function() {
assert.deepEqual(convert.wordArrayToBytes(wordArray), bytes)
2014-03-26 08:30:17 +01:00
})
})
2014-03-31 05:47:47 +02:00
})
describe('reverseEndian', function() {
it('works', function() {
var bigEndian = "6a4062273ac4f9ea4ffca52d9fd102b08f6c32faa0a4d1318e3a7b2e437bb9c7"
var littleEdian = "c7b97b432e7b3a8e31d1a4a0fa326c8fb002d19f2da5fc4feaf9c43a2762406a"
assert.deepEqual(convert.reverseEndian(bigEndian), littleEdian)
assert.deepEqual(convert.reverseEndian(littleEdian), bigEndian)
})
2014-03-31 05:47:47 +02:00
})
})