graphenej/src/main/java/com/luminiasoft/bitshares/Address.java

40 lines
1.1 KiB
Java
Raw Normal View History

2016-11-23 19:45:35 +00:00
package com.luminiasoft.bitshares;
import com.google.common.primitives.Bytes;
import org.bitcoinj.core.Base58;
import org.bitcoinj.core.ECKey;
import org.spongycastle.crypto.digests.RIPEMD160Digest;
import java.util.Arrays;
/**
* Class used to encapsulate address-related operations.
*/
public class Address {
public final static String DEFAULT_PREFIX = "BTS";
private ECKey key;
private String prefix;
public Address(ECKey key){
this.key = key;
this.prefix = DEFAULT_PREFIX;
}
public Address(ECKey key, String prefix){
this.key = key;
this.prefix = prefix;
}
@Override
public String toString(){
byte[] pubKey = key.getPubKey();
byte[] checksum = new byte[160 / 8];
RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest();
ripemd160Digest.update(pubKey, 0, pubKey.length);
ripemd160Digest.doFinal(checksum, 0);
byte[] pubKeyChecksummed = Bytes.concat(pubKey, Arrays.copyOfRange(checksum, 0, 4));
return this.prefix + Base58.encode(pubKeyChecksummed);
}
}