public void ScriptSignTest() { ExtPrivkey privkey = new ExtPrivkey("xprv9zt1onyw8BdEf7SQ6wUVH3bQQdGD9iy9QzXveQQRhX7i5iUN7jZgLbqFEe491LfjozztYa6bJAGZ65GmDCNcbjMdjZcgmdisPJwVjcfcDhV"); Transaction tx = new Transaction("020000000201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff01000000000000000000000000000000000000000000000000000000000000000300000000ffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c00000000"); OutPoint outpoint2 = new OutPoint("0000000000000000000000000000000000000000000000000000000000000001", 3); long amount = 25000; Privkey privkey21 = privkey.DerivePrivkey(21).GetPrivkey(); Privkey privkey22 = privkey.DerivePrivkey(22).GetPrivkey(); Privkey privkey23 = privkey.DerivePrivkey(23).GetPrivkey(); Pubkey pubkey21 = privkey21.GetPubkey(); Pubkey pubkey22 = privkey22.GetPubkey(); Pubkey pubkey23 = privkey23.GetPubkey(); Script multisigScript = Script.CreateMultisigScript(2, new[] { pubkey21, pubkey22, pubkey23, }); SignatureHashType sighashType = new SignatureHashType(CfdSighashType.All, false); ByteData sighash = tx.GetSignatureHash(outpoint2, CfdHashType.P2shP2wsh, multisigScript, amount, sighashType); SignParameter sig22 = privkey22.CalculateEcSignature(sighash); SignParameter sig23 = privkey23.CalculateEcSignature(sighash); sig22.SetDerEncode(sighashType); sig22.SetRelatedPubkey(pubkey22); sig23.SetDerEncode(sighashType); sig23.SetRelatedPubkey(pubkey23); tx.AddScriptSign(outpoint2, CfdHashType.P2shP2wsh, new[] { new SignParameter(), sig22, sig23 }, multisigScript); output.WriteLine("tx:\n" + tx.ToHexString()); Assert.Equal("0200000000010201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff0100000000000000000000000000000000000000000000000000000000000000030000002322002064a0e02e723ce71d8f18441a39bedd5cefc9c5411c3045614c34bba1a8fbd94fffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c0004004730440220749cbe5080a3ce49c2a89f897be537b2b5449b75c64b57030dea1859b22c183f02200573f5be5170bfe4ca617edec0eb021638dd78b90209bbd8eede8a9e8138a32c01473044022019105df75884ff34111282f32c22986db295596983a87bf0df1d16905b4f9a50022075f8a2c8e3335a4265265b428df185fb045d9614ed1b08929bfa9f3f9d294a72016952210334bd4f1bab7f3e6f6bfc4a4aeaa890b858a9a146c6bd6bc5a3fbc00a12524ca72103ff743075c59596729d74b79694ca99b2c57bed6a77a06871b123b6e0d729823021036759d0dc7623e781de940a9bc9162f69c6ad68cc5be1c748e960ae4613e658e053ae00000000", tx.ToHexString()); }
public void EncodeToDerTest() { ByteData sig = new ByteData("773420c0ded41a55b1f1205cfb632f08f3f911a53e7338a0dac73ec6cbe3ca471907434d046185abedc5afddc2761a642bccc70af6d22b46394f1d04a8b24226"); ByteData derencData = SignParameter.EncodeToDer(sig, new SignatureHashType(CfdSighashType.All, false)); Assert.Equal("30440220773420c0ded41a55b1f1205cfb632f08f3f911a53e7338a0dac73ec6cbe3ca4702201907434d046185abedc5afddc2761a642bccc70af6d22b46394f1d04a8b2422601", derencData.ToHexString()); }
public void DecodeFromDerTest() { SignParameter signParam = SignParameter.DecodeFromDer( new ByteData("30440220773420c0ded41a55b1f1205cfb632f08f3f911a53e7338a0dac73ec6cbe3ca4702201907434d046185abedc5afddc2761a642bccc70af6d22b46394f1d04a8b2422601")); Assert.Equal("773420c0ded41a55b1f1205cfb632f08f3f911a53e7338a0dac73ec6cbe3ca471907434d046185abedc5afddc2761a642bccc70af6d22b46394f1d04a8b24226", signParam.GetData().ToHexString()); Assert.Equal(CfdSighashType.All, signParam.GetSignatureHashType().SighashType); Assert.False(signParam.GetSignatureHashType().IsSighashAnyoneCanPay); Assert.False(signParam.GetSignatureHashType().IsSighashRangeproof); signParam = SignParameter.DecodeFromDer( new ByteData("30440220773420c0ded41a55b1f1205cfb632f08f3f911a53e7338a0dac73ec6cbe3ca4702201907434d046185abedc5afddc2761a642bccc70af6d22b46394f1d04a8b2422642")); Assert.Equal("773420c0ded41a55b1f1205cfb632f08f3f911a53e7338a0dac73ec6cbe3ca471907434d046185abedc5afddc2761a642bccc70af6d22b46394f1d04a8b24226", signParam.GetData().ToHexString()); Assert.Equal(CfdSighashType.None, signParam.GetSignatureHashType().SighashType); Assert.False(signParam.GetSignatureHashType().IsSighashAnyoneCanPay); Assert.True(signParam.GetSignatureHashType().IsSighashRangeproof); signParam = SignParameter.DecodeFromDer( new ByteData("30440220773420c0ded41a55b1f1205cfb632f08f3f911a53e7338a0dac73ec6cbe3ca4702201907434d046185abedc5afddc2761a642bccc70af6d22b46394f1d04a8b2422683")); Assert.Equal("773420c0ded41a55b1f1205cfb632f08f3f911a53e7338a0dac73ec6cbe3ca471907434d046185abedc5afddc2761a642bccc70af6d22b46394f1d04a8b24226", signParam.GetData().ToHexString()); Assert.Equal(CfdSighashType.Single, signParam.GetSignatureHashType().SighashType); Assert.True(signParam.GetSignatureHashType().IsSighashAnyoneCanPay); Assert.False(signParam.GetSignatureHashType().IsSighashRangeproof); }
public void SignParameterTest() { SignParameter data = new SignParameter("0e68b55347fe37338beb3c28920267c5915a0c474d1dcafc65b087b9b3819cae6ae5e8fb12d669a63127abb4724070f8bd232a9efe3704e6544296a843a64f2c"); Assert.Equal("0e68b55347fe37338beb3c28920267c5915a0c474d1dcafc65b087b9b3819cae6ae5e8fb12d669a63127abb4724070f8bd232a9efe3704e6544296a843a64f2c", data.ToHexString()); SignParameter data2 = new SignParameter(data.GetBytes()); Assert.Equal(data.ToHexString(), data2.ToHexString()); ByteData normalize = SignParameter.NormalizeSignature(data.GetData()); Assert.Equal(data.ToHexString(), normalize.ToHexString()); }
public void CreateRawTransactionTest() { ExtPrivkey privkey = new ExtPrivkey("xprv9zt1onyw8BdEf7SQ6wUVH3bQQdGD9iy9QzXveQQRhX7i5iUN7jZgLbqFEe491LfjozztYa6bJAGZ65GmDCNcbjMdjZcgmdisPJwVjcfcDhV"); Address addr1 = new Address(privkey.DerivePubkey(1).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Regtest); Address addr2 = new Address(privkey.DerivePubkey(2).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Regtest); Address addr3 = new Address(privkey.DerivePubkey(3).GetPubkey(), CfdAddressType.P2wpkh, CfdNetworkType.Regtest); OutPoint outpoint1 = new OutPoint("0000000000000000000000000000000000000000000000000000000000000001", 2); OutPoint outpoint2 = new OutPoint("0000000000000000000000000000000000000000000000000000000000000001", 3); Transaction tx = new Transaction("02000000000000000000", new[] { new TxIn(outpoint1), new TxIn(outpoint2), }, new[] { new TxOut(10000, addr1.GetLockingScript()), new TxOut(10000, addr2.GetLockingScript()), }); tx.AddTxOut(50000, addr3); output.WriteLine("tx:\n" + tx.ToHexString()); Assert.Equal("020000000201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff01000000000000000000000000000000000000000000000000000000000000000300000000ffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c00000000", tx.ToHexString()); Privkey privkey1 = privkey.DerivePrivkey(11).GetPrivkey(); Pubkey pubkey1 = privkey1.GetPubkey(); SignatureHashType sighashType = new SignatureHashType(CfdSighashType.All, false); ByteData sighash = tx.GetSignatureHash(outpoint1, CfdHashType.P2wpkh, pubkey1, 50000, sighashType); SignParameter signature = privkey1.CalculateEcSignature(sighash); signature.SetDerEncode(sighashType); tx.AddSign(outpoint1, CfdHashType.P2wpkh, signature, true); tx.AddSign(outpoint1, CfdHashType.P2wpkh, new SignParameter(pubkey1.ToHexString()), false); output.WriteLine("tx:\n" + tx.ToHexString()); Assert.Equal("0200000000010201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff01000000000000000000000000000000000000000000000000000000000000000300000000ffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c02473044022034db802aad655cd9be589075fc8ef325b6ffb8c24e5b27eb87bde8ad38f5fd7a0220364c916c8e8fc0adf714d7148cd1c6dc6f3e67d55471e57233b1870c65ec2727012103782f0ea892d7000e5f0f82b6ff283382a76500137a542bb0a616530094a8f54c0000000000", tx.ToHexString()); Address addr11 = new Address(pubkey1, CfdAddressType.P2wpkh, CfdNetworkType.Regtest); try { tx.VerifySign(outpoint1, addr11, addr11.GetAddressType(), 50000); } catch (Exception e) { Assert.Null(e); } string json = Transaction.DecodeRawTransaction(tx); output.WriteLine(json); }
public void CalculateEcSignatureTest() { Privkey key = new Privkey("305e293b010d29bf3c888b617763a438fee9054c8cab66eb12ad078f819d9f27"); ByteData sighash = new ByteData("2a67f03e63a6a422125878b40b82da593be8d4efaafe88ee528af6e5a9955c6e"); SignParameter signData = key.CalculateEcSignature(sighash); Assert.Equal("0e68b55347fe37338beb3c28920267c5915a0c474d1dcafc65b087b9b3819cae6ae5e8fb12d669a63127abb4724070f8bd232a9efe3704e6544296a843a64f2c", signData.ToHexString()); signData.SetSignatureHashType(new SignatureHashType(CfdSighashType.Single, false)); Assert.True(signData.IsDerEncode()); SignParameter signData2 = key.CalculateEcSignature(sighash, false); Assert.Equal("0e68b55347fe37338beb3c28920267c5915a0c474d1dcafc65b087b9b3819cae6ae5e8fb12d669a63127abb4724070f8bd232a9efe3704e6544296a843a64f2c", signData2.ToHexString()); Assert.True(signData.IsDerEncode()); }
public void PubkeySignTest() { ExtPrivkey privkey = new ExtPrivkey("xprv9zt1onyw8BdEf7SQ6wUVH3bQQdGD9iy9QzXveQQRhX7i5iUN7jZgLbqFEe491LfjozztYa6bJAGZ65GmDCNcbjMdjZcgmdisPJwVjcfcDhV"); Transaction tx = new Transaction("020000000201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff01000000000000000000000000000000000000000000000000000000000000000300000000ffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c00000000"); OutPoint outpoint1 = new OutPoint("0000000000000000000000000000000000000000000000000000000000000001", 2); Privkey privkey1 = privkey.DerivePrivkey(11).GetPrivkey(); Pubkey pubkey1 = privkey1.GetPubkey(); SignatureHashType sighashType = new SignatureHashType(CfdSighashType.All, false); ByteData sighash = tx.GetSignatureHash(outpoint1, CfdHashType.P2wpkh, pubkey1, 50000, sighashType); SignParameter signature = privkey1.CalculateEcSignature(sighash); signature.SetDerEncode(sighashType); tx.AddPubkeySign(outpoint1, CfdHashType.P2wpkh, pubkey1, signature); // output.WriteLine("tx:\n" + tx.ToHexString()); Assert.Equal("0200000000010201000000000000000000000000000000000000000000000000000000000000000200000000ffffffff01000000000000000000000000000000000000000000000000000000000000000300000000ffffffff0310270000000000001600148b756cbd98f4f55e985f80437a619d47f0732a941027000000000000160014c0a3dd0b7c1b3281be91112e16ce931dbac2a97950c3000000000000160014ad3abd3c325e40e20d89aa054dd980b97494f16c02473044022034db802aad655cd9be589075fc8ef325b6ffb8c24e5b27eb87bde8ad38f5fd7a0220364c916c8e8fc0adf714d7148cd1c6dc6f3e67d55471e57233b1870c65ec2727012103782f0ea892d7000e5f0f82b6ff283382a76500137a542bb0a616530094a8f54c0000000000", tx.ToHexString()); bool isVerify = tx.VerifySignature(outpoint1, CfdHashType.P2wpkh, signature, pubkey1, sighashType, 50000); Assert.True(isVerify); }