public static byte[] CreatePrivateKeyScript(Transaction tx, int inputIndex, byte hashType, ECPrivateKeyParameters privateKey, ECPublicKeyParameters publicKey) { //TODO var scriptEngine = new ScriptEngine(); var publicAddress = CreatePublicAddress(publicKey); var publicKeyScript = CreatePublicKeyScript(publicAddress); var txSignature = scriptEngine.TxSignature(publicKeyScript.ToImmutableArray(), tx, inputIndex, hashType); var txSignatureHash = Crypto.DoubleSHA256(txSignature); //Debug.WriteLine("Signing Tx: {0}".Format2(txSignature.ToHexDataString())); //Debug.WriteLine("Signing Tx Hash: {0}".Format2(txSignatureHash.ToHexDataString())); var signer = new ECDsaSigner(); signer.Init(forSigning: true, parameters: privateKey); var signature = signer.GenerateSignature(txSignatureHash); var r = signature[0]; var s = signature[1]; byte[] sigEncoded; using (var stream = new MemoryStream()) { using (var asn1Stream = new Asn1OutputStream(stream)) { asn1Stream.WriteObject(new DerSequence(new DerInteger(r), new DerInteger(s))); } sigEncoded = stream.ToArray().Concat(hashType); } //Debug.WriteLine("Sig R: {0}".Format2(r.ToHexNumberStringUnsigned())); //Debug.WriteLine("Sig S: {0}".Format2(s.ToHexNumberStringUnsigned())); //Debug.WriteLine("Sig Encoded: {0}".Format2(sigEncoded.ToHexDataString())); var privateKeyScript = new ScriptBuilder(); privateKeyScript.WritePushData(sigEncoded); privateKeyScript.WritePushData(publicAddress); //Debug.WriteLine("Private Script: {0}".Format2(privateKeyScript.GetScript().ToHexDataString())); return privateKeyScript.GetScript(); }
public static byte[] CreatePublicKeyScript(byte[] publicAddress) { var publicAddressHash = Crypto.SingleRIPEMD160(Crypto.SingleSHA256(publicAddress)); var publicKeyScript = new ScriptBuilder(); publicKeyScript.WriteOp(ScriptOp.OP_DUP); publicKeyScript.WriteOp(ScriptOp.OP_HASH160); publicKeyScript.WritePushData(publicAddressHash); publicKeyScript.WriteOp(ScriptOp.OP_EQUALVERIFY); publicKeyScript.WriteOp(ScriptOp.OP_CHECKSIG); //Debug.WriteLine("Public Script: {0}".Format2(publicKeyScript.GetScript().ToHexDataString())); return publicKeyScript.GetScript(); }