Search in sources :

Example 36 with ECKey

use of com.sparrowwallet.drongo.crypto.ECKey in project drongo by sparrowwallet.

the class TransactionTest method verifyReconstructedTxP2WPKH.

@Test
public void verifyReconstructedTxP2WPKH() throws NonStandardScriptException, IOException, InvalidAddressException {
    String spent0Hex = "02000000000101ba704c9e8a5981f2b2682312bf3632a3d4f99c5d6ee7dbf8f8135804459890730100000000ffffffff02be71b80c000000001600142db5decf53c76ecfc3ee3ba95909872fad4a0aefcfb81600000000001976a91429418cb34cec93a0a9f3bcd81ff1550cbdca002e88ac02473044022004c576c0f1cddc2d85a2e6ea620f051790415155d88e05d859b9f9347751963c02204a31f40a03f9da9b6a0f763b3ad852576383c74ceb4ed9549c198f37fc4b19b6012103fda72b0f00ba00675b1bc911c40e3fb708bb64548b79b6497e11b243c46f46ab00000000";
    Transaction spent2Transaction = new Transaction(Utils.hexToBytes(spent0Hex));
    TransactionOutput spent0Output = spent2Transaction.getOutputs().get(0);
    ScriptType spent0ScriptType = ScriptType.getType(spent0Output.getScript());
    Assert.assertEquals(ScriptType.P2WPKH, spent0ScriptType);
    String spendingHex = "020000000001017ea85b784c10a639517cc2d8ed4f5dcc261149aed9efc379ce66b9baed1259b70000000000ffffffff02de879c0c000000001600142db5decf53c76ecfc3ee3ba95909872fad4a0aef7b8f1b000000000017a914f85d965a9244e89bdb1925d62a9fd06b602b8962870247304402203a00ecd77a3051e924cc5a42b7b023a5abb17aea00365be49a75a5f5d9057c8702201e889ece99b24b8d44d63b4d727e40c88882d98adee937af9e4543f52de79ac8012103fda72b0f00ba00675b1bc911c40e3fb708bb64548b79b6497e11b243c46f46ab00000000";
    Transaction spendingTransaction = new Transaction(Utils.hexToBytes(spendingHex));
    TransactionInput input0 = spendingTransaction.getInputs().get(0);
    TransactionWitness witness0 = input0.getWitness();
    TransactionSignature signature0 = TransactionSignature.decodeFromBitcoin(witness0.getPushes().get(0), false);
    ECKey key0 = ECKey.fromPublicOnly(witness0.getPushes().get(1));
    Transaction transaction = new Transaction();
    transaction.setVersion(2);
    transaction.setSegwitFlag(1);
    spent0ScriptType.addSpendingInput(transaction, spent0Output, key0, signature0);
    transaction.addOutput(211584990, Address.fromString("bc1q9k6aan6ncahvlslw8w54jzv897k55zh077un6s"));
    transaction.addOutput(1806203, Address.fromString("3QLFcgKFNzo262FYRFgGfrUNiUurpQbDZv"));
    Assert.assertEquals(spendingTransaction.getLength(), transaction.getLength());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    transaction.bitcoinSerializeToStream(baos);
    String constructedHex = Utils.bytesToHex(baos.toByteArray());
    Assert.assertEquals(spendingHex, constructedHex);
}
Also used : ECKey(com.sparrowwallet.drongo.crypto.ECKey) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 37 with ECKey

use of com.sparrowwallet.drongo.crypto.ECKey in project drongo by sparrowwallet.

the class TransactionTest method verifyP2WPKH.

@Test
public void verifyP2WPKH() {
    String hex = "0100000002fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f0000000000eeffffffef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a0100000000ffffffff02202cb206000000001976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac9093510d000000001976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac11000000";
    Transaction transaction = new Transaction(Utils.hexToBytes(hex));
    ECKey pubKey = ECKey.fromPublicOnly(Utils.hexToBytes("025476c2e83188368da1ff3e292e7acafcdb3566bb0ad253f62fc70f07aeee6357"));
    Sha256Hash hash = transaction.hashForWitnessSignature(1, ScriptType.P2PKH.getOutputScript(pubKey.getPubKeyHash()), 600000000L, SigHash.ALL);
    TransactionSignature signature = TransactionSignature.decodeFromBitcoin(Utils.hexToBytes("304402203609e17b84f6a7d30c80bfa610b5b4542f32a8a0d5447a12fb1366d7f01cc44a0220573a954c4518331561406f90300e8f3358f51928d43c212a8caed02de67eebee"), false);
    Assert.assertTrue(pubKey.verify(hash, signature));
}
Also used : ECKey(com.sparrowwallet.drongo.crypto.ECKey) Test(org.junit.Test)

Example 38 with ECKey

use of com.sparrowwallet.drongo.crypto.ECKey in project drongo by sparrowwallet.

the class TransactionTest method verifyP2WSHSigHashSingle.

@Test
public void verifyP2WSHSigHashSingle() {
    String hex = "0100000002fe3dc9208094f3ffd12645477b3dc56f60ec4fa8e6f5d67c565d1c6b9216b36e0000000000ffffffff0815cf020f013ed6cf91d29f4202e8a58726b1ac6c79da47c23d1bee0a6925f80000000000ffffffff0100f2052a010000001976a914a30741f8145e5acadf23f751864167f32e0963f788ac00000000";
    Transaction transaction = new Transaction(Utils.hexToBytes(hex));
    ECKey pubKey = ECKey.fromPublicOnly(Utils.hexToBytes("026dccc749adc2a9d0d89497ac511f760f45c47dc5ed9cf352a58ac706453880ae"));
    Script script = new Script(Utils.hexToBytes("21026dccc749adc2a9d0d89497ac511f760f45c47dc5ed9cf352a58ac706453880aeadab210255a9626aebf5e29c0e6538428ba0d1dcf6ca98ffdf086aa8ced5e0d0215ea465ac"));
    Sha256Hash hash = transaction.hashForWitnessSignature(1, script, 4900000000L, SigHash.SINGLE);
    TransactionSignature signature = TransactionSignature.decodeFromBitcoin(Utils.hexToBytes("3044022027dc95ad6b740fe5129e7e62a75dd00f291a2aeb1200b84b09d9e3789406b6c002201a9ecd315dd6a0e632ab20bbb98948bc0c6fb204f2c286963bb48517a7058e2703"), true);
    Assert.assertTrue(pubKey.verify(hash, signature));
}
Also used : ECKey(com.sparrowwallet.drongo.crypto.ECKey) Test(org.junit.Test)

Example 39 with ECKey

use of com.sparrowwallet.drongo.crypto.ECKey in project drongo by sparrowwallet.

the class TransactionTest method verifyP2WSHSigHashSingleAnyoneCanPay.

@Test
public void verifyP2WSHSigHashSingleAnyoneCanPay() {
    String hex = "0100000002e9b542c5176808107ff1df906f46bb1f2583b16112b95ee5380665ba7fcfc0010000000000ffffffff80e68831516392fcd100d186b3c2c7b95c80b53c77e77c35ba03a66b429a2a1b0000000000ffffffff0280969800000000001976a914de4b231626ef508c9a74a8517e6783c0546d6b2888ac80969800000000001976a9146648a8cd4531e1ec47f35916de8e259237294d1e88ac00000000";
    Transaction transaction = new Transaction(Utils.hexToBytes(hex));
    ECKey pubKey = ECKey.fromPublicOnly(Utils.hexToBytes("0392972e2eb617b2388771abe27235fd5ac44af8e61693261550447a4c3e39da98"));
    Script script = new Script(Utils.hexToBytes("68210392972e2eb617b2388771abe27235fd5ac44af8e61693261550447a4c3e39da98ac"));
    Sha256Hash hash = transaction.hashForWitnessSignature(1, script, 16777215L, SigHash.ANYONECANPAY_SINGLE);
    TransactionSignature signature = TransactionSignature.decodeFromBitcoin(Utils.hexToBytes("30440220032521802a76ad7bf74d0e2c218b72cf0cbc867066e2e53db905ba37f130397e02207709e2188ed7f08f4c952d9d13986da504502b8c3be59617e043552f506c46ff83"), true);
    Assert.assertTrue(pubKey.verify(hash, signature));
}
Also used : ECKey(com.sparrowwallet.drongo.crypto.ECKey) Test(org.junit.Test)

Example 40 with ECKey

use of com.sparrowwallet.drongo.crypto.ECKey in project drongo by sparrowwallet.

the class TransactionTest method verifyP2SHP2WSHSigHashAllAnyoneCanPay.

@Test
public void verifyP2SHP2WSHSigHashAllAnyoneCanPay() {
    String hex = "010000000136641869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e0100000000ffffffff0200e9a435000000001976a914389ffce9cd9ae88dcc0631e88a821ffdbe9bfe2688acc0832f05000000001976a9147480a33f950689af511e6e84c138dbbd3c3ee41588ac00000000";
    Transaction transaction = new Transaction(Utils.hexToBytes(hex));
    ECKey pubKey = ECKey.fromPublicOnly(Utils.hexToBytes("033400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f4"));
    Script script = new Script(Utils.hexToBytes("56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a21033400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b661b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56ae"));
    Sha256Hash hash = transaction.hashForWitnessSignature(0, script, 987654321L, SigHash.ANYONECANPAY_ALL);
    TransactionSignature signature = TransactionSignature.decodeFromBitcoin(Utils.hexToBytes("3045022100fbefd94bd0a488d50b79102b5dad4ab6ced30c4069f1eaa69a4b5a763414067e02203156c6a5c9cf88f91265f5a942e96213afae16d83321c8b31bb342142a14d16381"), true);
    Assert.assertTrue(pubKey.verify(hash, signature));
}
Also used : ECKey(com.sparrowwallet.drongo.crypto.ECKey) Test(org.junit.Test)

Aggregations

ECKey (com.sparrowwallet.drongo.crypto.ECKey)45 Test (org.junit.Test)22 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 Address (com.sparrowwallet.drongo.address.Address)6 ChildNumber (com.sparrowwallet.drongo.crypto.ChildNumber)6 PaymentCode (com.sparrowwallet.drongo.bip47.PaymentCode)5 PSBT (com.sparrowwallet.drongo.psbt.PSBT)5 PSBTInput (com.sparrowwallet.drongo.psbt.PSBTInput)5 InvalidAddressException (com.sparrowwallet.drongo.address.InvalidAddressException)4 ScriptType (com.sparrowwallet.drongo.protocol.ScriptType)4 SecureString (com.sparrowwallet.drongo.SecureString)3 SecretPoint (com.sparrowwallet.drongo.bip47.SecretPoint)3 DeterministicKey (com.sparrowwallet.drongo.crypto.DeterministicKey)3 SchnorrSignature (com.sparrowwallet.drongo.crypto.SchnorrSignature)3 PSBTOutput (com.sparrowwallet.drongo.psbt.PSBTOutput)3 StandardCharsets (java.nio.charset.StandardCharsets)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 Subscribe (com.google.common.eventbus.Subscribe)2 Files (com.google.common.io.Files)2