package com.samourai.wallet.send;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class RBFSpend    {

    private String strHash = null;
    private List<String> changeAddrs = null;
    private String strSerializedTx = null;
    private List<String> keyBag = null;

    public RBFSpend(String strHash, List<String> changeAddrs, String strSerializedTx, List<String> keyBag)  {
        this.strHash = strHash;
        this.changeAddrs = changeAddrs;
        this.strSerializedTx = strSerializedTx;
        this.keyBag = keyBag;
    }

    public RBFSpend()  {
        changeAddrs = new ArrayList<String>();
        keyBag = new ArrayList<String>();
    }

    public String getHash() {
        return strHash;
    }

    public void setHash(String strHash) {
        this.strHash = strHash;
    }

    public List<String> getChangeAddrs() {
        return changeAddrs;
    }

    public void setChangeAddrs(List<String> changeAddrs) {
        this.changeAddrs = changeAddrs;
    }

    public void addChangeAddr(String addr)   {
        changeAddrs.add(addr);
    }

    public boolean containsChangeAddr(String addr)   {
        return changeAddrs.contains(addr);
    }

    public void addKey(String key)   {
        keyBag.add(key);
    }

    public boolean containsKey(String key)   {
        return keyBag.contains(key);
    }

    public String getSerializedTx() {
        return strSerializedTx;
    }

    public void setSerializedTx(String strSerializedTx) {
        this.strSerializedTx = strSerializedTx;
    }

    public List<String> getKeyBag() {
        return keyBag;
    }

    public void setKeyBag(List<String> keyBag) {
        this.keyBag = keyBag;
    }

    public JSONObject toJSON() {

        JSONObject jsonPayload = new JSONObject();
        JSONArray array = null;
        try {
            array = new JSONArray();

            for(String addr : changeAddrs)   {
                array.put(addr);
            }

            jsonPayload.put("change_addresses", array);

            array = new JSONArray();

            for(String k : keyBag)   {
                array.put(k);
            }

            jsonPayload.put("key_bag", array);

            if(strHash != null)    {
                jsonPayload.put("hash", strHash);
            }

            if(strSerializedTx != null)    {
                jsonPayload.put("tx", strSerializedTx);
            }

        }
        catch(JSONException je) {
            ;
        }

        return jsonPayload;
    }

    public void fromJSON(JSONObject jsonPayload) {

        try {

            if(jsonPayload.has("change_addresses"))    {

                JSONArray array = jsonPayload.getJSONArray("change_addresses");

                for(int i = 0; i < array.length(); i++)   {
                    changeAddrs.add((String)array.get(i));
                }

            }

            if(jsonPayload.has("key_bag"))    {

                JSONArray array = jsonPayload.getJSONArray("key_bag");

                for(int i = 0; i < array.length(); i++)   {
                    keyBag.add((String)array.get(i));
                }

            }

            if(jsonPayload.has("hash"))    {
                strHash = jsonPayload.getString("hash");
            }

            if(jsonPayload.has("tx"))    {
                strSerializedTx = jsonPayload.getString("tx");
            }

        }
        catch(JSONException ex) {
            throw new RuntimeException(ex);
        }

    }

}

@@ -143,16 +143,42 @@
 ayList;%0A
+import java.util.HashMap;%0A
 import j
@@ -348,37 +348,47 @@
 ll;%0A    private 
-List%3C
+HashMap%3CString,
 String%3E keyBag =
@@ -481,21 +481,31 @@
 izedTx, 
-List%3C
+HashMap%3CString,
 String%3E 
@@ -761,34 +761,39 @@
 eyBag = new 
-ArrayList%3C
+HashMap%3CString,
 String%3E();%0A 
@@ -1336,35 +1336,53 @@
 d addKey(String 
-key
+outpoint, String path
 )   %7B%0A        ke
@@ -1390,15 +1390,26 @@
 Bag.
-add(key
+put(outpoint, path
 );%0A 
@@ -1452,19 +1452,24 @@
 (String 
-key
+outpoint
 )   %7B%0A  
@@ -1500,12 +1500,20 @@
 ains
-(key
+Key(outpoint
 );%0A 
@@ -1711,37 +1711,47 @@
   %7D%0A%0A    public 
-List%3C
+HashMap%3CString,
 String%3E getKeyBa
@@ -1812,21 +1812,31 @@
 tKeyBag(
-List%3C
+HashMap%3CString,
 String%3E 
@@ -2264,17 +2264,24 @@
 (String 
-k
+outpoint
  : keyBa
@@ -2281,22 +2281,187 @@
 : keyBag
+.keySet()
 )   %7B%0A
+                JSONObject jobj = new JSONObject();%0A                jobj.put(%22outpoint%22, outpoint);%0A                jobj.put(%22path%22, keyBag.get(outpoint));%0A
         
@@ -2478,17 +2478,20 @@
 ray.put(
-k
+jobj
 );%0A     
@@ -3434,38 +3434,133 @@
     
-keyBag.add((String)array.get(i
+JSONObject jobj = (JSONObject) array.get(i);%0A                    keyBag.put(jobj.getString(%22outpoint%22), jobj.getString(%22path%22
 ));%0A

rename variables