import { SerializableWire , StorageKey } from '@neo-one/node-core' ; import { BN } from 'bn.js' ; export class KeyBuilder { private readonly id : number ; private mutableBuffer : Buffer ; public constructor ( id , prefix ) { if ( prefix . length !== 1 ) { throw new Error ( 'invalid prefix' ) ; } this . id = id ; this . mutableBuffer = prefix ; } public addBuffer ( key ) { this . mutableBuffer = Buffer . concat ( [ this . mutableBuffer , key ] ) ; return this ; } public addSerializable ( key ) { const serialized = key . serializeWire ( ) ; this . mutableBuffer = Buffer . concat ( [ this . mutableBuffer , serialized ] ) ; return this ; } public addUInt32BE ( value ) { const buffer = Buffer . alloc ( 4 ) ; buffer . writeUInt32BE ( value ) ; return this . addBuffer ( buffer ) ; } public addUInt64LE ( value ) { const buffer = value . toArrayLike ( Buffer , 'le' , 8 ) ; return this . addBuffer ( buffer ) ; } public addUInt64BE ( value ) { const buffer = value . toArrayLike ( Buffer , 'be' , 8 ) ; return this . addBuffer ( buffer ) ; } public toSearchPrefix ( ) { return StorageKey . createSearchPrefix ( this . id , this . mutableBuffer ) ; } public toStorageKey ( ) { return new StorageKey ( { id : this . id , key : this . mutableBuffer } ) ; } }