/** * create a signed transaction sending all the found outputs to the given address * * @param $destinationAddress * @return array * @throws \Exception */ protected function createTransaction($destinationAddress) { if ($this->debug) { echo "\nCreating transaction to address {$destinationAddress}"; } // create raw transaction $inputs = []; foreach ($this->sweepData['utxos'] as $address => $data) { $inputs = array_merge($inputs, array_map(function ($utxo) use($address, $data) { return ['txid' => $utxo['hash'], 'vout' => $utxo['index'], 'scriptPubKey' => $utxo['script_hex'], 'value' => $utxo['value'], 'address' => $address, 'path' => $data['path'], 'redeemScript' => $data['redeem']]; }, $data['utxos'])); } $outputs = []; $fee = Wallet::estimateFee($this->sweepData['count'], 1); $outputs[$destinationAddress] = $this->sweepData['balance'] - $fee; //create the raw transaction $rawTransaction = RawTransaction::create($inputs, $outputs); if (!$rawTransaction) { throw new \Exception("Failed to create raw transaction"); } if ($this->debug) { echo "\nSigning transaction"; } //sign the raw transaction $transaction = $this->signTransaction($rawTransaction, $inputs); if (!$transaction['sign_count']) { throw new \Exception("Failed to sign transaction"); } return $transaction; }
/** * initialize a previously created wallet * * Either takes one argument: * @param array $options * * Or takes two arguments (old, deprecated syntax): * (@nonPHP-doc) @param string $identifier the wallet identifier to be initialized * (@nonPHP-doc) @param string $password the password to decrypt the mnemonic with * * @return WalletInterface * @throws \Exception */ public function initWallet($options) { if (!is_array($options)) { $args = func_get_args(); $options = ["identifier" => $args[0], "password" => $args[1]]; } $identifier = $options['identifier']; $readonly = isset($options['readonly']) ? $options['readonly'] : (isset($options['readOnly']) ? $options['readOnly'] : (isset($options['read-only']) ? $options['read-only'] : false)); // get the wallet data from the server $data = $this->getWallet($identifier); if (!$data) { throw new \Exception("Failed to get wallet"); } // explode the wallet data $primaryMnemonic = isset($options['primary_mnemonic']) ? $options['primary_mnemonic'] : $data['primary_mnemonic']; $checksum = $data['checksum']; $backupPublicKey = $data['backup_public_key']; $primaryPublicKeys = $data['primary_public_keys']; $blocktrailPublicKeys = $data['blocktrail_public_keys']; $keyIndex = isset($options['key_index']) ? $options['key_index'] : $data['key_index']; $wallet = new Wallet($this, $identifier, $primaryMnemonic, $primaryPublicKeys, $backupPublicKey, $blocktrailPublicKeys, $keyIndex, $this->network, $this->testnet, $checksum); if (!$readonly) { $wallet->unlock($options); } return $wallet; }
public function testEstimateFee() { $this->assertEquals(30000, Wallet::estimateFee(1, 66)); $this->assertEquals(40000, Wallet::estimateFee(2, 71)); }