mirror of
https://github.com/sparrowwallet/drongo.git
synced 2024-12-27 02:26:44 +00:00
better xpub header type support
This commit is contained in:
parent
0fbce035a3
commit
ea5101dff7
1 changed files with 35 additions and 27 deletions
|
@ -107,7 +107,7 @@ public class ExtendedKey {
|
||||||
throw new IllegalArgumentException("Found unexpected data in key");
|
throw new IllegalArgumentException("Found unexpected data in key");
|
||||||
}
|
}
|
||||||
|
|
||||||
if(header.isPrivate()) {
|
if(header.isPrivateKey()) {
|
||||||
DeterministicKey prvKey = HDKeyDerivation.createMasterPrivKeyFromBytes(Arrays.copyOfRange(data, 1, 33), chainCode, path);
|
DeterministicKey prvKey = HDKeyDerivation.createMasterPrivKeyFromBytes(Arrays.copyOfRange(data, 1, 33), chainCode, path);
|
||||||
return new ExtendedKey(prvKey, parentFingerprint, childNumber);
|
return new ExtendedKey(prvKey, parentFingerprint, childNumber);
|
||||||
} else {
|
} else {
|
||||||
|
@ -145,35 +145,39 @@ public class ExtendedKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Header {
|
public enum Header {
|
||||||
xprv("xprv", 0x0488ADE4, ScriptType.P2PKH),
|
xprv("xprv", 0x0488ADE4, ScriptType.P2PKH, true, true),
|
||||||
xpub("xpub", 0x0488B21E, ScriptType.P2PKH),
|
xpub("xpub", 0x0488B21E, ScriptType.P2PKH, false, true),
|
||||||
yprv("yprv", 0x049D7878, ScriptType.P2SH_P2WPKH),
|
yprv("yprv", 0x049D7878, ScriptType.P2SH_P2WPKH, true, true),
|
||||||
ypub("ypub", 0x049D7CB2, ScriptType.P2SH_P2WPKH),
|
ypub("ypub", 0x049D7CB2, ScriptType.P2SH_P2WPKH, false, true),
|
||||||
zprv("zprv", 0x04b2430c, ScriptType.P2WPKH),
|
zprv("zprv", 0x04b2430c, ScriptType.P2WPKH, true, true),
|
||||||
zpub("zpub", 0x04B24746, ScriptType.P2WPKH),
|
zpub("zpub", 0x04B24746, ScriptType.P2WPKH, false, true),
|
||||||
Yprv("Yprv", 0x0295b005, ScriptType.P2SH_P2WSH),
|
Yprv("Yprv", 0x0295b005, ScriptType.P2SH_P2WSH, true, true),
|
||||||
Ypub("Ypub", 0x0295b43f, ScriptType.P2SH_P2WSH),
|
Ypub("Ypub", 0x0295b43f, ScriptType.P2SH_P2WSH, false, true),
|
||||||
Zprv("Zprv", 0x02aa7a99, ScriptType.P2WSH),
|
Zprv("Zprv", 0x02aa7a99, ScriptType.P2WSH, true, true),
|
||||||
Zpub("Zpub", 0x02aa7ed3, ScriptType.P2WSH),
|
Zpub("Zpub", 0x02aa7ed3, ScriptType.P2WSH, false, true),
|
||||||
tpub("tpub", 0x043587cf, ScriptType.P2PKH),
|
tprv("tprv", 0x04358394, ScriptType.P2PKH, true, false),
|
||||||
tprv("tprv", 0x04358394, ScriptType.P2PKH),
|
tpub("tpub", 0x043587cf, ScriptType.P2PKH, false, false),
|
||||||
uprv("uprv", 0x044a4e28, ScriptType.P2SH_P2WPKH),
|
uprv("uprv", 0x044a4e28, ScriptType.P2SH_P2WPKH, true, false),
|
||||||
upub("upub", 0x044a5262, ScriptType.P2SH_P2WPKH),
|
upub("upub", 0x044a5262, ScriptType.P2SH_P2WPKH, false, false),
|
||||||
vprv("vprv", 0x045f18bc, ScriptType.P2WPKH),
|
vprv("vprv", 0x045f18bc, ScriptType.P2WPKH, true, false),
|
||||||
vpub("vpub", 0x045f1cf6, ScriptType.P2WPKH),
|
vpub("vpub", 0x045f1cf6, ScriptType.P2WPKH, false, false),
|
||||||
Uprv("Uprv", 0x024285b5, ScriptType.P2SH_P2WSH),
|
Uprv("Uprv", 0x024285b5, ScriptType.P2SH_P2WSH, true, false),
|
||||||
Upub("Upub", 0x024289ef, ScriptType.P2SH_P2WSH),
|
Upub("Upub", 0x024289ef, ScriptType.P2SH_P2WSH, false, false),
|
||||||
Vprv("Vprv", 0x02575048, ScriptType.P2WSH),
|
Vprv("Vprv", 0x02575048, ScriptType.P2WSH, true, false),
|
||||||
Vpub("Vpub", 0x02575483, ScriptType.P2WSH);
|
Vpub("Vpub", 0x02575483, ScriptType.P2WSH, false, false);
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final int header;
|
private final int header;
|
||||||
private final ScriptType defaultScriptType;
|
private final ScriptType defaultScriptType;
|
||||||
|
private final boolean privateKey;
|
||||||
|
private final boolean mainnet;
|
||||||
|
|
||||||
Header(String name, int header, ScriptType defaultScriptType) {
|
Header(String name, int header, ScriptType defaultScriptType, boolean privateKey, boolean mainnet) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.header = header;
|
this.header = header;
|
||||||
this.defaultScriptType = defaultScriptType;
|
this.defaultScriptType = defaultScriptType;
|
||||||
|
this.privateKey = privateKey;
|
||||||
|
this.mainnet = mainnet;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
|
@ -188,6 +192,14 @@ public class ExtendedKey {
|
||||||
return defaultScriptType;
|
return defaultScriptType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isPrivateKey() {
|
||||||
|
return privateKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isMainnet() {
|
||||||
|
return mainnet;
|
||||||
|
}
|
||||||
|
|
||||||
public static Header fromExtendedKey(String xkey) {
|
public static Header fromExtendedKey(String xkey) {
|
||||||
for(Header extendedKeyHeader : Header.values()) {
|
for(Header extendedKeyHeader : Header.values()) {
|
||||||
if(xkey.startsWith(extendedKeyHeader.name)) {
|
if(xkey.startsWith(extendedKeyHeader.name)) {
|
||||||
|
@ -200,7 +212,7 @@ public class ExtendedKey {
|
||||||
|
|
||||||
public static Header fromScriptType(ScriptType scriptType, boolean privateKey) {
|
public static Header fromScriptType(ScriptType scriptType, boolean privateKey) {
|
||||||
for(Header header : Header.values()) {
|
for(Header header : Header.values()) {
|
||||||
if(header.defaultScriptType != null && header.defaultScriptType.equals(scriptType) && header.isPrivate() == privateKey) {
|
if(header.defaultScriptType != null && header.defaultScriptType.equals(scriptType) && header.isPrivateKey() == privateKey) {
|
||||||
return header;
|
return header;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -208,10 +220,6 @@ public class ExtendedKey {
|
||||||
return Header.xpub;
|
return Header.xpub;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isPrivate() {
|
|
||||||
return name.endsWith("prv");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Header getHeader(int header) {
|
public static Header getHeader(int header) {
|
||||||
for(Header extendedKeyHeader : Header.values()) {
|
for(Header extendedKeyHeader : Header.values()) {
|
||||||
if(header == extendedKeyHeader.header) {
|
if(header == extendedKeyHeader.header) {
|
||||||
|
|
Loading…
Reference in a new issue