import * as rp from "request-promise" ; import { KeyManagementAPI , KeyStore } from "./KeyStore" ; type KeyType = "asset" | "platform" ; class RemoteKeyManager implements KeyManagementAPI { public keystoreURL : string ; public keyType : string ; constructor ( keystoreURL , keyType ) { this . keystoreURL = keystoreURL ; this . keyType = keyType ; } public async getKeyList ( ) { const response = await rp . get ( `${ this . keystoreURL }/api/keys` , { body : { keyType : this . keyType } , json : true } ) ; if ( ! response . success ) { throw Error ( response . error ) ; } return response . result ; } public async createKey ( params = { } ) { const response = await rp . post ( `${ this . keystoreURL }/api/keys` , { body : { keyType : this . keyType , passphrase : params . passphrase } , json : true } ) ; if ( ! response . success ) { throw Error ( response . error ) ; } return response . result ; } public async removeKey ( params ) { const response = await rp . delete ( `${ this . keystoreURL }/api/keys/${ params . key }` , { body : { keyType : this . keyType } , json : true } ) ; if ( ! response . success ) { throw Error ( response . error ) ; } return response . result ; } public async exportRawKey ( params ) { const response = await rp . post ( `${ this . keystoreURL }/api/keys/${ params . key }/sign` , { body : { keyType : this . keyType , passphrase : params . passphrase } , json : true } ) ; if ( ! response . success ) { throw Error ( response . error ) ; } return response . result ; } public async getPublicKey ( params ) { const response = await rp . get ( `${ this . keystoreURL }/api/keys/${ params . key }/publicKey` , { body : { keyType : this . keyType } , json : true } ) ; if ( ! response . success ) { throw Error ( response . error ) ; } return response . result ; } public async sign ( params ) { const response = await rp . post ( `${ this . keystoreURL }/api/keys/${ params . key }/sign` , { body : { keyType : this . keyType , passphrase : params . passphrase , publicKey : params . key , message : params . message } , json : true } ) ; if ( ! response . success ) { throw Error ( response . error ) ; } return response . result ; } } export class RemoteKeyStore implements KeyStore { public static async create ( keystoreURL ) { const keystore = new RemoteKeyStore ( keystoreURL ) ; await keystore . ping ( ) ; return keystore ; } public static createUnsafe ( keystoreURL ) { const keystore = new RemoteKeyStore ( keystoreURL ) ; keystore . ping ( ) ; return keystore ; } public platform : KeyManagementAPI ; public asset : KeyManagementAPI ; public keystoreURL : string ; public mapping = { get : async ( params ) : Promise < string > => { const response = await rp . get ( `${ this . keystoreURL }/api/mapping/${ params . key }` , { json : true } ) ; if ( ! response . success ) { throw Error ( response . error ) ; } return response . result ; } } ; public constructor ( keystoreURL ) { this . keystoreURL = keystoreURL ; this . platform = new RemoteKeyManager ( keystoreURL , "platform" ) ; this . asset = new RemoteKeyManager ( keystoreURL , "asset" ) ; } public async ping ( ) { const response = await rp . get ( `${ this . keystoreURL }/ping` , { json : true } ) ; if ( ! response . success ) { throw Error ( response . error ) ; } return response . result ; } }