import { UnwrappableCodec } from "./interfaces/UnwrappableCodec" ; import { Bytes } from "./utils/Bytes" ; export class Hash implements UnwrappableCodec < Array < u8 > > { private _values : Array < u8 > ; constructor ( value = [ ] ) { this . _values = new Array < u8 > ( 32 ) ; Bytes . copy ( value , this . _values ) ; } description public unwrap ( ) { return this . _values ; } description public toU8a ( ) { const result = new Array < u8 > ( this . encodedLength ( ) ) ; Bytes . copy < u8 > ( this . _values , result ) ; return result ; } description param bytes param index public populateFromBytes ( bytes , index = 0 ) { assert ( bytes . length - index >= 0 , "Hash: Empty bytes array provided" ) ; this . _values = new Array < u8 > ( 32 ) ; Bytes . copy ( bytes , this . _values , 0 , index ) ; } description public toString ( ) { return "0x" + this . _values . join ( "" ) ; } description static bytesToHash ( bytes ) { let hash = new Hash ( [ ] ) ; if ( bytes . length > 32 ) { bytes = bytes . slice ( bytes . length - 32 ) ; } const position = 32 - bytes . length ; Bytes . copy < u8 > ( bytes , hash . unwrap ( ) , position ) ; return hash ; } description public encodedLength ( ) { return 32 ; } description param input param index static fromU8a ( input , index = 0 ) { assert ( input . length - index >= 0 , "Hash: Empty bytes array provided" ) ; return new Hash ( input . slice ( index ) ) ; } eq ( other ) { let areEqual = true ; for ( let i = 0 ; i < this . unwrap ( ) . length ; i ++ ) { if ( this . unwrap ( ) [ i ] != other . unwrap ( ) [ i ] ) { areEqual = false ; break ; } } return areEqual ; } notEq ( other ) { return ! this . eq ( other ) ; } @ inline @ operator ( "==" ) static eq ( a , b ) { return a . eq ( b ) ; } @ inline @ operator ( "!=" ) static notEq ( a , b ) { return a . notEq ( b ) ; } toU8aPacked ( ) { return this . toU8a ( ) ; } encodedLengthPacked ( ) { return this . encodedLength ( ) ; } populateFromPackedBytes ( bytes , index = 0 ) { this . populateFromBytes ( bytes , index ) ; } }