/** * Takes the $p2shScript and generates the scriptHash address. * * @param ScriptInterface $p2shScript * @return ScriptHashAddress */ public static function fromScript(ScriptInterface $p2shScript) { return new ScriptHashAddress($p2shScript->getScriptHash()); }
/** * Create a P2SH output script * * @param ScriptInterface $p2shScript * @return ScriptInterface */ public function payToScriptHash(ScriptInterface $p2shScript) { return ScriptFactory::create()->op('OP_HASH160')->push($p2shScript->getScriptHash())->op('OP_EQUAL')->getScript(); }
/** * @param ScriptInterface $script * @return ScriptHashAddress */ public static function fromScript(ScriptInterface $script) { $address = new ScriptHashAddress($script->getScriptHash()); return $address; }
/** * @param PrivateKeyInterface $key * @param ScriptInterface|null $redeemScript * @param ScriptInterface|null $witnessScript * @return bool */ public function sign(PrivateKeyInterface $key, ScriptInterface $redeemScript = null, ScriptInterface $witnessScript = null) { /** @var BufferInterface[] $return */ $type = null; $return = []; $solved = $this->doSignature($key, $this->txOut->getScript(), $type, $return, 0); if ($solved && $type === OutputClassifier::PAYTOSCRIPTHASH) { $redeemScriptBuffer = $return[0]; if (!$redeemScript instanceof ScriptInterface) { throw new \InvalidArgumentException('Must provide redeem script for P2SH'); } if (!$redeemScript->getScriptHash()->getBinary() === $redeemScriptBuffer->getBinary()) { throw new \InvalidArgumentException("Incorrect redeem script - hash doesn't match"); } $results = []; // ??? $solved = $solved && $this->doSignature($key, $redeemScript, $type, $results, 0) && $type !== OutputClassifier::PAYTOSCRIPTHASH; if ($solved) { $this->redeemScript = $redeemScript; } } if ($solved && $type === OutputClassifier::WITNESS_V0_KEYHASH) { $pubKeyHash = $return[0]; $witnessScript = ScriptFactory::sequence([Opcodes::OP_DUP, Opcodes::OP_HASH160, $pubKeyHash, Opcodes::OP_EQUALVERIFY, Opcodes::OP_CHECKSIG]); $subType = null; $subResults = []; $solved = $solved && $this->doSignature($key, $witnessScript, $subType, $subResults, 1); } else { if ($solved && $type === OutputClassifier::WITNESS_V0_SCRIPTHASH) { $scriptHash = $return[0]; if (!$witnessScript instanceof ScriptInterface) { throw new \InvalidArgumentException('Must provide witness script for witness v0 scripthash'); } if (!Hash::sha256($witnessScript->getBuffer())->getBinary() === $scriptHash->getBinary()) { throw new \InvalidArgumentException("Incorrect witness script - hash doesn't match"); } $subType = null; $subResults = []; $solved = $solved && $this->doSignature($key, $witnessScript, $subType, $subResults, 1) && $subType !== OutputClassifier::PAYTOSCRIPTHASH && $subType !== OutputClassifier::WITNESS_V0_SCRIPTHASH && $subType !== OutputClassifier::WITNESS_V0_KEYHASH; if ($solved) { $this->witnessScript = $witnessScript; } } } return $solved; }