/** * @param CreateTransactionCommand $command * @throws \Exception */ public function handle(CreateTransactionCommand $command) { $walletId = $command->getWalletId(); $payToAddress = $command->getPayToAddress(); $description = $command->getDescription(); $amount = $command->getAmount(); // Get wallet object from repository $wallet = $this->walletRepository->walletOfId(new WalletId($walletId)); if (!$wallet) { throw new \Exception(sprintf("Wallet not found %s", $walletId)); } // Call BlockCypher API to generate new transaction $txSkeleton = $this->blockCypherTransactionService->create($wallet->getId()->getValue(), $wallet->getCoinSymbol(), $wallet->getToken(), $payToAddress, $amount); // Create new app Transaction $transaction = new Transaction($this->transactionRepository->nextIdentity(), $wallet->getId(), null, $payToAddress, $description, $amount, $this->clock->now()); // Get all addresses from all tx inputs. $allInputsAddresses = $txSkeleton->getInputsAddresses(); // Get private keys from repository $privateKeys = $this->getPrivateKeysFromRepository($allInputsAddresses, $walletId); // Check private keys $this->checkPrivateKeys($privateKeys, $txSkeleton->getTosign()); // Sign transaction $txSkeletonSigned = $this->blockCypherTransactionService->sign($txSkeleton, $privateKeys, $wallet->getCoinSymbol(), $wallet->getToken()); // Send transaction to the network $txSkeletonSent = $this->blockCypherTransactionService->send($txSkeletonSigned, $wallet->getCoinSymbol(), $wallet->getToken()); // Map real network tx with app transaction $transaction->assignNetworkTransactionHash($txSkeletonSent->getTx()->getHash()); // Store new local app transaction $this->transactionRepository->insert($transaction); }
/** * @param CreateWalletCommand $command * @throws \Exception */ public function handle(CreateWalletCommand $command) { // DEBUG //var_dump($command); $walletOwnerId = $command->getWalletOwnerId(); $walletName = $command->getName(); $walletCoinSymbol = $command->getCoinSymbol(); $walletToken = $command->getToken(); // Create app wallet $wallet = new Wallet($this->walletRepository->nextIdentity(), new UserId($walletOwnerId), $walletName, $walletCoinSymbol, $walletToken, $this->clock->now(), array()); $this->walletRepository->insert($wallet); // Create BlockCypher wallet $this->blockCypherWalletService->createWallet($wallet->getId()->getValue(), $wallet->getCoinSymbol(), $wallet->getToken()); }
/** * @param CreateAddressCommand $command * @throws \Exception */ public function handle(CreateAddressCommand $command) { // DEBUG //var_dump($command); $walletId = $command->getWalletId(); $addressTag = $command->getTag(); $addressCallbackUrl = $command->getCallbackUrl(); // DEBUG: create a sample wallet //$wallet = $this->walletRepository->loadFixtures(); $wallet = $this->walletRepository->walletOfId(new WalletId($walletId)); // DEBUG //var_dump($wallet); //die(); if ($wallet === null) { // TODO: create domain exception throw new \Exception(sprintf("Wallet not found %s", $walletId)); } // 1.- Call BlockCypher API to generate new address $walletGenerateAddressResponse = $this->blockCypherWalletService->generateAddress($wallet->getId()->getValue(), $wallet->getCoinSymbol(), $wallet->getToken()); // 2.- Create new app Address $address = new Address($this->addressRepository->nextIdentity(), new WalletId($walletId), $walletGenerateAddressResponse->getAddress(), $addressTag, $walletGenerateAddressResponse->getPrivate(), $walletGenerateAddressResponse->getPublic(), $walletGenerateAddressResponse->getWif(), $addressCallbackUrl, $this->clock->now()); $this->addressRepository->insert($address); }
/** * Create a sample wallet * @param FlywheelWalletRepository $repository * @param string $token * @return Wallet */ public function loadFixtures($repository, $token) { $wallet = new Wallet(new WalletId("5564B09652AFA054401239"), new UserId(new UserId($token), $token), 'alice', WalletCoinSymbol::BTC_TESTNET, $token, $this->clock->now()); $repository->insert($wallet); return $wallet; }