| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace BitWasp\Bitcoin\Transaction\Factory\ScriptInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use BitWasp\Bitcoin\Crypto\Hash; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use BitWasp\Bitcoin\Script\Opcodes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use BitWasp\Buffertools\BufferInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Generic classifier of hashlocks, to be used by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * the InputSigner. This is strictly internal to this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * At the moment, tolerates: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @package BitWasp\Bitcoin\Transaction\Factory\ScriptInfo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @internal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class Hashlock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private static $sizeMap = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         Opcodes::OP_RIPEMD160 => 20, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         Opcodes::OP_SHA1 => 20, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         Opcodes::OP_SHA256 => 32, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         Opcodes::OP_HASH256 => 32, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         Opcodes::OP_HASH160 => 20, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var BufferInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private $hash; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     private $opcode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * Hashlock constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param BufferInterface $hash | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param $opcode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function __construct(BufferInterface $hash, $opcode) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if (!array_key_exists($opcode, self::$sizeMap)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             throw new \RuntimeException("Unknown opcode"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $size = self::$sizeMap[$opcode]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         if ($hash->getSize() !== $size) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             throw new \RuntimeException("Unexpected size for hash"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->hash = $opcode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->opcode = $opcode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return BufferInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function getHash() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return $this->hash; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @return int | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     public function getOpcode() | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         return $this->opcode; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param BufferInterface $preimage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function checkPreimage(BufferInterface $preimage) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         switch ($this->opcode) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             case Opcodes::OP_SHA1: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 $hash = Hash::sha1($preimage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             case Opcodes::OP_SHA256: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 $hash = Hash::sha256($preimage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             case Opcodes::OP_RIPEMD160: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 $hash = Hash::ripemd160($preimage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             case Opcodes::OP_HASH160: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 $hash = Hash::sha256ripe160($preimage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             case Opcodes::OP_HASH256: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 $hash = Hash::sha256d($preimage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 throw new \RuntimeException("Missing hash function in Hashlock, but opcode was allowed by constructor.."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return $hash->equals($preimage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  |