包全/eagle-sdk

为成员提供使用baoquan.com服务的SDK

1.1.1 2019-11-26 06:54 UTC

README

欢迎使用Baoquan.com API SDK。

创建Baoquan客户端

$client = new BaoquanClient();
$client->setHost('https://baoquan.com');
$client->setAccessKey('fsBswNzfECKZH9aWyh47fc'); // replace it with your access key
$client->setPemPath('path/to/rsa_private.pem');

创建证明

创建无需签名的证明

try {
  $response = $this->client->createAttestation([
    'template_id'=>'5Yhus2mVSMnQRXobRJCYgt',
    'identities'=>[
       'ID'=>'42012319800127691X',
       'MO'=>'15857112383',
    ],
    'factoids'=>[
       [
           'type'=>'product',
           'data'=>[
               'name'=>'浙金网',
               'description'=>'p2g理财平台'
           ]
       ]
    ],
    'completed'=>false
  ]);
  echo $response['data']['no'];
} catch (ServerException $e) {
  echo $e->getMessage();
}

创建带签名的证明

try {
  $response = $this->client->createAttestation([
       'template_id'=>'2hSWTZ4oqVEJKAmK2RiyT4',
       'identities'=>[
           'ID'=>'42012319800127691X',
           'MO'=>'15857112383',
       ],
       'factoids'=>[
           [
               'type'=>'user',
               'data'=>[
                   'name'=>'张三',
                   'phone_number'=>'13234568732',
                   'registered_at'=>'1466674609',
                   'username'=>'tom'
               ]
           ]
       ],
       'signs'=>[
           0=>[
               0=>[
                   'F98F99A554E944B6996882E8A68C60B2'=>['甲方(签章)'],
                   '0A68783469E04CAC95ADEAE995A92E65'=>['乙方(签章)'],
               ]
           ]
       ],
       'completed'=>true
   ], [
       0=>[
           [
               'resource'=>fopen(__DIR__.'/resources/contract.pdf', 'r'),
               'resource_name'=>'contract.pdf'
           ]
       ]
   ]);
  echo $response['data']['no'];
} catch (ServerException $e) {
  echo $e->getMessage();
}

创建带哈希的证明

try {
    $response = $client->createAttestationHash([
            // 设置保全唯一码
            'unique_id'=>'5bf54bc4-ec69-4a5d-b6e4-a3f670f795f4',
            // 设置模板id
            'template_id'=>'v137Mok8WPreDoorWcuZaH',
            // 设置保全所有者的身份标识
            'identities'=>[
                'ID'=>'42012319800127691X',
                'MO'=>'15857112383',
            ],
            // 陈述对象列表
            'factoids'=>[
                // user陈述
                [
                    'unique_id'=>'c83d838e-3844-4689-addf-ca0f01171e7c',
                    'type'=>'file',

                    'data'=>[
                        'file_name'=>'123.txt'
                    ]
                ]
            ],
            // 设置陈述是否上传完成,如果设置成true,则后续不能继续追加陈述
            'completed'=>true
            ]
            ,"064eb22a4e3bff9f478eb94b87b5b2cb65063de6a3585d44903e814c0aaf1356"
    );
    echo $response['data']['no'];
} catch (ServerException $e) {
    echo $e->getMessage();
}

通过URL创建证明

try {
    $response = $client->createAttestationURL([
            // 设置保全唯一码
            'unique_id'=>'5bf54bc4-ec69-4a5d-b6e4-a3f670f795f5',
            // 设置模板id
            'template_id'=>'uiqAvzh5uLKYBd4Jp9Upr1',
            // 设置保全所有者的身份标识
            'identities'=>[
                'ID'=>'42012319800127691X',
                'MO'=>'15857112383',
            ],
            // 陈述对象列表
            'factoids'=>[
                // user陈述
                [
                    'unique_id'=>'c83d838e-3844-4689-addf-ca0f01171e7c',
                    'type'=>'content',

                    'data'=>[
                        'url'=>'$url'
                    ]
                ]
            ],
            // 设置陈述是否上传完成,如果设置成true,则后续不能继续追加陈述
            'completed'=>true
            ]
            ,"http://www.baidu.com/"
    );
    echo $response['data']['no'];
} catch (ServerException $e) {
    echo $e->getMessage();
}

添加事实信息

try {
  $response = $this->client->addFactoids([
     'ano'=>'7F189BBB5FA1451EA8601D0693E36FE7',
     'factoids'=>[
         [
             'type'=>'user',
             'data'=>[
                 'name'=>'张三',
                 'phone_number'=>'13234568732',
                 'registered_at'=>'1466674609',
                 'username'=>'tom'
             ]
         ]
     ],
     'completed'=>true
    ]);
  echo $response['data']['success'];
} catch (ServerException $e) {
  echo $e->getMessage();
}

获取证明数据

try {
  $response = $client->getAttestation('DB0C8DB14E3C44C7B9FBBE30EB179241', ['factoids']);
  var_dump($response['data']);
} catch (ServerException $e) {
  echo $e->getMessage();
}

getAttestation的第二个参数值可以是null、空数组、包含字段"identities"、"factoids"、"attachments"的数组。如果为null,则响应包含所有字段值;如果为空数组,则响应包含所有字段值,除了"identities"、"factoids"、"attachments";如果为包含一个或多个"identities"、"factoids"、"attachments"值的数组,则响应包含响应字段值。如果你只想获取区块链哈希,你应该将第二个参数设置为null,因为服务器需要更多时间来连接数据库、在获取"identities"、"factoids"、"attachments"时解密数据。

下载证明

try {
  $response = $client->downloadAttestation('DB0C8DB14E3C44C7B9FBBE30EB179241');
  $file = fopen($response['file_name'], 'w');
  // $response['file'] is a \Psr\Http\Message\StreamInterface object
  fwrite($file, $response['file']->getContents());
  fclose($file);
} catch (ServerException $e) {
  echo $e->getMessage();
}

申请CA

申请个人CA

try {
  $response = $this->client->applyCa([
     'type'=>'PERSONAL',
     'link_name'=>'张三',
     'link_id_card'='432982198405237845',
     'link_phone'=>'13578674532',
     'link_email'=>'13578674532@qq.com',
    ]);
  echo $response['data']['no'];
} catch (ServerException $e) {
  echo $e->getMessage();
}

申请企业CA

try {
  $response = $this->client->applyCa([
    'type'=>'ENTERPRISE',
    'name'=>'浙金网',
    'ic_code'=>'91330105311263043J',
    'org_code'=>'311263043',
    'tax_code'=>'330105311263043',
    'link_name'=>'张三',
    'link_id_card'='432982198405237845',
    'link_phone'=>'13578674532',
    'link_email'=>'13578674532@qq.com',
    ], [
     'resource'=>fopen(__DIR__.'/resources/seal.png', 'r'),
     'resource_name'=>'seal.png'
    ]);
  echo $response['data']['no'];
} catch (ServerException $e) {
  echo $e->getMessage();
}

你可以查看单元测试以获取更多示例。