mirror of
https://github.com/sparrowwallet/drongo.git
synced 2025-11-05 11:56:38 +00:00
Change to Groestlcoin specific hashes for tx, addresses, signatures
This commit is contained in:
parent
5ffda9d1f0
commit
f56f4e12da
3 changed files with 14 additions and 12 deletions
|
|
@ -719,7 +719,7 @@ public class ECKey implements EncryptableItem {
|
||||||
*/
|
*/
|
||||||
public String signMessage(String message, ScriptType scriptType, Key aesKey) throws KeyCrypterException {
|
public String signMessage(String message, ScriptType scriptType, Key aesKey) throws KeyCrypterException {
|
||||||
byte[] data = formatMessageForSigning(message);
|
byte[] data = formatMessageForSigning(message);
|
||||||
Sha256Hash hash = Sha256Hash.twiceOf(data);
|
Sha256Hash hash = Sha256Hash.of(data);
|
||||||
ECDSASignature sig = sign(hash, aesKey);
|
ECDSASignature sig = sign(hash, aesKey);
|
||||||
byte recId = findRecoveryId(hash, sig);
|
byte recId = findRecoveryId(hash, sig);
|
||||||
int headerByte = recId + getSigningTypeConstant(scriptType);
|
int headerByte = recId + getSigningTypeConstant(scriptType);
|
||||||
|
|
@ -785,7 +785,7 @@ public class ECKey implements EncryptableItem {
|
||||||
byte[] messageBytes = formatMessageForSigning(message);
|
byte[] messageBytes = formatMessageForSigning(message);
|
||||||
// Note that the C++ code doesn't actually seem to specify any character encoding. Presumably it's whatever
|
// Note that the C++ code doesn't actually seem to specify any character encoding. Presumably it's whatever
|
||||||
// JSON-SPIRIT hands back. Assume UTF-8 for now.
|
// JSON-SPIRIT hands back. Assume UTF-8 for now.
|
||||||
Sha256Hash messageHash = Sha256Hash.twiceOf(messageBytes);
|
Sha256Hash messageHash = Sha256Hash.of(messageBytes);
|
||||||
boolean compressed = false;
|
boolean compressed = false;
|
||||||
if(header >= 39) { // this is a bech32 signature
|
if(header >= 39) { // this is a bech32 signature
|
||||||
header -= 12;
|
header -= 12;
|
||||||
|
|
@ -1156,7 +1156,7 @@ public class ECKey implements EncryptableItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The string that prefixes all text messages signed using Bitcoin keys. */
|
/** The string that prefixes all text messages signed using Bitcoin keys. */
|
||||||
private static final String BITCOIN_SIGNED_MESSAGE_HEADER = "Bitcoin Signed Message:\n";
|
private static final String BITCOIN_SIGNED_MESSAGE_HEADER = "GroestlCoin Signed Message:\n";
|
||||||
private static final byte[] BITCOIN_SIGNED_MESSAGE_HEADER_BYTES = BITCOIN_SIGNED_MESSAGE_HEADER.getBytes(StandardCharsets.UTF_8);
|
private static final byte[] BITCOIN_SIGNED_MESSAGE_HEADER_BYTES = BITCOIN_SIGNED_MESSAGE_HEADER.getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
package com.sparrowwallet.drongo.protocol;
|
package com.sparrowwallet.drongo.protocol;
|
||||||
|
|
||||||
|
import com.sparrowwallet.drongo.crypto.Groestl;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
|
@ -99,7 +101,7 @@ public class Base58 {
|
||||||
// data bytes + 4 bytes check code (a truncated hash)
|
// data bytes + 4 bytes check code (a truncated hash)
|
||||||
byte[] addressBytes = new byte[payload.length + 4];
|
byte[] addressBytes = new byte[payload.length + 4];
|
||||||
System.arraycopy(payload, 0, addressBytes, 0, payload.length);
|
System.arraycopy(payload, 0, addressBytes, 0, payload.length);
|
||||||
byte[] checksum = Sha256Hash.hashTwice(addressBytes, 0, payload.length);
|
byte[] checksum = Groestl.digest(addressBytes, 0, payload.length);
|
||||||
System.arraycopy(checksum, 0, addressBytes, payload.length, 4);
|
System.arraycopy(checksum, 0, addressBytes, payload.length, 4);
|
||||||
return Base58.encode(addressBytes);
|
return Base58.encode(addressBytes);
|
||||||
}
|
}
|
||||||
|
|
@ -184,7 +186,7 @@ public class Base58 {
|
||||||
throw new ProtocolException("Input too short: " + decoded.length);
|
throw new ProtocolException("Input too short: " + decoded.length);
|
||||||
byte[] data = Arrays.copyOfRange(decoded, 0, decoded.length - 4);
|
byte[] data = Arrays.copyOfRange(decoded, 0, decoded.length - 4);
|
||||||
byte[] checksum = Arrays.copyOfRange(decoded, decoded.length - 4, decoded.length);
|
byte[] checksum = Arrays.copyOfRange(decoded, decoded.length - 4, decoded.length);
|
||||||
byte[] actualChecksum = Arrays.copyOfRange(Sha256Hash.hashTwice(data), 0, 4);
|
byte[] actualChecksum = Arrays.copyOfRange(Groestl.digest(data), 0, 4);
|
||||||
if (!Arrays.equals(checksum, actualChecksum))
|
if (!Arrays.equals(checksum, actualChecksum))
|
||||||
throw new ProtocolException("Invalid checksum");
|
throw new ProtocolException("Invalid checksum");
|
||||||
return data;
|
return data;
|
||||||
|
|
|
||||||
|
|
@ -129,7 +129,7 @@ public class Transaction extends ChildMessage {
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e); // cannot happen
|
throw new RuntimeException(e); // cannot happen
|
||||||
}
|
}
|
||||||
return Sha256Hash.wrapReversed(Sha256Hash.hashTwice(stream.toByteArray()));
|
return Sha256Hash.wrapReversed(Sha256Hash.hash(stream.toByteArray()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSegwit() {
|
public boolean isSegwit() {
|
||||||
|
|
@ -522,7 +522,7 @@ public class Transaction extends ChildMessage {
|
||||||
uint32ToByteStreamLE(0x000000ff & sigHashType, bos);
|
uint32ToByteStreamLE(0x000000ff & sigHashType, bos);
|
||||||
// Note that this is NOT reversed to ensure it will be signed correctly. If it were to be printed out
|
// Note that this is NOT reversed to ensure it will be signed correctly. If it were to be printed out
|
||||||
// however then we would expect that it is IS reversed.
|
// however then we would expect that it is IS reversed.
|
||||||
Sha256Hash hash = Sha256Hash.twiceOf(bos.toByteArray());
|
Sha256Hash hash = Sha256Hash.of(bos.toByteArray());
|
||||||
bos.close();
|
bos.close();
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
|
|
@ -562,7 +562,7 @@ public class Transaction extends ChildMessage {
|
||||||
bosHashPrevouts.write(this.inputs.get(i).getOutpoint().getHash().getReversedBytes());
|
bosHashPrevouts.write(this.inputs.get(i).getOutpoint().getHash().getReversedBytes());
|
||||||
uint32ToByteStreamLE(this.inputs.get(i).getOutpoint().getIndex(), bosHashPrevouts);
|
uint32ToByteStreamLE(this.inputs.get(i).getOutpoint().getIndex(), bosHashPrevouts);
|
||||||
}
|
}
|
||||||
hashPrevouts = Sha256Hash.hashTwice(bosHashPrevouts.toByteArray());
|
hashPrevouts = Sha256Hash.hash(bosHashPrevouts.toByteArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!anyoneCanPay && signAll) {
|
if(!anyoneCanPay && signAll) {
|
||||||
|
|
@ -570,7 +570,7 @@ public class Transaction extends ChildMessage {
|
||||||
for(int i = 0; i < this.inputs.size(); ++i) {
|
for(int i = 0; i < this.inputs.size(); ++i) {
|
||||||
uint32ToByteStreamLE(this.inputs.get(i).getSequenceNumber(), bosSequence);
|
uint32ToByteStreamLE(this.inputs.get(i).getSequenceNumber(), bosSequence);
|
||||||
}
|
}
|
||||||
hashSequence = Sha256Hash.hashTwice(bosSequence.toByteArray());
|
hashSequence = Sha256Hash.hash(bosSequence.toByteArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
if(signAll) {
|
if(signAll) {
|
||||||
|
|
@ -580,13 +580,13 @@ public class Transaction extends ChildMessage {
|
||||||
bosHashOutputs.write(new VarInt(this.outputs.get(i).getScriptBytes().length).encode());
|
bosHashOutputs.write(new VarInt(this.outputs.get(i).getScriptBytes().length).encode());
|
||||||
bosHashOutputs.write(this.outputs.get(i).getScriptBytes());
|
bosHashOutputs.write(this.outputs.get(i).getScriptBytes());
|
||||||
}
|
}
|
||||||
hashOutputs = Sha256Hash.hashTwice(bosHashOutputs.toByteArray());
|
hashOutputs = Sha256Hash.hash(bosHashOutputs.toByteArray());
|
||||||
} else if(basicSigHashType == SigHash.SINGLE.value && inputIndex < outputs.size()) {
|
} else if(basicSigHashType == SigHash.SINGLE.value && inputIndex < outputs.size()) {
|
||||||
ByteArrayOutputStream bosHashOutputs = new UnsafeByteArrayOutputStream(256);
|
ByteArrayOutputStream bosHashOutputs = new UnsafeByteArrayOutputStream(256);
|
||||||
uint64ToByteStreamLE(BigInteger.valueOf(this.outputs.get(inputIndex).getValue()), bosHashOutputs);
|
uint64ToByteStreamLE(BigInteger.valueOf(this.outputs.get(inputIndex).getValue()), bosHashOutputs);
|
||||||
bosHashOutputs.write(new VarInt(this.outputs.get(inputIndex).getScriptBytes().length).encode());
|
bosHashOutputs.write(new VarInt(this.outputs.get(inputIndex).getScriptBytes().length).encode());
|
||||||
bosHashOutputs.write(this.outputs.get(inputIndex).getScriptBytes());
|
bosHashOutputs.write(this.outputs.get(inputIndex).getScriptBytes());
|
||||||
hashOutputs = Sha256Hash.hashTwice(bosHashOutputs.toByteArray());
|
hashOutputs = Sha256Hash.hash(bosHashOutputs.toByteArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32ToByteStreamLE(version, bos);
|
uint32ToByteStreamLE(version, bos);
|
||||||
|
|
@ -606,6 +606,6 @@ public class Transaction extends ChildMessage {
|
||||||
throw new RuntimeException(e); // Cannot happen.
|
throw new RuntimeException(e); // Cannot happen.
|
||||||
}
|
}
|
||||||
|
|
||||||
return Sha256Hash.twiceOf(bos.toByteArray());
|
return Sha256Hash.of(bos.toByteArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue