import { generateID , getPubKeyPoint , IServiceProvider , IStorageLayer , KEY_NOT_FOUND , MockStorageLayerArgs , StringifiedType , } from "@tkey/common-types" ; import BN from "bn.js" ; import stringify from "json-stable-stringify" ; class MockStorageLayer implements IStorageLayer { dataMap : { [ key ] : unknown ; } ; storageLayerName : string ; lockMap : { [ key ] : string ; } ; serviceProvider : IServiceProvider ; constructor ( { dataMap , lockMap } = { dataMap : { } , lockMap : { } } ) { this . dataMap = dataMap || { } ; this . lockMap = lockMap || { } ; this . storageLayerName = "MockStorageLayer" ; } static fromJSON ( value ) { const { dataMap , lockMap , storageLayerName } = value ; if ( storageLayerName !== "MockStorageLayer" ) return undefined ; return new MockStorageLayer ( { dataMap , lockMap } ) ; } param privKey async getMetadata < T > ( params ) { const { serviceProvider , privKey } = params ; let usedKey ; if ( ! privKey ) usedKey = serviceProvider . retrievePubKeyPoint ( ) . getX ( ) ; else usedKey = getPubKeyPoint ( privKey ) . x ; const fromMap = this . dataMap [ usedKey . toString ( "hex" ) ] ; if ( ! fromMap ) { return Object . create ( { message : KEY_NOT_FOUND } ) as T ; } return JSON . parse ( this . dataMap [ usedKey . toString ( "hex" ) ] as string ) as T ; } param input param privKey async setMetadata < T > ( params ) { const { serviceProvider , privKey , input } = params ; let usedKey ; if ( ! privKey ) usedKey = serviceProvider . retrievePubKeyPoint ( ) . getX ( ) ; else usedKey = getPubKeyPoint ( privKey ) . x ; this . dataMap [ usedKey . toString ( "hex" ) ] = stringify ( input ) ; return { message : "success" } ; } async setMetadataStream < T > ( params ) { const { serviceProvider , privKey , input } = params ; input . forEach ( ( el , index ) => { let usedKey ; if ( ! privKey || ! privKey [ index ] ) usedKey = serviceProvider . retrievePubKeyPoint ( ) . getX ( ) ; else usedKey = getPubKeyPoint ( privKey [ index ] ) . x ; this . dataMap [ usedKey . toString ( "hex" ) ] = stringify ( el ) ; } ) ; return { message : "success" } ; } async acquireWriteLock ( params ) { const { serviceProvider , privKey } = params ; let usedKey ; if ( ! privKey ) usedKey = serviceProvider . retrievePubKeyPoint ( ) . getX ( ) ; else usedKey = getPubKeyPoint ( privKey ) . x ; if ( this . lockMap [ usedKey . toString ( "hex" ) ] ) return { status : 0 } ; const id = generateID ( ) ; this . lockMap [ usedKey . toString ( "hex" ) ] = id ; return { status : 1 , id } ; } async releaseWriteLock ( params ) { const { serviceProvider , privKey , id } = params ; let usedKey ; if ( ! privKey ) usedKey = serviceProvider . retrievePubKeyPoint ( ) . getX ( ) ; else usedKey = getPubKeyPoint ( privKey ) . x ; if ( ! this . lockMap [ usedKey . toString ( "hex" ) ] ) return { status : 0 } ; if ( id !== this . lockMap [ usedKey . toString ( "hex" ) ] ) return { status : 2 } ; this . lockMap [ usedKey . toString ( "hex" ) ] = null ; return { status : 1 } ; } toJSON ( ) { return { dataMap : this . dataMap , serviceProvider : this . serviceProvider , storageLayerName : this . storageLayerName , } ; } } export default MockStorageLayer ;