import { RadixSerializer , RadixPrimitive } from '..' ; const id = ':aid:' ; @ RadixSerializer . registerPrimitive ( id ) export class RadixAID implements RadixPrimitive { public static BYTES = 32 ; public static HASH_BYTES = 32 ; private bytes : Buffer ; public constructor ( bytes ) { if ( bytes . length !== RadixAID . BYTES ) { throw new Error ( `Number of bytes must be ${ RadixAID . BYTES } but is ${ bytes . length }` ) ; } this . bytes = Buffer . from ( bytes ) ; } public static from ( hash ) { const bytes = Buffer . alloc ( this . BYTES ) ; hash . copy ( bytes , 0 , 0 , this . HASH_BYTES ) ; return new this ( bytes ) ; } public static fromAIDString ( aidString ) { const bytesHex = aidString . split ( id ) [ 1 ] ; return new RadixAID ( Buffer . from ( bytesHex , 'hex' ) ) ; } public static fromJSON ( data ) { return new this ( Buffer . from ( data , 'hex' ) ) ; } public toJSON ( ) { return `${ id }${ this . bytes . toString ( 'hex' ) }` ; } public toDSON ( ) { return RadixSerializer . toDSON ( this ) ; } public encodeCBOR ( encoder ) { const output = Buffer . alloc ( this . bytes . length + 1 ) ; output . writeInt8 ( 0x08 , 0 ) ; this . bytes . copy ( output , 1 ) ; return encoder . pushAny ( output ) ; } public equals ( aid ) { return this . bytes . compare ( aid . bytes ) === 0 ; } public toString ( ) { return this . bytes . toString ( 'hex' ) ; } }