use of com.sparrowwallet.drongo.crypto.ECKey in project drongo by sparrowwallet.
the class TransactionTest method verifyReconstructedTxP2SHP2WPKH.
@Test
public void verifyReconstructedTxP2SHP2WPKH() throws NonStandardScriptException, IOException, InvalidAddressException {
String spent0Hex = "010000000001014e6120e50f876635a088f825793487fc0467722dbddef14ca31ae3a88aef1fc901000000171600148edd09d1117175a77cf9d58e1cb9c96430903979fdffffff03a00f0000000000001976a914d83f1dc836e701d17b8adf2c5e9726a4af8078b088ac4e6a00000000000017a91450dab4502ba8881856259a45611e0e9d859e1b4787026206000000000017a914c8bfc88770631bfcee814b3524abba965bf7be048702483045022100c8deb4e97dec5a0107ca124a05f6484a107c7bf9c6d9ddc50b0b2ff4a7a9f632022040390473e032b9d5f38fe777c209bac820d804800ae0494bf7fd938eb8fd8a3f012102ada4557d88d3d7130bd7b28cc06397ed91d2a1d49d4280f08c76b7b947b769b600000000";
Transaction spent2Transaction = new Transaction(Utils.hexToBytes(spent0Hex));
TransactionOutput spent0Output = spent2Transaction.getOutputs().get(2);
ScriptType spent0ScriptType = ScriptType.getType(spent0Output.getScript());
Assert.assertEquals(ScriptType.P2SH, spent0ScriptType);
String spendingHex = "01000000000101e0a6a25fd728d9b755b4329acf66ae5f264e1bba763487b91410c36c85fb6e3802000000171600148edd09d1117175a77cf9d58e1cb9c96430903979fdffffff03a00f0000000000001976a914d83f1dc836e701d17b8adf2c5e9726a4af8078b088acf68205000000000017a914054e8b8fcd56228d18a4b3d4cc550cbad5f0c6a387dc6900000000000017a914c8bfc88770631bfcee814b3524abba965bf7be048702483045022100b6ecb3e7d4f607cc495bac79e2d5cf999cc347a51751b90e50505bf1a9153dcc02203bd844375f01183e7d08d90c6c18118dc8ac6b3686e5a22ec9a39205f632482b012102ada4557d88d3d7130bd7b28cc06397ed91d2a1d49d4280f08c76b7b947b769b600000000";
Transaction spendingTransaction = new Transaction(Utils.hexToBytes(spendingHex));
TransactionInput input0 = spendingTransaction.getInputs().get(0);
Script spendingScript = input0.getScriptSig();
TransactionWitness witness0 = input0.getWitness();
TransactionSignature signature0 = TransactionSignature.decodeFromBitcoin(witness0.getPushes().get(0), false);
ECKey pubKey0 = ECKey.fromPublicOnly(witness0.getPushes().get(1));
Transaction transaction = new Transaction();
transaction.setSegwitFlag(1);
TransactionInput input = ScriptType.P2SH_P2WPKH.addSpendingInput(transaction, spent0Output, pubKey0, signature0);
input.setSequenceNumber(TransactionInput.SEQUENCE_RBF_ENABLED);
transaction.addOutput(4000, Address.fromString("1LiQZqSwPqb615uyxDKTaN9Tg4CER98cgJ"));
transaction.addOutput(361206, Address.fromString("32B5Pv7Nvhh8iQ3Z2xK8cbKBW5f2bGMoqp"));
transaction.addOutput(27100, Address.fromString("3KzUpFMVKXiNETUy19VVW9Re5EimboDuyX"));
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 verifyP2SHP2WSHSigHashNoneAnyoneCanPay.
@Test
public void verifyP2SHP2WSHSigHashNoneAnyoneCanPay() {
String hex = "010000000136641869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e0100000000ffffffff0200e9a435000000001976a914389ffce9cd9ae88dcc0631e88a821ffdbe9bfe2688acc0832f05000000001976a9147480a33f950689af511e6e84c138dbbd3c3ee41588ac00000000";
Transaction transaction = new Transaction(Utils.hexToBytes(hex));
ECKey pubKey = ECKey.fromPublicOnly(Utils.hexToBytes("03a6d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac16"));
Script script = new Script(Utils.hexToBytes("56210307b8ae49ac90a048e9b53357a2354b3334e9c8bee813ecb98e99a7e07e8c3ba32103b28f0c28bfab54554ae8c658ac5c3e0ce6e79ad336331f78c428dd43eea8449b21034b8113d703413d57761b8b9781957b8c0ac1dfe69f492580ca4195f50376ba4a21033400f6afecb833092a9a21cfdf1ed1376e58c5d1f47de74683123987e967a8f42103a6d48b1131e94ba04d9737d61acdaa1322008af9602b3b14862c07a1789aac162102d8b661b0b3302ee2f162b09e07a55ad5dfbe673a9f01d9f0c19617681024306b56ae"));
Sha256Hash hash = transaction.hashForWitnessSignature(0, script, 987654321L, SigHash.ANYONECANPAY_NONE);
TransactionSignature signature = TransactionSignature.decodeFromBitcoin(Utils.hexToBytes("3045022100a5263ea0553ba89221984bd7f0b13613db16e7a70c549a86de0cc0444141a407022005c360ef0ae5a5d4f9f2f87a56c1546cc8268cab08c73501d6b3be2e1e1a8a0882"), true);
Assert.assertTrue(pubKey.verify(hash, signature));
}
use of com.sparrowwallet.drongo.crypto.ECKey in project drongo by sparrowwallet.
the class TransactionTest method verifyReconstructedTxP2WSH.
@Test
public void verifyReconstructedTxP2WSH() throws NonStandardScriptException, IOException, InvalidAddressException {
String spent0Hex = "01000000000101275f4973ebc66a9d44b0196ec33820ad29ee32bef55899f2e322782740afd55d0500000000ffffffff06001bb7000000000017a914f065c662326faa3acd1226817f5f48f3d9748afd87b9ca1e000000000017a9149d19a31ef45bf46dee11974dcf6253c287a5fa7d8780f0fa020000000017a914a1475b36de36df9b8bb484fdc27ce051ef04115887ea42ee000000000017a91479e90530f4ecdae0ce59bf6dc9a7260240a656da8700c2eb0b000000001976a9145e1eb0472895a56375936e9bbc851ff0239acc9d88acab71cf1d00000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d040047304402204226aa74970340e5f2b63f8a70081624df42fac85f75217794fc047f6173fa6602200f534a6948767d79f0d9980dfe0bf8fccac17726fec7a1dfe8a4b42eda19ae080147304402201be0ad255f70d7944d0019e62bfa68da7b97b292c6e17419e3fc203a50eac81e0220604e6467257f623e3d3fcec718725c33b13d3445bc70aed719c04e429bd52efe016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000";
Transaction spent2Transaction = new Transaction(Utils.hexToBytes(spent0Hex));
TransactionOutput spent0Output = spent2Transaction.getOutputs().get(5);
ScriptType spent0ScriptType = ScriptType.getType(spent0Output.getScript());
Assert.assertEquals(ScriptType.P2WSH, spent0ScriptType);
String spendingHex = "01000000000101b892f0a74954a730bc3e8a5a4341a144fc43dce4d9c2bc97dbdb13c501b067690500000000ffffffff032052a6000000000017a91485b5696f13edb4e9b2ac68f0de7a3e26e65c7c4e87208cd113000000001976a914c6872477e0d3f4bbd73cbaf4b9134f4204205e3888ac2bf7560900000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d040047304402205599051161390edd68d8ed01535a64f8f5d53f7418e73838c6fd6513670a095602200d7846aecf92765f4aa26da0f519f86d7b00cd29b9d43b8d73644a53975b94440147304402205cea311a37eb62219a75d4e05b513afd80a448b59caae99d4a9a3029d55dfd8d0220134656a5bcc2c5ec27c0f6e14f9e9212b0d5ca838fc7e5ac3699f8953fdafaf5016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000";
Transaction spendingTransaction = new Transaction(Utils.hexToBytes(spendingHex));
TransactionInput input0 = spendingTransaction.getInputs().get(0);
TransactionWitness witness0 = input0.getWitness();
TransactionSignature signature0 = TransactionSignature.decodeFromBitcoin(witness0.getPushes().get(1), false);
TransactionSignature signature1 = TransactionSignature.decodeFromBitcoin(witness0.getPushes().get(2), false);
Script witnessScript = new Script(witness0.getPushes().get(3));
ECKey key0 = witnessScript.getChunks().get(1).getPubKey();
ECKey key1 = witnessScript.getChunks().get(2).getPubKey();
ECKey key2 = witnessScript.getChunks().get(3).getPubKey();
Map<ECKey, TransactionSignature> pubKeySignatures = new TreeMap<>(new ECKey.LexicographicECKeyComparator());
pubKeySignatures.put(key0, signature0);
pubKeySignatures.put(key1, signature1);
pubKeySignatures.put(key2, null);
Transaction transaction = new Transaction();
transaction.setSegwitFlag(1);
spent0ScriptType.addMultisigSpendingInput(transaction, spent0Output, 2, pubKeySignatures);
transaction.addOutput(10900000, Address.fromString("3Dt17mpd8FDXBjP56rCD7a4Sx7wpL91uhn"));
transaction.addOutput(332500000, Address.fromString("1K6igqzm36x8jxRTavPhgWXLVcVZVDTGc9"));
transaction.addOutput(156694315, Address.fromString("bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej"));
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 PaymentCodeTest method testPaymentAddress.
@Test
public void testPaymentAddress() throws MnemonicException, InvalidPaymentCodeException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, NoSuchProviderException, NotSecp256k1Exception {
DeterministicSeed seed = new DeterministicSeed("response seminar brave tip suit recall often sound stick owner lottery motion", "", 0, DeterministicSeed.Type.BIP39);
Keystore keystore = Keystore.fromSeed(seed, List.of(new ChildNumber(47, true), ChildNumber.ZERO_HARDENED, ChildNumber.ZERO_HARDENED));
DeterministicKey privateKey = keystore.getExtendedPrivateKey().getKey(List.of(ChildNumber.ZERO_HARDENED, ChildNumber.ZERO));
PaymentCode paymentCodeBob = new PaymentCode("PM8TJS2JxQ5ztXUpBBRnpTbcUXbUHy2T1abfrb3KkAAtMEGNbey4oumH7Hc578WgQJhPjBxteQ5GHHToTYHE3A1w6p7tU6KSoFmWBVbFGjKPisZDbP97");
PaymentAddress paymentAddress0 = new PaymentAddress(paymentCodeBob, 0, privateKey.getPrivKeyBytes());
ECKey sendKey0 = paymentAddress0.getSendECKey();
Assert.assertEquals("141fi7TY3h936vRUKh1qfUZr8rSBuYbVBK", ScriptType.P2PKH.getAddress(sendKey0).toString());
PaymentAddress paymentAddress1 = new PaymentAddress(paymentCodeBob, 1, privateKey.getPrivKeyBytes());
ECKey sendKey1 = paymentAddress1.getSendECKey();
Assert.assertEquals("12u3Uued2fuko2nY4SoSFGCoGLCBUGPkk6", ScriptType.P2PKH.getAddress(sendKey1).toString());
}
use of com.sparrowwallet.drongo.crypto.ECKey in project drongo by sparrowwallet.
the class PaymentCodeTest method testChildWallet.
@Test
public void testChildWallet() throws MnemonicException, InvalidPaymentCodeException {
DeterministicSeed aliceSeed = new DeterministicSeed("response seminar brave tip suit recall often sound stick owner lottery motion", "", 0, DeterministicSeed.Type.BIP39);
Wallet aliceWallet = new Wallet();
aliceWallet.setPolicyType(PolicyType.SINGLE);
aliceWallet.setScriptType(ScriptType.P2PKH);
aliceWallet.getKeystores().add(Keystore.fromSeed(aliceSeed, aliceWallet.getScriptType().getDefaultDerivation()));
aliceWallet.setDefaultPolicy(Policy.getPolicy(PolicyType.SINGLE, ScriptType.P2PKH, aliceWallet.getKeystores(), 1));
PaymentCode paymentCodeBob = new PaymentCode("PM8TJS2JxQ5ztXUpBBRnpTbcUXbUHy2T1abfrb3KkAAtMEGNbey4oumH7Hc578WgQJhPjBxteQ5GHHToTYHE3A1w6p7tU6KSoFmWBVbFGjKPisZDbP97");
Wallet aliceBip47Wallet = aliceWallet.addChildWallet(paymentCodeBob, ScriptType.P2PKH, "Alice");
PaymentCode paymentCodeAlice = aliceBip47Wallet.getKeystores().get(0).getPaymentCode();
Assert.assertEquals(aliceWallet.getPaymentCode(), aliceBip47Wallet.getPaymentCode());
Assert.assertEquals("PM8TJTLJbPRGxSbc8EJi42Wrr6QbNSaSSVJ5Y3E4pbCYiTHUskHg13935Ubb7q8tx9GVbh2UuRnBc3WSyJHhUrw8KhprKnn9eDznYGieTzFcwQRya4GA", paymentCodeAlice.toString());
Assert.assertEquals("1JDdmqFLhpzcUwPeinhJbUPw4Co3aWLyzW", paymentCodeAlice.getNotificationAddress().toString());
WalletNode sendNode0 = aliceBip47Wallet.getFreshNode(KeyPurpose.SEND);
Address address0 = sendNode0.getAddress();
Assert.assertEquals("141fi7TY3h936vRUKh1qfUZr8rSBuYbVBK", address0.toString());
WalletNode sendNode1 = aliceBip47Wallet.getFreshNode(KeyPurpose.SEND, sendNode0);
Address address1 = sendNode1.getAddress();
Assert.assertEquals("12u3Uued2fuko2nY4SoSFGCoGLCBUGPkk6", address1.toString());
WalletNode sendNode2 = aliceBip47Wallet.getFreshNode(KeyPurpose.SEND, sendNode1);
Address address2 = sendNode2.getAddress();
Assert.assertEquals("1FsBVhT5dQutGwaPePTYMe5qvYqqjxyftc", address2.toString());
DeterministicSeed bobSeed = new DeterministicSeed("reward upper indicate eight swift arch injury crystal super wrestle already dentist", "", 0, DeterministicSeed.Type.BIP39);
Wallet bobWallet = new Wallet();
bobWallet.setPolicyType(PolicyType.SINGLE);
bobWallet.setScriptType(ScriptType.P2PKH);
bobWallet.getKeystores().add(Keystore.fromSeed(bobSeed, bobWallet.getScriptType().getDefaultDerivation()));
bobWallet.setDefaultPolicy(Policy.getPolicy(PolicyType.SINGLE, ScriptType.P2PKH, bobWallet.getKeystores(), 1));
Wallet bobBip47Wallet = bobWallet.addChildWallet(paymentCodeAlice, ScriptType.P2PKH, "Bob");
Assert.assertEquals(paymentCodeBob.toString(), bobBip47Wallet.getKeystores().get(0).getPaymentCode().toString());
Assert.assertEquals("1ChvUUvht2hUQufHBXF8NgLhW8SwE2ecGV", paymentCodeBob.getNotificationAddress().toString());
WalletNode receiveNode0 = bobBip47Wallet.getFreshNode(KeyPurpose.RECEIVE);
Address receiveAddress0 = receiveNode0.getAddress();
Assert.assertEquals("141fi7TY3h936vRUKh1qfUZr8rSBuYbVBK", receiveAddress0.toString());
WalletNode receiveNode1 = bobBip47Wallet.getFreshNode(KeyPurpose.RECEIVE, receiveNode0);
Address receiveAddress1 = receiveNode1.getAddress();
Assert.assertEquals("12u3Uued2fuko2nY4SoSFGCoGLCBUGPkk6", receiveAddress1.toString());
WalletNode receiveNode2 = bobBip47Wallet.getFreshNode(KeyPurpose.RECEIVE, receiveNode1);
Address receiveAddress2 = receiveNode2.getAddress();
Assert.assertEquals("1FsBVhT5dQutGwaPePTYMe5qvYqqjxyftc", receiveAddress2.toString());
ECKey privKey0 = bobWallet.getKeystores().get(0).getKey(receiveNode0);
ECKey pubKey0 = bobWallet.getKeystores().get(0).getPubKey(receiveNode0);
Assert.assertArrayEquals(privKey0.getPubKey(), pubKey0.getPubKey());
ECKey privKey1 = bobWallet.getKeystores().get(0).getKey(receiveNode1);
ECKey pubKey1 = bobWallet.getKeystores().get(0).getPubKey(receiveNode1);
Assert.assertArrayEquals(privKey1.getPubKey(), pubKey1.getPubKey());
}
Aggregations