本文整理了Java中org.bitcoinj.core.Address
类的一些代码示例,展示了Address
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Address
类的具体详情如下:
包路径:org.bitcoinj.core.Address
类名称:Address
[英]A Bitcoin address looks like 1MsScoe2fTJoq4ZPdQgqyhgWeoNamYPevy and is derived from an elliptic curve public key plus a set of network parameters. Not to be confused with a PeerAddress or AddressMessagewhich are about network (TCP) addresses.
A standard address is built by taking the RIPE-MD160 hash of the public key bytes, with a version prefix and a checksum suffix, then encoding it textually as base58. The version prefix is used to both denote the network for which the address is valid (see NetworkParameters, and also to indicate how the bytes inside the address should be interpreted. Whilst almost all addresses today are hashes of public keys, another (currently unsupported type) can contain a hash of a script instead.
[中]比特币地址看起来像1MSSCOE2FTJOQ4ZPDQGQYHGEWEONMYPEVY,由椭圆曲线公钥加上一组网络参数派生而来。不要与关于网络(TCP)地址的对等地址或AddressMessage混淆。
标准地址是通过获取公钥字节的RIME-MD160散列、版本前缀和校验和后缀,然后将其文本编码为base58来构建的。版本前缀既用于表示地址有效的网络(请参阅NetworkParameters),也用于指示如何解释地址中的字节。虽然目前几乎所有地址都是公钥哈希,但另一个(当前不支持的类型)可以包含脚本哈希。
代码示例来源:origin: Multibit-Legacy/multibit-hd
/**
* @return The contact name if present, otherwise the Bitcoin address
*/
public String getSummary() {
if (contact.isPresent()) {
return contact.get().getName();
}
// The Base58 representation of this address
return bitcoinAddress.toString();
}
}
代码示例来源:origin: greenaddress/GreenBits
@Test
public void comparisonBytesVsString() throws Exception {
// TODO: To properly test this we need a much larger data set
Address a = Address.fromBase58(mainParams, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
Address b = Address.fromBase58(mainParams, "1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P");
int resultBytes = a.compareTo(b);
int resultsString = a.toString().compareTo(b.toString());
assertTrue( resultBytes < 0 );
assertTrue( resultsString < 0 );
}
}
代码示例来源:origin: DanielKrawisz/Shufflepuff
public static boolean isValidAddress(String address, NetworkParameters params) {
try {
new Address(params, address);
return true;
} catch (AddressFormatException e) {
return false;
}
}
代码示例来源:origin: Coinomi/coinomi-android
BitAddress(Address address) throws WrongNetworkException {
this((CoinType) address.getParameters(), address.getVersion(), address.getHash160());
}
代码示例来源:origin: HashEngineering/dashj
boolean setSporkAddress(String strAddress) {
try {
Address address = Address.fromBase58(context.getParams(), strAddress);
sporkPubKeyId = address.getHash160();
} catch (AddressFormatException x) {
log.error("CSporkManager::SetSporkAddress -- Failed to parse spork address");
return false;
}
return true;
}
代码示例来源:origin: fr.acinq/bitcoinj-core
/** If the given P2SH address is "current", advance it to a new one. */
private void maybeMarkCurrentAddressAsUsed(Address address) {
checkArgument(address.isP2SHAddress());
for (Map.Entry<KeyChain.KeyPurpose, Address> entry : currentAddresses.entrySet()) {
if (entry.getValue() != null && entry.getValue().equals(address)) {
log.info("Marking P2SH address as used: {}", address);
currentAddresses.put(entry.getKey(), freshAddress(entry.getKey()));
return;
}
}
}
代码示例来源:origin: greenaddress/GreenBits
@Test
public void p2shAddress() throws Exception {
// Test that we can construct P2SH addresses
Address mainNetP2SHAddress = Address.fromBase58(MainNetParams.get(), "35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU");
assertEquals(mainNetP2SHAddress.version, MainNetParams.get().p2shHeader);
assertTrue(mainNetP2SHAddress.isP2SHAddress());
Address testNetP2SHAddress = Address.fromBase58(TestNet3Params.get(), "2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe");
assertEquals(testNetP2SHAddress.version, TestNet3Params.get().p2shHeader);
assertTrue(testNetP2SHAddress.isP2SHAddress());
// Test that we can determine what network a P2SH address belongs to
NetworkParameters mainNetParams = Address.getParametersFromAddress("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU");
assertEquals(MainNetParams.get().getId(), mainNetParams.getId());
NetworkParameters testNetParams = Address.getParametersFromAddress("2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe");
assertEquals(TestNet3Params.get().getId(), testNetParams.getId());
// Test that we can convert them from hashes
byte[] hex = HEX.decode("2ac4b0b501117cc8119c5797b519538d4942e90e");
Address a = Address.fromP2SHHash(mainParams, hex);
assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", a.toString());
Address b = Address.fromP2SHHash(testParams, HEX.decode("18a0e827269b5211eb51a4af1b2fa69333efa722"));
assertEquals("2MuVSxtfivPKJe93EC1Tb9UhJtGhsoWEHCe", b.toString());
Address c = Address.fromP2SHScript(mainParams, ScriptBuilder.createP2SHOutputScript(hex));
assertEquals("35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU", c.toString());
}
代码示例来源:origin: Coinomi/coinomi-android
@Test
public void derive() throws Exception {
ECKey key1 = chain.getKey(SimpleHDKeyChain.KeyPurpose.RECEIVE_FUNDS);
ECKey key2 = chain.getKey(SimpleHDKeyChain.KeyPurpose.RECEIVE_FUNDS);
final Address address = new Address(UnitTestParams.get(), "n1bQNoEx8uhmCzzA5JPG6sFdtsUQhwiQJV");
assertEquals(address, key1.toAddress(UnitTestParams.get()));
assertEquals("mnHUcqUVvrfi5kAaXJDQzBb9HsWs78b42R", key2.toAddress(UnitTestParams.get()).toString());
assertEquals(key1, chain.findKeyFromPubHash(address.getHash160()));
assertEquals(key2, chain.findKeyFromPubKey(key2.getPubKey()));
key1.sign(Sha256Hash.ZERO_HASH);
ECKey key3 = chain.getKey(SimpleHDKeyChain.KeyPurpose.CHANGE);
assertEquals("mqumHgVDqNzuXNrszBmi7A2UpmwaPMx4HQ", key3.toAddress(UnitTestParams.get()).toString());
key3.sign(Sha256Hash.ZERO_HASH);
}
代码示例来源:origin: greenaddress/GreenBits
@Test
public void stringification() throws Exception {
// Test a testnet address.
Address a = new Address(testParams, HEX.decode("fda79a24e50ff70ff42f7d89585da5bd19d9e5cc"));
assertEquals("n4eA2nbYqErp7H6jebchxAN59DmNpksexv", a.toString());
assertFalse(a.isP2SHAddress());
Address b = new Address(mainParams, HEX.decode("4a22c3c4cbb31e4d03b15550636762bda0baf85a"));
assertEquals("17kzeh4N8g49GFvdDzSf8PjaPfyoD1MndL", b.toString());
assertFalse(b.isP2SHAddress());
}
代码示例来源:origin: cash.bitcoinj/bitcoinj-core
public void markP2SHAddressAsUsed(Address address) {
checkArgument(address.isP2SHAddress());
RedeemData data = findRedeemDataFromScriptHash(address.getHash160());
if (data == null)
return; // Not our P2SH address.
for (ECKey key : data.keys) {
for (DeterministicKeyChain chain : chains) {
DeterministicKey k = chain.findKeyFromPubKey(key.getPubKey());
if (k == null) continue;
chain.markKeyAsUsed(k);
maybeMarkCurrentAddressAsUsed(address);
}
}
}
代码示例来源:origin: ConsensusJ/consensusj
public AddressGroupingItem(List<Object> addressItem, NetworkParameters netParams) {
String addressStr = (String) addressItem.get(0);
//TODO: Try to avoid using Double
Double balanceDouble = (Double) addressItem.get(1);
account = (addressItem.size() > 2) ? (String) addressItem.get(2) : null;
address = Address.fromBase58(netParams, addressStr);
balance = Coin.valueOf(((Double)(balanceDouble * 100000000.0)).longValue());
}
代码示例来源:origin: greenaddress/GreenBits
@Test
public void testP2SHOutputScript() throws Exception {
String P2SHAddressString = "35b9vsyH1KoFT5a5KtrKusaCcPLkiSo1tU";
Address P2SHAddress = Address.fromBase58(MainNetParams.get(), P2SHAddressString);
Script script = ScriptBuilder.createOutputScript(P2SHAddress);
Transaction tx = new Transaction(MainNetParams.get());
tx.addOutput(Coin.COIN, script);
assertEquals(P2SHAddressString, tx.getOutput(0).getAddressFromP2SH(MainNetParams.get()).toString());
}
代码示例来源:origin: greenaddress/GreenBits
@Test
public void testScriptPubKey() throws Exception {
// Check we can extract the to address
byte[] pubkeyBytes = HEX.decode(pubkeyProg);
Script pubkey = new Script(pubkeyBytes);
assertEquals("DUP HASH160 PUSHDATA(20)[33e81a941e64cda12c6a299ed322ddbdd03f8d0e] EQUALVERIFY CHECKSIG", pubkey.toString());
Address toAddr = new Address(PARAMS, pubkey.getPubKeyHash());
assertEquals("mkFQohBpy2HDXrCwyMrYL5RtfrmeiuuPY2", toAddr.toString());
}
代码示例来源:origin: cash.bitcoinj/bitcoinj-core
/**
* Gets the destination address from this script, if it's in the required form (see getPubKey).
*
* @param forcePayToPubKey
* If true, allow payToPubKey to be casted to the corresponding address. This is useful if you prefer
* showing addresses rather than pubkeys.
*/
public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException {
if (isSentToAddress())
return new Address(params, getPubKeyHash());
else if (isPayToScriptHash())
return Address.fromP2SHScript(params, this);
else if (forcePayToPubKey && isSentToRawPubKey())
return ECKey.fromPublicOnly(getPubKey()).toAddress(params);
else
throw new ScriptException("Cannot cast this script to a pay-to-address type");
}
代码示例来源:origin: Multibit-Legacy/multibit-hd
Address signingAddress = new Address(BitcoinNetwork.current().get(), addressText);
if (signingAddress.equals(gotAddress)) {
return new VerifyMessageResult(true, CoreMessageKey.VERIFY_MESSAGE_VERIFY_SUCCESS, null);
} else {
代码示例来源:origin: greenaddress/GreenBits
/** Creates a scriptPubKey that encodes payment to the given address. */
public static Script createOutputScript(Address to) {
if (to.isP2SHAddress()) {
// OP_HASH160 <scriptHash> OP_EQUAL
return new ScriptBuilder()
.op(OP_HASH160)
.data(to.getHash160())
.op(OP_EQUAL)
.build();
} else if (to.isP2WPKHAddress() || to.isP2WSHAddress()) {
return new ScriptBuilder()
.smallNum(0)
.data(to.getHash())
.build();
} else {
// OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
return new ScriptBuilder()
.op(OP_DUP)
.op(OP_HASH160)
.data(to.getHash160())
.op(OP_EQUALVERIFY)
.op(OP_CHECKSIG)
.build();
}
}
代码示例来源:origin: greenaddress/GreenBits
@Test
public void p2shAddressCreationFromKeys() throws Exception {
// import some keys from this example: https://gist.github.com/gavinandresen/3966071
ECKey key1 = DumpedPrivateKey.fromBase58(mainParams, "5JaTXbAUmfPYZFRwrYaALK48fN6sFJp4rHqq2QSXs8ucfpE4yQU").getKey();
key1 = ECKey.fromPrivate(key1.getPrivKeyBytes());
ECKey key2 = DumpedPrivateKey.fromBase58(mainParams, "5Jb7fCeh1Wtm4yBBg3q3XbT6B525i17kVhy3vMC9AqfR6FH2qGk").getKey();
key2 = ECKey.fromPrivate(key2.getPrivKeyBytes());
ECKey key3 = DumpedPrivateKey.fromBase58(mainParams, "5JFjmGo5Fww9p8gvx48qBYDJNAzR9pmH5S389axMtDyPT8ddqmw").getKey();
key3 = ECKey.fromPrivate(key3.getPrivKeyBytes());
List<ECKey> keys = Arrays.asList(key1, key2, key3);
Script p2shScript = ScriptBuilder.createP2SHOutputScript(2, keys);
Address address = Address.fromP2SHScript(mainParams, p2shScript);
assertEquals("3N25saC4dT24RphDAwLtD8LUN4E2gZPJke", address.toString());
}
代码示例来源:origin: greenaddress/GreenBits
@Test
public void comparisonCloneEqualTo() throws Exception {
Address a = Address.fromBase58(mainParams, "1Dorian4RoXcnBv9hnQ4Y2C1an6NJ4UrjX");
Address b = a.clone();
int result = a.compareTo(b);
assertEquals(0, result);
}
代码示例来源:origin: greenaddress/GreenBits
/**
* Gets the destination address from this script, if it's in the required form (see getPubKey).
*
* @param forcePayToPubKey
* If true, allow payToPubKey to be casted to the corresponding address. This is useful if you prefer
* showing addresses rather than pubkeys.
*/
public Address getToAddress(NetworkParameters params, boolean forcePayToPubKey) throws ScriptException {
if (isSentToAddress())
return new Address(params, getPubKeyHash());
else if (isPayToScriptHash())
return Address.fromP2SHScript(params, this);
else if (isSentToP2WPKH())
return Address.fromP2WPKHHash(params, getPubKeyHash());
else if (isSentToP2WSH())
return Address.fromP2WSHHash(params, getPubKeyHash());
else if (forcePayToPubKey && isSentToRawPubKey())
return ECKey.fromPublicOnly(getPubKey()).toAddress(params);
else
throw new ScriptException("Cannot cast this script to a pay-to-address type");
}
代码示例来源:origin: DanielKrawisz/Shufflepuff
@Override
public boolean equals(Object obj) {
return obj instanceof AddressImpl && address.equals(((AddressImpl) obj).address);
}
}
内容来源于网络,如有侵权,请联系作者删除!