示例#1
0
        public void TestP2SH_P2PKH()
        {
            //Create p2pkh
            CryptoRSA rsa = new CryptoRSA(key, true);

            Interpreter.Initialize();

            //Create address
            byte[] pubkeyhash;

            using (var rid = RIPEMD160.Create())
                using (var sha = SHA256.Create())
                    pubkeyhash = rid.ComputeHash(sha.ComputeHash(key.publicKey));

            LockingScript   ls = new LockingScript(BASE58.Encode(pubkeyhash));
            UnlockingScript us = new UnlockingScript(rsa.Sign(transaction.Hash()), key.publicKey);

            //Create p2sh and insert p2pkh in it
            LockingScript   ls1 = new LockingScript(ls);
            UnlockingScript us1 = new UnlockingScript(ls, us);

            us1.InsertScript(ls1);

            Assert.AreEqual(EXECUTION_RESULT.SUCCESS, us1.Run(transaction), "Execution of P2SH script failed");
        }
示例#2
0
        /// <summary>
        /// This will initialize the script as an P2PKH-locking script (Pay To Private Key Hash)
        /// </summary>
        /// <param name="address">The hashed and BASE58 encoded address</param>
        public LockingScript(string address) : base(SCRIPTTYPE.LOCK_P2PKH)
        {
            if (address.Length < 20)
            {
                throw new ArgumentException("Address is invalid: too short");
            }
            if (address.Length > 255)
            {
                throw new ArgumentException("Address is invalid: too big");
            }

            byte[] addressHash = BASE58.Decode(address);
            if (addressHash.Length != 20)
            {
                throw new ArgumentException("Invalid address. Address byte[] size must be 20 (HASH160)");
            }

            this.Add(OPCODE.DUP);       //OPCODE = Duplicate the original public key, first instruction
            this.Add(OPCODE.HASH160);   //OPCODE = Hash public key
            this.Add(OPCODE.PUBKEY_HASH);
            this.AddRange(addressHash); //Hashed publicKey
            this.Add(OPCODE.EQ_VERIFY); //OPCODE = Check if equal: address and hashed public key
            this.Add(OPCODE.CHECKSIG);  //Validate signature with the provided public key
        }