/** * Fetch a public key and check the capability of the threemaId * * @param string $threemaId * @param \Closure $capabilityCheck * @return string Public key as binary * @throws Exception */ private final function fetchPublicKeyAndCheckCapability($threemaId, \Closure $capabilityCheck = null) { //fetch the public key $receiverPublicKey = $this->connection->fetchPublicKey($threemaId); if (null === $receiverPublicKey || !$receiverPublicKey->isSuccess()) { throw new Exception('Invalid threema id'); } if (null !== $capabilityCheck) { //check capability $capability = $this->connection->keyCapability($threemaId); if (null === $capability || false === $capabilityCheck->__invoke($capability)) { throw new Exception('threema id does not have the capability'); } } return $this->cryptTool->hex2bin($receiverPublicKey->getPublicKey()); }
protected function doRun() { $id = $this->getArgumentThreemaId(self::argThreemaId); $from = $this->getArgumentThreemaId(self::argFrom); $secret = $this->getArgument(self::argSecret); Common::required($id, $from, $secret); //define connection settings $settings = new ConnectionSettings($from, $secret); //create a connection $connector = new Connection($settings, $this->publicKeyStore); $result = $connector->fetchPublicKey($id); if ($result->isSuccess()) { Common::l(Common::convertPublicKey($result->getPublicKey())); } else { Common::e($result->getErrorMessage()); } }
<?php use Threema\MsgApi\Connection; use Threema\MsgApi\ConnectionSettings; //include_project require_once 'bootstrap.php'; //define your connection settings $settings = new ConnectionSettings('*YOUR_GATEWAY_THREEMA_ID', 'YOUR_GATEWAY_THREEMA_ID_SECRET'); //public key store file //best practice: create a publickeystore //Threema\MsgApi\PublicKeyStores\PhpFile::create('keystore.php'); //new keystore //$publicKeyStore = new Threema\MsgApi\PublicKeyStores\PhpFile('keystore.php'); //reuse keystore $publicKeyStore = null; //create a connection $connector = new Connection($settings, $publicKeyStore); $result = $connector->fetchPublicKey('ECHOECHO'); if ($result->isSuccess()) { echo 'public key ' . $result->getPublicKey() . "\n"; } else { echo 'error ' . $result->getErrorMessage() . "\n"; }