use of com.sparrowwallet.drongo.crypto.ECKey in project drongo by sparrowwallet.
the class TransactionTest method verifyReconstructedTxP2PK.
@Test
public void verifyReconstructedTxP2PK() throws NonStandardScriptException, IOException, InvalidAddressException {
String spent0Hex = "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804e6ed5b1b02b000ffffffff0100f2052a01000000434104ab3779ba979cd2f7d76fd1b6a57f42bf4bdd9210409a693a46d6d426c0ba021aca2f364ce5141b7721b47fb5f34ce7301abbab24c067048b721c633ae65e1af0ac00000000";
Transaction spent2Transaction = new Transaction(Utils.hexToBytes(spent0Hex));
TransactionOutput spent0Output = spent2Transaction.getOutputs().get(0);
ScriptType spent0ScriptType = ScriptType.getType(spent0Output.getScript());
Assert.assertEquals(ScriptType.P2PK, spent0ScriptType);
ECKey key0 = ECKey.fromPublicOnly(spent0Output.getScript().getChunks().get(0).getData());
String spendingHex = "010000000183ec1de4385a9617e0ea098ab28936e22757b370c73132028f9d7eed08478db70000000049483045022100c9455b5b385292ca8783201d030ed3e091a56d8bc4f030b7ed1eec20cf9110d2022020b3455f661d466b55cfaae0dbd6e6f861e82f64f225861217b07315167e1b1501ffffffff02005ed0b2000000001976a914aa1cfb996782dfac1b860599d512ed6967e2d25a88ac00943577000000001976a9145fbc2d7b0018d31b5e6628150e5485af17b3fd1988ac00000000";
Transaction spendingTransaction = new Transaction(Utils.hexToBytes(spendingHex));
TransactionInput input0 = spendingTransaction.getInputs().get(0);
TransactionSignature signature0 = input0.getScriptSig().getChunks().get(0).getSignature();
Transaction transaction = new Transaction();
spent0ScriptType.addSpendingInput(transaction, spent0Output, key0, signature0);
transaction.addOutput(3000000000L, Address.fromString("1GWUbNagGsvpwygRCjoczegGVDvpm5fLV8"));
transaction.addOutput(2000000000L, Address.fromString("19jCd38mHkNcXiGF4AjUCoJBSo7iqqjRHT"));
Assert.assertEquals(spendingTransaction.getLength(), transaction.getLength());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transaction.bitcoinSerializeToStream(baos);
String constructedHex = Utils.bytesToHex(baos.toByteArray());
Assert.assertEquals(spendingHex, constructedHex);
}
use of com.sparrowwallet.drongo.crypto.ECKey in project drongo by sparrowwallet.
the class TransactionTest method verifyP2SHP2WPKH.
@Test
public void verifyP2SHP2WPKH() {
String hex = "0100000001db6b1b20aa0fd7b23880be2ecbd4a98130974cf4748fb66092ac4d3ceb1a54770100000000feffffff02b8b4eb0b000000001976a914a457b684d7f0d539a46a45bbc043f35b59d0d96388ac0008af2f000000001976a914fd270b1ee6abcaea97fea7ad0402e8bd8ad6d77c88ac92040000";
Transaction transaction = new Transaction(Utils.hexToBytes(hex));
ECKey pubKey = ECKey.fromPublicOnly(Utils.hexToBytes("03ad1d8e89212f0b92c74d23bb710c00662ad1470198ac48c43f7d6f93a2a26873"));
Sha256Hash hash = transaction.hashForWitnessSignature(0, ScriptType.P2PKH.getOutputScript(pubKey.getPubKeyHash()), 1000000000L, SigHash.ALL);
TransactionSignature signature = TransactionSignature.decodeFromBitcoin(Utils.hexToBytes("3044022047ac8e878352d3ebbde1c94ce3a10d057c24175747116f8288e5d794d12d482f0220217f36a485cae903c713331d877c1f64677e3622ad4010726870540656fe9dcb01"), true);
Assert.assertTrue(pubKey.verify(hash, signature));
}
use of com.sparrowwallet.drongo.crypto.ECKey in project drongo by sparrowwallet.
the class TransactionTest method signBip340.
@Test
public void signBip340() {
ECKey privKey = ECKey.fromPrivate(Utils.hexToBytes("0000000000000000000000000000000000000000000000000000000000000003"));
Assert.assertEquals("F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9", Utils.bytesToHex(privKey.getPubKeyXCoord()).toUpperCase());
SchnorrSignature sig = privKey.signSchnorr(Sha256Hash.ZERO_HASH);
Assert.assertEquals("E907831F80848D1069A5371B402410364BDF1C5F8307B0084C55F1CE2DCA821525F66A4A85EA8B71E482A74F382D2CE5EBEEE8FDB2172F477DF4900D310536C0", Utils.bytesToHex(sig.encode()).toUpperCase());
}
use of com.sparrowwallet.drongo.crypto.ECKey in project drongo by sparrowwallet.
the class TransactionTest method verifyP2SHP2WSHSigHashNone.
@Test
public void verifyP2SHP2WSHSigHashNone() {
String hex = "010000000136641869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e0100000000ffffffff0200e9a435000000001976a914389ffce9cd9ae88dcc0631e88a821ffdbe9bfe2688acc0832f05000000001976a9147480a33f950689af511e6e84c138dbbd3c3ee41588ac00000000";
Transaction transaction = new Transaction(Utils.hexToBytes(hex));
ECKey pubKey = ECKey.fromPublicOnly(Utils.hexToBytes("03b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b"));
Script script = new Script(Utils.hexToBytes("56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a21033400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b661b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56ae"));
Sha256Hash hash = transaction.hashForWitnessSignature(0, script, 987654321L, SigHash.NONE);
TransactionSignature signature = TransactionSignature.decodeFromBitcoin(Utils.hexToBytes("3044022068c7946a43232757cbdf9176f009a928e1cd9a1a8c212f15c1e11ac9f2925d9002205b75f937ff2f9f3c1246e547e54f62e027f64eefa2695578cc6432cdabce271502"), true);
Assert.assertTrue(pubKey.verify(hash, signature));
}
use of com.sparrowwallet.drongo.crypto.ECKey in project drongo by sparrowwallet.
the class TransactionTest method verifyP2SHP2WSHSigHashAll.
@Test
public void verifyP2SHP2WSHSigHashAll() {
String hex = "010000000136641869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e0100000000ffffffff0200e9a435000000001976a914389ffce9cd9ae88dcc0631e88a821ffdbe9bfe2688acc0832f05000000001976a9147480a33f950689af511e6e84c138dbbd3c3ee41588ac00000000";
Transaction transaction = new Transaction(Utils.hexToBytes(hex));
ECKey pubKey = ECKey.fromPublicOnly(Utils.hexToBytes("0307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba3"));
Script script = new Script(Utils.hexToBytes("56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a21033400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b661b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56ae"));
Sha256Hash hash = transaction.hashForWitnessSignature(0, script, 987654321L, SigHash.ALL);
TransactionSignature signature = TransactionSignature.decodeFromBitcoin(Utils.hexToBytes("304402206ac44d672dac41f9b00e28f4df20c52eeb087207e8d758d76d92c6fab3b73e2b0220367750dbbe19290069cba53d096f44530e4f98acaa594810388cf7409a1870ce01"), true);
Assert.assertTrue(pubKey.verify(hash, signature));
}
Aggregations