项目作者: ltonetwork

项目描述 :
LTO Network API client for PHP
高级语言: PHP
项目地址: git://github.com/ltonetwork/lto-api.php.git
创建时间: 2018-03-27T10:35:34Z
项目社区:https://github.com/ltonetwork/lto-api.php

开源协议:MIT License

下载


LTO github readme

LTO Network client for PHP

PHP
Scrutinizer Code Quality
Code Coverage
Packagist Stable Version
Packagist License

Signing and addresses work both for the (private) event chain as for the public chain.

Installation

  1. composer require lto/api

Accounts

Creation

Create an account from seed

  1. $seedText = "manage manual recall harvest series desert melt police rose hollow moral pledge kitten position add";
  2. $factory = new LTO\AccountFactory('T'); // 'T' for testnet, 'L' for mainnet
  3. $account = $factory->seed($seedText);

Create an account from sign key

  1. $secretKey = 'wJ4WH8dD88fSkNdFQRjaAhjFUZzZhV5yiDLDwNUnp6bYwRXrvWV8MJhQ9HL9uqMDG1n7XpTGZx7PafqaayQV8Rp';
  2. $factory = new LTO\AccountFactory('T'); // 'T' for testnet, 'L' for mainnet
  3. $account = $factory->create($secretKey);

Create an account from full info

  1. $accountInfo = [
  2. 'address' => '3PLSsSDUn3kZdGe8qWEDak9y8oAjLVecXV1',
  3. 'sign' => [
  4. 'secretkey' => 'wJ4WH8dD88fSkNdFQRjaAhjFUZzZhV5yiDLDwNUnp6bYwRXrvWV8MJhQ9HL9uqMDG1n7XpTGZx7PafqaayQV8Rp',
  5. 'publickey' => 'FkU1XyfrCftc4pQKXCrrDyRLSnifX1SMvmx1CYiiyB3Y'
  6. ],
  7. 'encrypt' => [
  8. 'secretkey' => 'BnjFJJarge15FiqcxrB7Mzt68nseBXXR4LQ54qFBsWJN',
  9. 'publickey' => 'BVv1ZuE3gKFa6krwWJQwEmrLYUESuUabNCXgYTmCoBt6'
  10. ]
  11. ];
  12. $factory = new LTO\AccountFactory('T'); // 'T' for testnet, 'L' for mainnet
  13. $account = $factory->create($accountInfo);

Properties that are specified will be verified. Properties that are omitted will be generated where possible.

Signing (ED25519)

Sign a message

  1. $signature = $account->sign('hello world'); // Base58 encoded signature

Verify a signature

  1. if (!$account->verify($signature, 'hello world')) {
  2. throw new RuntimeException('invalid signature');
  3. }

Encryption (X25519)

Encrypt a message for another account

  1. $message = 'hello world';
  2. $recipientPublicKey = "HBqhfdFASRQ5eBBpu2y6c6KKi1az6bMx8v1JxX4iW1Q8"; // base58 encoded X25519 public key
  3. $recipient = $factory->createPublic(null, $recipientPublicKey);
  4. $cyphertext = $account->encryptFor($recipient, $message); // Raw binary, not encoded

You can use $account->encryptFor($account, $message); to encrypt a message for yourself.

Decrypt a message received from another account

  1. $senderPublicKey = "HBqhfdFASRQ5eBBpu2y6c6KKi1az6bMx8v1JxX4iW1Q8"; // base58 encoded X25519 public key
  2. $sender = $factory->createPublic(null, $senderPublicKey);
  3. $message = $account->decryptFrom($sender, $cyphertext);

You can use $account->decryptFrom($account, $message); to decrypt a message from yourself.

Public layer

  1. use LTO\Transaction\Transfer;
  2. use LTO\PublicNode;
  3. $node = new PublicNode('https://nodes.lto.network');
  4. $amount = 1000.0; // Amount of LTO to transfer
  5. $recipient = "3Jo1JCrBvnWCg37VDxMXAjYhsS9rRDLBSze";
  6. $transferTx = (new Transfer($amount, $recipient))
  7. ->signWith($account)
  8. ->broadcastTo($node);

Private layer

Event chain

Create a new event chain

  1. $chain = $account->createEventChain(); // Creates an empty event chain with a valid id and last hash

Note: You need to add an identity as first event on the chain. This is not done automatically.

Create and sign an event and add it to an existing event chain

  1. $body = [
  2. '$schema' => "http://specs.example.com/message#",
  3. 'content' => "Hello world!"
  4. ];
  5. $chainId = "JEKNVnkbo3jqSHT8tfiAKK4tQTFK7jbx8t18wEEnygya";
  6. $chainLastHash = "3yMApqCuCjXDWPrbjfR5mjCPTHqFG8Pux1TxQrEM35jj";
  7. $chain = new LTO\EventChain($chainId, $chainLastHash);
  8. $chain->add(new Event($body))->signWith($account);

You need the chain id and the hash of the last event to use an existing chain.

HTTP Authentication

Signing HTTP Messages is described IETF draft draft-cavage-http-signatures-10.

The HTTP Authentication library can be used to sign and verify
PSR-7 requests.

This library can be used in conjunction with the HTTP authentication library. The keyId should be the base58 encoded
public key.

For POST and PUT requests, it’s recommended to create an HTTP Digest
(RFC 3230). This is a hash of the body, which manages to indirectly include the
body in the signature. See the HTTP Digest library.

Creating the HTTP Authentication service

  1. use Jasny\HttpSignature\HttpSignature;
  2. $secretKey = 'wJ4WH8dD88fSkNdFQRjaAhjFUZzZhV5yiDLDwNUnp6bYwRXrvWV8MJhQ9HL9uqMDG1n7XpTGZx7PafqaayQV8Rp';
  3. $factory = new LTO\AccountFactory('T'); // 'T' for testnet, 'L' for mainnet
  4. $ourAccount = $factory->create($secretKey);
  5. $service = new HttpSignature(
  6. ['ed25519', 'ed25519-sha256'],
  7. new SignCallback($ourAccount),
  8. new VerifyCallback($accountFactory)
  9. );

Server middleware

Create server middleware to verify incoming requests.

The LTO\Account\ServerMiddleware can be used to set the account attribute for a server request that contains a
signature_key_id attribute.

  1. use Jasny\HttpDigest\HttpDigest;
  2. use Jasny\HttpDigest\ServerMiddleware as DigestMiddleware;
  3. use Jasny\HttpDigest\Negitiation\DigestNegotiator;
  4. use Jasny\HttpSignature\HttpSignature;
  5. use Jasny\HttpSignature\ServerMiddleware as SignatureMiddleware;
  6. use LTO\Account\ServerMiddleware as AccountMiddleware;
  7. use Relay\RelayBuilder;
  8. $factory = new LTO\AccountFactory('T'); // 'T' for testnet, 'L' for mainnet
  9. $ourAccount = $factory->create($secretKey);
  10. $digestService = HttpDigest(new DigestNegotiator(), ["SHA-256"]);
  11. $signatureService = new HttpSignature(
  12. ['ed25519', 'ed25519-sha256'],
  13. function() { throw new \LogicException('sign not supported'); },
  14. new VerifyCallback($accountFactory)
  15. );
  16. $relayBuilder = new RelayBuilder($resolver);
  17. $relay = $relayBuilder->newInstance([
  18. (new DigestMiddleware($digestService))->asDoublePass(),
  19. (new SignatureMiddleware($signatureService))->asDoublePass(),
  20. (new AccountMiddleware($factory))->asDoublePass(),
  21. ]);

The server middleware implements the PSR-15 MiddlewareInterface for single pass support and returns a callback for
double pass with the asDoublePass() method.

Client middleware

Create client middleware to sign outgoing requests.

  1. use GuzzleHttp\HandlerStack;
  2. use GuzzleHttp\Client;
  3. use Jasny\HttpDigest\HttpDigest;
  4. use Jasny\HttpDigest\ClientMiddleware as DigestMiddleware;
  5. use Jasny\HttpDigest\Negitiation\DigestNegotiator;
  6. use Jasny\HttpSignature\HttpSignature;
  7. use Jasny\HttpSignature\ClientMiddleware as SignatureMiddleware;
  8. $secretKey = 'wJ4WH8dD88fSkNdFQRjaAhjFUZzZhV5yiDLDwNUnp6bYwRXrvWV8MJhQ9HL9uqMDG1n7XpTGZx7PafqaayQV8Rp';
  9. $factory = new LTO\AccountFactory('T'); // 'T' for testnet, 'L' for mainnet
  10. $ourAccount = $factory->create($secretKey);
  11. $digestService = HttpDigest(new DigestNegotiator(), ["SHA-256"]);
  12. $signatureService = new HttpSignature(
  13. ['ed25519', 'ed25519-sha256'],
  14. new SignCallback($ourAccount),
  15. function() { throw new \LogicException('verify not supported'); }
  16. );
  17. $signatureMiddleware = new SignatureMiddleware(
  18. $service->withAlgorithm('ed25519-sha256'),
  19. $ourAccount->getPublicKey()
  20. );
  21. $stack = new HandlerStack();
  22. $stack->push((new DigestMiddleware($digestService))->forGuzzle());
  23. $stack->push($signatureMiddleware->forGuzzle());
  24. $client = new Client(['handler' => $stack]);

Commandline scripts

Generate account from seed

Get the seed from stdin to generate account info.

  1. $ vendor/bin/lto-seed <<< "manage manual recall harvest series desert melt police rose hollow moral pledge kitten position add"
  2. address: 3JmCa4jLVv7Yn2XkCnBUGsa7WNFVEMxAfWe
  3. sign:
  4. secretkey: 4zsR9xoFpxfnNwLcY4hdRUarwf5xWtLj6FpKGDFBgscPxecPj2qgRNx4kJsFCpe9YDxBRNoeBWTh2SDAdwTySomS
  5. publickey: GjSacB6a5DFNEHjDSmn724QsrRStKYzkahPH67wyrhAY
  6. encrypt:
  7. secretkey: 4q7HKMbwbLcG58iFV3pz4vkRnPTwbrY9Q5JrwnwLEZCC
  8. publickey: 6fDod1xcVj4Zezwyy3tdPGHkuDyMq8bDHQouyp5BjXsX

By default a mainnet (L) address is generated. Pass T as argument to create a testnet address.

  1. $ vendor/bin/lto-seed T <<< "manage manual recall harvest series desert melt police rose hollow moral pledge kitten position add"

Account info

Show account info from the secret key. The secret key should be base58 encoded.

  1. $ vendor/bin/lto-account 4zsR9xoFpxfnNwLcY4hdRUarwf5xWtLj6FpKGDFBgscPxecPj2qgRNx4kJsFCpe9YDxBRNoeBWTh2SDAdwTySomS
  2. address: 3JmCa4jLVv7Yn2XkCnBUGsa7WNFVEMxAfWe
  3. sign:
  4. secretkey: 4zsR9xoFpxfnNwLcY4hdRUarwf5xWtLj6FpKGDFBgscPxecPj2qgRNx4kJsFCpe9YDxBRNoeBWTh2SDAdwTySomS
  5. publickey: GjSacB6a5DFNEHjDSmn724QsrRStKYzkahPH67wyrhAY
  6. encrypt:
  7. secretkey: 4q7HKMbwbLcG58iFV3pz4vkRnPTwbrY9Q5JrwnwLEZCC
  8. publickey: 6fDod1xcVj4Zezwyy3tdPGHkuDyMq8bDHQouyp5BjXsX

By default a mainnet (L) address is generated. Pass T as third argument to create a testnet address.

  1. $ vendor/bin/lto-seed 4zsR9xoFpxfnNwLcY4hdRUarwf5xWtLj6FpKGDFBgscPxecPj2qgRNx4kJsFCpe9YDxBRNoeBWTh2SDAdwTySomS T

Sign message

Sign a message (from stdin). The secret key should be base58 encoded. Outputs the signature and hash (base58
encoded).

  1. $ cat message.json | vendor/bin/lto-sign 4zsR9xoFpxfnNwLcY4hdRUarwf5xWtLj6FpKGDFBgscPxecPj2qgRNx4kJsFCpe9YDxBRNoeBWTh2SDAdwTySomS
  2. signature: 5i9gBaHwg9UFPuwU63LBdBR29yZdRDstWM9z7oo8GzevWhBdAAWwCSRUQbPLaCT3nFgjbQuuWxVQckzCd3CoFig4
  3. hash: 42TEXg1vFAbcJ65y7qdYG9iCPvYfy3NDdVLd75akX2P5