/** * @param EcAdapterInterface $ecAdapter * @param KeyInterface $masterKey */ public function __construct(EcAdapterInterface $ecAdapter, KeyInterface $masterKey) { if ($masterKey->isCompressed()) { throw new \RuntimeException('Electrum keys are not compressed'); } $this->ecAdapter = $ecAdapter; if ($masterKey instanceof PrivateKeyInterface) { $this->masterKey = $masterKey; $masterKey = $this->masterKey->getPublicKey(); } $this->publicKey = $masterKey; }
/** * Append private key to the list. * * @param PrivateKeyInterface $privateKey * @return $this */ public function addPrivateKey(PrivateKeyInterface $privateKey) { $pubKeyHex = $privateKey->getPublicKey()->getHex(); $this->privateKeys[$pubKeyHex] = $privateKey; }
/** * @param PrivateKeyInterface $privateKey * @param ScriptInterface $outputScript * @param $inputToSign * @param int $sigHashType * @param RedeemScript $redeemScript * @return $this * @throws \Exception */ public function signInputWithKey(PrivateKeyInterface $privateKey, ScriptInterface $outputScript, $inputToSign, RedeemScript $redeemScript = null, $sigHashType = SignatureHashInterface::SIGHASH_ALL) { // If the input state hasn't been set up, do so now. try { $inputState = $this->getInputState($inputToSign); } catch (BuilderNoInputState $e) { $inputState = $this->createInputState($inputToSign, $outputScript, $redeemScript); } // If it's PayToPubkey / PayToPubkeyHash, TransactionBuilderInputState needs to know the public key. if (in_array($inputState->getPrevOutType(), [OutputClassifier::PAYTOPUBKEYHASH])) { $inputState->setPublicKeys([$privateKey->getPublicKey()]); } // loop over the publicKeys to find the key to sign with foreach ($inputState->getPublicKeys() as $idx => $publicKey) { if ($privateKey->getPublicKey()->getBinary() === $publicKey->getBinary()) { $inputState->setSignature($idx, $this->sign($privateKey, $this->transaction->getSignatureHash()->calculate($redeemScript ?: $outputScript, $inputToSign, $sigHashType), $sigHashType)); } } return $this; }
/** * @param PrivateKeyInterface $privateKey * @param string $coinSymbol * @return mixed * @throws \Exception */ public static function getAddressFromPrivateKey($privateKey, $coinSymbol) { CoinSymbolValidator::validate($coinSymbol, 'coinSymbol'); $network = CoinSymbolNetworkMapping::getNetwork($coinSymbol); $publicKey = $privateKey->getPublicKey(); $address = $publicKey->getAddress()->getAddress($network); return $address; }