001package org.consensusj.namecoin.jsonrpc.pojo;
002
003import com.fasterxml.jackson.annotation.JsonCreator;
004import com.fasterxml.jackson.annotation.JsonProperty;
005import com.fasterxml.jackson.databind.JavaType;
006import com.fasterxml.jackson.databind.ObjectMapper;
007import org.bitcoinj.base.Address;
008import org.bitcoinj.base.Sha256Hash;
009
010import java.io.IOException;
011import java.util.Map;
012
013/**
014 * Namecoin name data
015 *
016 * example format:
017 * [name:d/beelin,
018 * value:{"alias":"beelin.github.io"},
019 * txid:5737868f7044ade9b0c04698c563955d9b49db841a4e575bc384873073b956ed,
020 * address:NAPwebo2VLvGdBFC4cHrLRPS6ZXPKddx9Z,
021 * expires_in:31874]
022 */
023public class NameData {
024    private static ObjectMapper mapper = new ObjectMapper();
025    private static JavaType mapType = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
026
027
028    private final   String name;
029    private final   Map<String, Object> value;     // Deserialized from escape JSON string
030    private final   Sha256Hash txid;
031    private final   Address address;
032    private final   int expires_in;
033
034    @JsonCreator
035    public NameData(@JsonProperty("name")       String name,
036                    @JsonProperty("value")      String value,
037                    @JsonProperty("txid")       Sha256Hash txid,
038                    @JsonProperty("address")    Address address,
039                    @JsonProperty("expires_in") int expires_in) throws IOException {
040        this.name = name;
041        this.value = mapper.readValue(value, mapType);
042        this.txid = txid;
043        this.address = address;
044        this.expires_in = expires_in;
045    }
046
047    /**
048     *
049     * @return namespace/name
050     */
051    public String getName() {
052        return name;
053    }
054
055    /**
056     *
057     * @return JSONNode
058     */
059    public Map<String, Object> getValue() {
060        return value;
061    }
062
063    public Sha256Hash getTxid() {
064        return txid;
065    }
066
067    /**
068     *
069     * @return Address as String (for now since bitcoinj won't allow N... addresses)
070     */
071    public Address getAddress() {
072        return address;
073    }
074
075    public int getExpires_in() {
076        return expires_in;
077    }
078}