From 47659e73d7f43b9ca13a1092f6c9f45af72591cb Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Sun, 27 Jan 2019 00:54:59 +0100 Subject: [PATCH] Address: Add fromKey() helper method. --- .../main/java/org/bitcoinj/core/Address.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/core/src/main/java/org/bitcoinj/core/Address.java b/core/src/main/java/org/bitcoinj/core/Address.java index 48c6cce0..82d2dde0 100644 --- a/core/src/main/java/org/bitcoinj/core/Address.java +++ b/core/src/main/java/org/bitcoinj/core/Address.java @@ -18,6 +18,7 @@ package org.bitcoinj.core; import javax.annotation.Nullable; +import org.bitcoinj.script.Script; import org.bitcoinj.script.Script.ScriptType; /** @@ -67,6 +68,26 @@ public abstract class Address extends PrefixedChecksummedBytes { } } + /** + * Construct an {@link Address} that represents the public part of the given {@link ECKey}. + * + * @param params + * network this address is valid for + * @param key + * only the public part is used + * @param outputScriptType + * script type the address should use + * @return constructed address + */ + public static Address fromKey(final NetworkParameters params, final ECKey key, final ScriptType outputScriptType) { + if (outputScriptType == Script.ScriptType.P2PKH) + return LegacyAddress.fromKey(params, key); + else if (outputScriptType == Script.ScriptType.P2WPKH) + return SegwitAddress.fromKey(params, key); + else + throw new IllegalArgumentException(outputScriptType.toString()); + } + /** * Get either the public key hash or script hash that is encoded in the address. *