25
25
26
26
import com .google .common .primitives .UnsignedBytes ;
27
27
import org .bitcoinj .params .Networks ;
28
+ import org .bitcoinj .script .Script ;
28
29
import org .bitcoinj .script .Script .ScriptType ;
30
+ import org .bitcoinj .script .ScriptBuilder ;
31
+ import org .bitcoinj .script .ScriptPattern ;
29
32
30
33
/**
31
34
* <p>A Bitcoin address looks like 1MsScoe2fTJoq4ZPdQgqyhgWeoNamYPevy and is derived from an elliptic curve public key
@@ -50,7 +53,7 @@ public class LegacyAddress extends Address {
50
53
/**
51
54
* Private constructor. Use {@link #fromBase58(NetworkParameters, String)},
52
55
* {@link #fromPubKeyHash(NetworkParameters, byte[])}, {@link #fromScriptHash(NetworkParameters, byte[])} or
53
- * {@link #fromKey(NetworkParameters, ECKey)}.
56
+ * {@link #fromKey(NetworkParameters, ECKey, ScriptType )}.
54
57
*
55
58
* @param params
56
59
* network this address is valid for
@@ -91,8 +94,15 @@ public static LegacyAddress fromPubKeyHash(NetworkParameters params, byte[] hash
91
94
* only the public part is used
92
95
* @return constructed address
93
96
*/
94
- public static LegacyAddress fromKey (NetworkParameters params , ECKey key ) {
95
- return fromPubKeyHash (params , key .getPubKeyHash ());
97
+ public static LegacyAddress fromKey (NetworkParameters params , ECKey key , ScriptType outputScriptType ) {
98
+ if (outputScriptType == ScriptType .P2PKH ) {
99
+ return fromPubKeyHash (params , key .getPubKeyHash ());
100
+ } else if (outputScriptType == ScriptType .P2SH_P2WPKH ) {
101
+ Script script = ScriptBuilder .createP2SHP2WPKHOutputScript (key );
102
+ return fromScriptHash (params , ScriptPattern .extractHashFromP2SH (script ));
103
+ } else {
104
+ throw new IllegalArgumentException ("Prohibited output script type: " + outputScriptType );
105
+ }
96
106
}
97
107
98
108
/**
0 commit comments