package com.chain.api;

import com.chain.exception.BadURLException;
import com.chain.exception.ChainException;
import com.chain.http.Context;

import java.net.MalformedURLException;
import java.net.URL;

public class MockHsm {
    public static class Key {
        public String xpub;
        public URL hsmUrl;

        public static Key create(Context ctx) throws ChainException {
            Key key = ctx.request("mockhsm/create-key", null, Key.class);
            key.hsmUrl = buildMockHsmUrl(ctx.getUrl());
            return key;
        }

        public static class Items extends PagedItems<Key> {
            public Items getPage() throws ChainException {
                Items items = this.context.request("mockhsm/list-keys", this.query, Items.class);
                items.setContext(this.context);
                URL mockHsmUrl = buildMockHsmUrl(this.context.getUrl());
                for (Key k : items.list) {
                    k.hsmUrl = mockHsmUrl;
                }
                return items;
            }
        }

        public static Items list(Context ctx) throws ChainException {
            Items items = new Items();
            items.setContext(ctx);
            return items.getPage();
        }
    }

    private static URL buildMockHsmUrl(URL coreUrl) throws BadURLException {
        try {
            return new URL(coreUrl.toString() + "/mockhsm");
        } catch (MalformedURLException e) {
            throw new BadURLException(e.getMessage());
        }
    }
}

@@ -195,16 +195,64 @@
 net.URL;
+%0Aimport java.util.HashMap;%0Aimport java.util.Map;
 %0A%0Apublic
@@ -294,24 +294,53 @@
 class Key %7B%0A
+        public String alias;%0A
         publ
@@ -445,32 +445,32 @@
 hainException %7B%0A
-
             Key 
@@ -610,32 +610,372 @@
 key;%0A        %7D%0A%0A
+        public static Key create(Context ctx, String alias) throws ChainException %7B%0A            Map%3CString, Object%3E req = new HashMap%3C%3E();%0A            req.put(%22alias%22, alias);%0A            Key key = ctx.request(%22mockhsm/create-key%22, req, Key.class);%0A            key.hsmUrl = buildMockHsmUrl(ctx.getUrl());%0A            return key;%0A        %7D%0A%0A
         public s

add alias member to MockHsm.Key