UInt160 can be an account or a plain hash.

The UInt160 class used to be hardcoded to be an Account. This commit changes it
so it can be used as an account or a plain hash. It will try to automatically
self-classify based on how it is initialized.

In the future we may want to have some dedicated classes rather than a
single configurable UInt160.
This commit is contained in:
Stefan Thomas
2013-11-12 15:51:50 -08:00
parent ae68e3a1a6
commit 05f3a97042
5 changed files with 135 additions and 20 deletions

View File

@@ -36,7 +36,7 @@ describe('Amount', function() {
assert.deepEqual(new BigInteger(), UInt160.from_generic('0')._value);
});
it('Parse 0 export', function () {
assert.strictEqual(UInt160.ACCOUNT_ZERO, UInt160.from_generic('0').to_json());
assert.strictEqual(UInt160.ACCOUNT_ZERO, UInt160.from_generic('0').set_version(0).to_json());
});
it('Parse 1', function () {
assert.deepEqual(new BigInteger([1]), UInt160.from_generic('1')._value);

View File

@@ -378,19 +378,37 @@ describe('Serialized types', function() {
describe('Hash160', function() {
it('Serialize 0', function () {
var hex = '0000000000000000000000000000000000000000';
var base58 = 'rrrrrrrrrrrrrrrrrrrrrhoLvTp';
var so = new SerializedObject();
types.Hash160.serialize(so, 'rrrrrrrrrrrrrrrrrrrrrhoLvTp');
assert.strictEqual(so.to_hex(), '0000000000000000000000000000000000000000');
types.Hash160.serialize(so, base58);
assert.strictEqual(so.to_hex(), hex);
so = new SerializedObject();
types.Hash160.serialize(so, hex);
assert.strictEqual(so.to_hex(), hex);
});
it('Serialize 1', function () {
var hex = '0000000000000000000000000000000000000001';
var base58 = 'rrrrrrrrrrrrrrrrrrrrBZbvji';
var so = new SerializedObject();
types.Hash160.serialize(so, 'rrrrrrrrrrrrrrrrrrrrBZbvji');
assert.strictEqual(so.to_hex(), '0000000000000000000000000000000000000001');
types.Hash160.serialize(so, base58);
assert.strictEqual(so.to_hex(), hex);
so = new SerializedObject();
types.Hash160.serialize(so, hex);
assert.strictEqual(so.to_hex(), hex);
});
it('Serialize FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', function () {
var hex = 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF';
var base58 = 'rQLbzfJH5BT1FS9apRLKV3G8dWEA5njaQi';
var so = new SerializedObject();
types.Hash160.serialize(so, 'rQLbzfJH5BT1FS9apRLKV3G8dWEA5njaQi');
assert.strictEqual(so.to_hex(), 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF');
types.Hash160.serialize(so, base58);
assert.strictEqual(so.to_hex(), hex);
so = new SerializedObject();
types.Hash160.serialize(so, hex);
assert.strictEqual(so.to_hex(), hex);
});
it('Parse 0', function () {
var val = '0000000000000000000000000000000000000000';
@@ -410,6 +428,14 @@ describe('Serialized types', function() {
var num = types.Hash160.parse(so);
assert.strictEqual(num.to_hex(), val);
});
it('Parse 0 as JSON', function () {
// Hash160 should be returned as hex in JSON, unlike
// addresses.
var val = '0000000000000000000000000000000000000000';
var so = new SerializedObject(val);
var num = types.Hash160.parse(so);
assert.strictEqual(num.to_json(), val);
});
});
describe('Hash256', function() {
@@ -539,6 +565,60 @@ describe('Serialized types', function() {
});
});
describe('Account', function() {
it('Serialize 0', function () {
var hex = '0000000000000000000000000000000000000000';
var base58 = 'rrrrrrrrrrrrrrrrrrrrrhoLvTp';
var so = new SerializedObject();
types.Account.serialize(so, base58);
assert.strictEqual(so.to_hex(), "14"+hex);
so = new SerializedObject();
types.Account.serialize(so, hex);
assert.strictEqual(so.to_hex(), "14"+hex);
});
it('Serialize 1', function () {
var hex = '0000000000000000000000000000000000000001';
var base58 = 'rrrrrrrrrrrrrrrrrrrrBZbvji';
var so = new SerializedObject();
types.Account.serialize(so, base58);
assert.strictEqual(so.to_hex(), "14"+hex);
so = new SerializedObject();
types.Account.serialize(so, hex);
assert.strictEqual(so.to_hex(), "14"+hex);
});
it('Serialize FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', function () {
var hex = 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF';
var base58 = 'rQLbzfJH5BT1FS9apRLKV3G8dWEA5njaQi';
var so = new SerializedObject();
types.Account.serialize(so, base58);
assert.strictEqual(so.to_hex(), "14"+hex);
so = new SerializedObject();
types.Account.serialize(so, hex);
assert.strictEqual(so.to_hex(), "14"+hex);
});
it('Parse 0', function () {
var val = '140000000000000000000000000000000000000000';
var so = new SerializedObject(val);
var num = types.Account.parse(so);
assert.strictEqual(num.to_json(), 'rrrrrrrrrrrrrrrrrrrrrhoLvTp');
});
it('Parse 1', function () {
var val = '140000000000000000000000000000000000000001';
var so = new SerializedObject(val);
var num = types.Account.parse(so);
assert.strictEqual(num.to_json(), 'rrrrrrrrrrrrrrrrrrrrBZbvji');
});
it('Parse HASH160_MAX', function () {
var val = '14FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF';
var so = new SerializedObject(val);
var num = types.Account.parse(so);
assert.strictEqual(num.to_json(), 'rQLbzfJH5BT1FS9apRLKV3G8dWEA5njaQi');
});
});
describe('PathSet', function() {
it('Serialize single empty path [[]]', function () {
var so = new SerializedObject();