mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-12-06 17:27:59 +00:00
fix: unset hash128 fields using AccountSet (#2156)
This commit is contained in:
@@ -9,8 +9,25 @@ class Hash128 extends Hash {
|
||||
static readonly ZERO_128: Hash128 = new Hash128(Buffer.alloc(Hash128.width))
|
||||
|
||||
constructor(bytes: Buffer) {
|
||||
if (bytes && bytes.byteLength === 0) {
|
||||
bytes = Hash128.ZERO_128.bytes
|
||||
}
|
||||
|
||||
super(bytes ?? Hash128.ZERO_128.bytes)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the hex representation of a hash-128 bytes, allowing unset
|
||||
*
|
||||
* @returns hex String of this.bytes
|
||||
*/
|
||||
toHex(): string {
|
||||
const hex = this.toBytes().toString('hex').toUpperCase()
|
||||
if (/^0+$/.exec(hex)) {
|
||||
return ''
|
||||
}
|
||||
return hex
|
||||
}
|
||||
}
|
||||
|
||||
export { Hash128 }
|
||||
|
||||
@@ -1,7 +1,33 @@
|
||||
const { coreTypes } = require('../dist/types')
|
||||
const { Hash160, Hash256, AccountID, Currency } = coreTypes
|
||||
const { Hash128, Hash160, Hash256, AccountID, Currency } = coreTypes
|
||||
const { Buffer } = require('buffer/')
|
||||
|
||||
describe('Hash128', function () {
|
||||
test('has a static width member', function () {
|
||||
expect(Hash128.width).toBe(16)
|
||||
})
|
||||
test('can be unset', function () {
|
||||
const h1 = Hash128.from('')
|
||||
expect(h1.toJSON()).toBe('')
|
||||
})
|
||||
test('can be compared against another', function () {
|
||||
const h1 = Hash128.from('100000000000000000000000000000000')
|
||||
const h2 = Hash128.from('200000000000000000000000000000000')
|
||||
const h3 = Hash128.from('000000000000000000000000000000003')
|
||||
expect(h1.lt(h2)).toBe(true)
|
||||
expect(h3.lt(h2)).toBe(true)
|
||||
expect(h2.gt(h1)).toBe(true)
|
||||
expect(h1.gt(h3)).toBe(true)
|
||||
})
|
||||
test('throws when constructed from invalid hash length', () => {
|
||||
expect(() => Hash128.from('1000000000000000000000000000000')).toThrow(
|
||||
'Invalid Hash length 15',
|
||||
)
|
||||
expect(() => Hash128.from('10000000000000000000000000000000000')).toThrow(
|
||||
'Invalid Hash length 17',
|
||||
)
|
||||
})
|
||||
})
|
||||
describe('Hash160', function () {
|
||||
test('has a static width member', function () {
|
||||
expect(Hash160.width).toBe(20)
|
||||
|
||||
Reference in New Issue
Block a user