/** * @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; }
/** * @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; }
/** * @param EcAdapterInterface $ecAdapter * @param PrivateKeyInterface $privateKey * @param Buffer $messageHash * @param string $algo */ public function __construct(EcAdapterInterface $ecAdapter, PrivateKeyInterface $privateKey, Buffer $messageHash, $algo = 'sha256') { $mdPk = new MdPrivateKey($ecAdapter->getMath(), $ecAdapter->getGenerator(), $privateKey->getSecretMultiplier()); $this->ecAdapter = $ecAdapter; $this->hmac = new HmacRandomNumberGenerator($ecAdapter->getMath(), $mdPk, $messageHash->getInt(), $algo); }
/** * 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 int|string $integer * @return \BitWasp\Bitcoin\Key\PrivateKey * @throws \Exception */ public function privateKeyAdd(PrivateKeyInterface $privateKey, $integer) { $privKey = $privateKey->getBuffer()->getBinary(); // mod by reference $ret = (bool) \secp256k1_ec_privkey_tweak_add($privKey, $this->getBinaryScalar($integer)); if ($ret === false) { throw new \Exception('Secp256k1 privkey tweak add: failed'); } return $this->getRelatedPrivateKey($privateKey, $privKey); }
/** * @param PrivateKeyInterface $privateKey * @return Buffer */ public function serialize(PrivateKeyInterface $privateKey) { $multiplier = $privateKey->getSecretMultiplier(); return Buffer::hex($this->ecAdapter->getMath()->decHex($multiplier), 32); }
/** * @param PrivateKeyInterface $oldPrivate * @param $newSecret * @return \BitWasp\Bitcoin\Key\PrivateKey */ private function getRelatedPrivateKey(PrivateKeyInterface $oldPrivate, $newSecret) { return new PrivateKey($this, $newSecret, $oldPrivate->isCompressed()); }