import { BigNumber } from 'bignumber.js' ; import { reverseHex , StringReader } from '../utils' ; const Fixed64Size = 8 ; export default class Fixed64 { static deserialize ( sr ) { const f = new Fixed64 ( ) ; let v = sr . read ( 8 ) ; v = reverseHex ( v ) ; while ( v . substr ( 0 , 2 ) === '00' ) { v = v . substring ( 2 ) ; } f . value = new BigNumber ( v , 16 ) . toString ( ) ; return f ; } value : string ; constructor ( value ? ) { if ( value && value . length > 16 || value && ! /^[0-9]\d*$/ . test ( value ) ) { throw new Error ( 'Invalid value.' + value ) ; } this . value = value || '0000000000000000' ; } serialize ( ) { let hexstring = new BigNumber ( this . value ) . toString ( 16 ) ; const size = Fixed64Size * 2 ; hexstring = hexstring . length % size === 0 ? hexstring : ( '0' . repeat ( size ) + hexstring ) . substring ( hexstring . length ) ; hexstring = reverseHex ( hexstring ) ; return hexstring ; } }