基于Crypto++密码库的ECIES和ECDSA算法的联合使用

Auteur:GX
CSDN:GuoXuan_CHN

#include <iostream>

#include "eccrypto.h"
#include "osrng.h"
#include "oids.h"
#include "hex.h"
#include "filters.h"

#ifndef ECC_ENCRYPTION_ALGORITHM_H_
#define ECC_ENCRYPTION_ALGORITHM_H_

#include<string>

class EccEncryption
{
public:
    /// This method is used to generate keys for ECC encryption algorithm
    ///
    ///  \param[in]  uiKeySize, length of key
    /// \param[out]  sPrivateKey, private key
    /// \param[out]  sPublicKey, public key
    void GenerateEccKeys(unsigned int uiKeySize, std::string& sPrivateKey, std::string& sPublicKey);

    /// This method is used to encrypt the input message using public key
    ///
    ///  \param[in]  sPublicKey, public key generated by the first method
    /// \param[out]  sMsgToEncrypt, message to encryppt
    /// \return  the message encrypted using the input public key
    std::string Encrypt(const std::string& sPublicKey, const std::string& sMsgToEncrypt);

    /// This method is used to decrypt the input message using private key
    ///
    /// \param[in] sPrivateKey, private key used to decrypt the cipher text
    /// \param[in] sMsgToDecrypt, cipher text used to decrypt to get the plain text
    /// \return decrypted plain text
    std::string Decrypt(const std::string& sPrivateKey, const std::string& sMsgToDecrytp);
};
#endif

void EccEncryption::GenerateEccKeys(unsigned int uiKeySize, std::string& sPrivateKey, std::string& sPublicKey)
{
    using namespace CryptoPP;
    // Random pool, the second parameter is the length of key
    // 随机数池,第二个参数是生成密钥的长
    AutoSeededRandomPool rnd(false, 256);

    ECIES<ECP>::PrivateKey  privateKey;
    ECIES<ECP>::PublicKey   publicKey;

    // Generate private key
    privateKey.Initialize(rnd, ASN1::secp256r1());
    // Generate public key using private key
    privateKey.MakePublicKey(publicKey);

    ECIES<ECP>::Encryptor encryptor(publicKey);
    HexEncoder pubEncoder(new StringSink(sPublicKey));
    publicKey.DEREncode(pubEncoder);
    pubEncoder.MessageEnd();

    ECIES<ECP>::Decryptor decryptor(privateKey);
    HexEncoder prvEncoder(new StringSink(sPrivateKey));
    privateKey.DEREncode(prvEncoder);
    prvEncoder.MessageEnd();
}

std::string EccEncryption::Encrypt(const std::string& sPublicKey, const std::string& sMsgToEncrypt)
{
    using namespace CryptoPP;
    // If to save the keys into a file, FileSource should be replace StringSource
    StringSource pubString(sPublicKey, true, new HexDecoder);
    ECIES<ECP>::Encryptor encryptor(pubString);

    // Calculate the length of cipher text
    size_t uiCipherTextSize = encryptor.CiphertextLength(sMsgToEncrypt.size());
    std::string sCipherText;
    sCipherText.resize(uiCipherTextSize);
    RandomPool rnd;
    encryptor.Encrypt(rnd, (byte*)(sMsgToEncrypt.c_str()), sMsgToEncrypt.size(), (byte*)(sCipherText.data()));
    return sCipherText;
}

std::string EccEncryption::Decrypt(const std::string& sPrivateKey, const std::string& sMsgToDecrytp)
{
    using namespace CryptoPP;
    StringSource privString(sPrivateKey, true, new HexDecoder);
    ECIES<ECP>::Decryptor decryptor(privString);

    auto sPlainTextLen = decryptor.MaxPlaintextLength(sMsgToDecrytp.size());
    std::string sDecryText;
    sDecryText.resize(sPlainTextLen);
    RandomPool rnd;
    decryptor.Decrypt(rnd, (byte*)sMsgToDecrytp.c_str(), sMsgToDecrytp.size(), (byte*)sDecryText.data());
    return sDecryText;
}

int main()
{
    std::string sStrToTest = std::string("Hello world. This is an example of Ecc encryption algorithm of Crypto++ open source library.");
    EccEncryption ecc;
    std::string sPrivateKey, sPublicKey;
    ecc.GenerateEccKeys(1024, sPrivateKey, sPublicKey);

    std::cout << "Generated private key is : "<< std::endl;
    std::cout << sPrivateKey << std::endl;
    std::cout << "***********************************************************" << std::endl;

    std::cout << "Generated public key is : "<< std::endl;
    std::cout << sPublicKey << std::endl;
    std::cout << "***********************************************************" << std::endl;

    std::cout << "The message to be encrypted is : " << std::endl;
    std::cout << sStrToTest << std::endl;
    std::cout << "***********************************************************" << std::endl;

    std::string sEncryptResult = ecc.Encrypt(sPublicKey, sStrToTest);
    std::cout << "The result of encrypt is : " << std::endl;
    std::cout << sEncryptResult << std::endl;
    std::cout << "***********************************************************" << std::endl;

    std::string sDecryptResult = ecc.Decrypt(sPrivateKey, sEncryptResult);
    std::cout << "The result of decrypt is : " << std::endl;
    std::cout << sDecryptResult << std::endl;
    std::cout << "***********************************************************" << std::endl;

    return 0;
}

ECIES-ECSDSA联合使用Demo

/*
auteur:GX
CSDN:GuoXuan_CHN
*/
#include <fstream>
#include <string>
#include <iostream>

#include "eccrypto.h"
#include "osrng.h"
#include "oids.h"
#include "hex.h"
#include "filters.h"
#include "des.h"

using namespace std;

 CryptoPP::ECIES<CryptoPP::ECP>::PrivateKey  ePrivateKey;
 CryptoPP::ECIES<CryptoPP::ECP>::PublicKey   ePublicKey;
string sPrivateKey, sPublicKey;


void GenerateEccKeys()
{
    using namespace CryptoPP;

    // Random pool, the second parameter is the length of key
    // 随机数池,第二个参数是生成密钥的长
    AutoSeededRandomPool rnd(false, 256);


    // Generate private key
    // 生成私钥
    ePrivateKey.Initialize(rnd, ASN1::secp256r1());

    // Generate public key using private key
    // 用私钥生成密钥
    ePrivateKey.MakePublicKey(ePublicKey);


    HexEncoder pubEncoder(new StringSink(sPublicKey));
    ePublicKey.DEREncode(pubEncoder);
    pubEncoder.MessageEnd();

    HexEncoder prvEncoder(new StringSink(sPrivateKey));
    ePrivateKey.DEREncode(prvEncoder);
    prvEncoder.MessageEnd();
}

string signe (string message)
{
    std::string signature="";

    //数字签名过程
    CryptoPP::ECDSA<CryptoPP::ECP, CryptoPP::SHA1>::PrivateKey privateKey;
    std::string exp = sPrivateKey.substr(70);

    CryptoPP::HexDecoder decoder;
    decoder.Put((CryptoPP::byte *)&exp[0], exp.size());
    decoder.MessageEnd();

    CryptoPP::Integer x;
    x.Decode(decoder, decoder.MaxRetrievable());

    privateKey.Initialize(CryptoPP::ASN1::secp256r1(), x);

    CryptoPP::ECDSA<CryptoPP::ECP, CryptoPP::SHA1>::Signer signer( privateKey );

    CryptoPP::AutoSeededRandomPool prng;

    //签名结果
    signature = "";

    CryptoPP::StringSource s( message, true /*pump all*/,
                             new  CryptoPP::SignerFilter( prng,
                                                         signer,
                                                         new  CryptoPP::StringSink( signature )
                                                         ) // SignerFilter
                             ); // StringSource

    return signature;
    //签名过程结束
}

bool VerifierSignature(string signature,string message)
{
    std::string pt="";

    //验签过程
    CryptoPP::ECDSA<CryptoPP::ECP, CryptoPP::SHA1>::PublicKey publicKey;

    pt = sPublicKey.substr(54);

    CryptoPP::HexDecoder decoder;
    decoder.Put((CryptoPP::byte *)&pt[0], pt.size());
    decoder.MessageEnd();

    CryptoPP::ECP::Point q;
    size_t len = decoder.MaxRetrievable();

    q.identity = false;
    q.x.Decode(decoder, len/2);
    q.y.Decode(decoder, len/2);

    publicKey.Initialize( CryptoPP::ASN1::secp256r1(), q );

    CryptoPP::ECDSA<CryptoPP::ECP,CryptoPP::SHA1>::Verifier verifier(publicKey);

    // Result of the verification process
    bool result = false;

    CryptoPP::StringSource ss( signature+message, true /*pump all*/,
                              new CryptoPP::SignatureVerificationFilter(
                                                                        verifier,
                                                                        new CryptoPP::ArraySink((CryptoPP::byte *)&result, sizeof(result) )
                                                                        )
                              );
    return result;
}

int main()
{
    std::string message = "Yoda said, Do or do not. There is no try.";
    std::string signature="";
    bool result = false;

    GenerateEccKeys();

    signature = signe (message);

    result = VerifierSignature(signature,message);
    cout << "****** tester la bon*****" << endl;
    cout << result << endl;

    result = VerifierSignature(signature,"1234567890");
    cout << "****** tester la mauvais*****" << endl;
    cout << result << endl;

}
10/08/2018 19:01 下午 posted in  Bitcoin

iOS - ECC椭圆曲线、ECDSA签名验签和ECIES加解密

前言

ECC英文全称"Ellipse Curve Cryptography",与传统的基于大质数因子分解困难性的加密方法不同,ECC通过椭圆曲线方程式的性质产生密钥

ECC164位的密钥产生一个安全级,相当于RSA 1024位密钥提供的保密强度,而且计算量较小,处理速度更快,存储空间和传输带宽占用较少。目前我国居民二代身份证正在使用 256 位的椭圆曲线密码,虚拟货币比特币也选择ECC作为加密算法。

加密

基于这个秘密值,用来对Alice和Bob之间的报文进行加密的实际方法是适应以前的,最初是在其他组中描述使用的离散对数密码系统。这些系统包括:

Diffie-Hellman—ECDH

MQV—ECMQV

ElGamal discrete log cryptosystem—ECElGamal

数字签名算法—ECDSA

对于ECC系统来说,完成运行系统所必须的群操作比同样大小的因数分解系统或模整数离散对数系统要慢。不过,ECC系统的拥护者相信ECDLP问题比DLP或因数分解问题要难的多,并且因此使用ECC能用小的多的密钥长度来提供同等的安全,在这方面来说它确实比例如RSA之类的更快。到目前为止已经公布的结果趋于支持这个结论,不过一些专家表示怀疑。

ECC被广泛认为是在给定密钥长度的情况下,最强大的非对称算法,因此在对带宽要求十分紧的连接中会十分有用。

优点

安全性高

有研究表示160位的椭圆密钥与1024位的RSA密钥安全性相同。

处理速度快

在私钥的加密解密速度上,ecc算法比RSA、DSA速度更快。
存储空间占用小。
带宽要求低。
以上为ECC椭圆曲线算法需要了解的基本知识,摘自强大的百科度娘。

iOS-ECC

关于ECC,苹果支持以下算法:

PKG:

curves P-224, P-256, P-384, P-521

PKV:

curves P-224, P-256, P-384, P-521

Signature Generation:

curves P-224, P-256, P-384, P-521

using (SHA-224, SHA-256, SHA384, SHA512)

Signature Verification:

curves P-224, P-256, P-384, P-521

using (SHA-1, SHA-224, SHA-256, SHA384, SHA512)

采用的都是NIST标准和规范。但是苹果官方API仅为开发者提供了椭圆曲线P-256的256位EC密钥。由于苹果SEP硬件提供的保护机制,私钥会直接以keychain的形式截留在SEP中,不能提取,也不能从外部导入,只能通过引用使用。

ECDSA

椭圆曲线数字签名算法(ECDSA)是使用椭圆曲线密码(ECC)对数字签名算法(DSA)的模拟,下面是关于ECDSA的API调用。

1、创建ECC椭圆曲线的keychain属性,属性设置具体可以根据自己需要,获取ECC私钥。

sacObject = SecAccessControlCreateWithFlags(kCFAllocatorDefault,  
                                             kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly,  
                                             // kSecAccessControlTouchIDAny |  
                                             kSecAccessControlPrivateKeyUsage, &error);  
    
 // Create parameters dictionary for key generation.  
 NSDictionary *parameters = @{  
                              (id)kSecAttrTokenID: (id)kSecAttrTokenIDSecureEnclave,  
                              (id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom,  
                              (id)kSecAttrKeySizeInBits: @256,  
                              (id)kSecAttrLabel: @"my-se-key",  
                              (id)kSecPrivateKeyAttrs: @{  
                                      (id)kSecAttrAccessControl: (__bridge_transfer id)sacObject,  
                                      (id)kSecAttrIsPermanent: @YES,  
                                      }  
                              };
NSError *gen_error = nil;
//根据参数生成私钥
id privateKey = CFBridgingRelease(SecKeyCreateRandomKey((__bridge CFDictionaryRef)parameters, (voidvoid *)&gen_error));
2.使用私钥提取公钥,并用于签名。

//根据keychain的属性查找ECC私钥,并获取私钥引用。

NSDictionary *params = @{
 
(id)kSecClass: (id)kSecClassKey, (id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom, (id)kSecAttrKeySizeInBits: @256, (id)kSecAttrLabel: @"my-se-key", (id)kSecReturnRef: @YES, (id)kSecUseOperationPrompt: @"Authenticate to sign data" };

SecKeyRef privateKey;  
      OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)params, (CFTypeRef *)&privateKey);
     

3.签名

NSError *error;  
NSData *dataToSign = [@"我是签名内容" dataUsingEncoding:NSUTF8StringEncoding];  
NSData *signature = CFBridgingRelease(SecKeyCreateSignature(privateKey, kSecKeyAlgorithmECDSASignatureMessageX962SHA256, (CFDataRef)dataToSign, (voidvoid *)&error));

对于kSecKeyAlgorithmECDSASignatureMessageX962SHA256签名算法,官方还给了:SHA1、SHA224、SHA384、SHA512用于EC密钥摘要。可以自己需求选择签名对应的摘要算法。API的名字也很明确的给了这里执行的标准规范为X9.62。

4.验签

//提取公钥,进行验签,验签选择的算法必须与签名时的算法一致。
 
id publicKey = CFBridgingRelease(SecKeyCopyPublicKey((SecKeyRef)privateKey));
 
Boolean verified = SecKeyVerifySignature((SecKeyRef)publicKey, kSecKeyAlgorithmECDSASignatureMessageX962SHA256, (CFDataRef)dataToSign, (CFDataRef)signature, (void *)&error); if (verified == 1) { message = [NSString stringWithFormat:@"signature:%@ verified:%@ error:%@", signature, @"验签成功", error]; }else{ message = [NSString stringWithFormat:@"signature:%@ verified:%@ error:%@", signature, @"验签失败", error]; }

##ECIES
校验密钥是否和算法是否匹配,只有都符合条件了才能用于加密。

SecKeyAlgorithm algorithm = kSecKeyAlgorithmECIESEncryptionCofactorX963SHA256AESGCM;  
BOOL canEncrypt = SecKeyIsAlgorithmSupported((SecKeyRef)publicKey,kSecKeyOperationTypeEncrypt, algorithm);

加密

CFErrorRef error = NULL;  
   cipherText = (NSData*)CFBridgingRelease(      // ARC takes ownership  
                                           SecKeyCreateEncryptedData(publicKey,  
                                                                     algorithm,  
                                                                     (__bridge CFDataRef)encryptionData,&error));

encryptionData为要加密的数据,这里提示一下:

As an additional check before encrypting, because asymmetric encryption restricts the length of the data that you can encrypt, verify that the data is short enough. For this particular algorithm, the plain text data must be 130 bytes smaller than the key’s block size, as reported by SecKeyGetBlockSize. You therefore further condition the proceedings on a length test:

NSData* plainText = ;
canEncrypt &= ([plainText length] < (SecKeyGetBlockSize(publicKey)-130));

官方API描述,明文数据要比密钥块小130个字节。

解密

CFErrorRef error = NULL;  
    clearText = (NSData*)CFBridgingRelease(       // ARC takes ownership  
                                           SecKeyCreateDecryptedData(private,  
                                                                     algorithm,  
                                                                     (__bridge CFDataRef)cipherText,

https://developer.virgilsecurity.com/docs/sdk-and-tools

https://medium.com/@edisonlo/objective-c-digital-signature-signing-and-verification-with-pem-der-or-base64-string-aff4c0a7f805

https://kjur.github.io/jsrsasign/sample/sample-ecdsa.html

https://forums.developer.apple.com/thread/87758

PUBLIC_KEY = "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAESJCvH4lEoGgLof637UGdAYHwFW0GddD/DbVu8yFVTt5Zq+kkftDpQDelSnhmmbr9v+ZsIESINctknP3LTbeLIg==";
PRIVATE_KEY = "MIGNAgEAMBAGByqGSM49AgEGBSuBBAAKBHYwdAIBAQQgi5h75Y80gEeJQQZ6zq7zjT9a11lyLhf9kF/ItIGFDHCgBwYFK4EEAAqhRANCAARIkK8fiUSgaAuh/rftQZ0BgfAVbQZ10P8NtW7zIVVO3lmr6SR+0OlAN6VKeGaZuv2/5mwgRIg1y2Sc/ctNt4si";

09/30/2018 16:36 下午 posted in  Bitcoin

使用JSONRPC创建账户

创建成功返回的JSON:
创建是用Action进行的,包含三个action,newaccount,buyram,delegatebw,
先对各自的abi进行参数对齐 然后进行abi_json_to_bin请求 最后进行整合请求

{
    "timestamp": "2018-09-10T09:33:29.000",
    "producer": "funnyhamster",
    "confirmed": 0,
    "previous": "00d06974a6485b5e6c80d804313d6c0058870848f103735fb24ae63851a61f66",
    "transaction_mroot": "00eb3fbea28360f5ab26cae9cf50c081baf5579800aa6062f059808ad9b44d7a",
    "action_mroot": "10a2e5ea43847603f2d68bc5b403eea5ec744b70d38ff1d73e859fbe9413e6c6",
    "schedule_version": 217,
    "new_producers": null,
    "header_extensions": [],
    "producer_signature": "SIG_K1_K26xvdaxUhQcE6Cga1fQfDLKxVYKpAG9nNk8isA4Pby8ZH2Jp8y8Z6ki8p3ZHAvuBFTJDrNTAW6va9Xbd1R7E8DoosQdXE",
    "transactions": [
        {
            "status": "executed",
            "cpu_usage_us": 7238,
            "net_usage_words": 43,
            "trx": {
                "id": "929ecaca3aa6aa09a515239ff79e78b4df62bf87809a45a7baf2848696c3f094",
                "signatures": [
                    "SIG_K1_KVJz9woQUmHRCot6prUeeY3H8vjZBSStUhefQhrmzsCDKS8MkqYV2ANgfPeSpdEgmAnqX39WnKraCGiwJYedzKkbu5nBnS"
                ],
                "compression": "none",
                "packed_context_free_data": "",
                "context_free_data": [],
                "packed_trx": "e03a965b2868e3b6720100000000030000000000ea305500409e9a2264b89a0150c810611a17b58b00000000a8ed32326650c810611a17b58b208269a69996b1ca01000000010002aa1bf6ea68794eb7ad364ea9e3438171866a557f1db49f596c67eab2a6a0d7230100000001000000010002aa1bf6ea68794eb7ad364ea9e3438171866a557f1db49f596c67eab2a6a0d723010000000000000000ea3055000000004873bd3e0150c810611a17b58b00000000a8ed32322050c810611a17b58b208269a69996b1caa08601000000000004454f53000000000000000000ea305500003f2a1ba6a24a0150c810611a17b58b00000000a8ed32323150c810611a17b58b208269a69996b1caa08601000000000004454f5300000000a08601000000000004454f53000000000100",
                "transaction": {
                    "expiration": "2018-09-10T09:35:28",
                    "ref_block_num": 26664,
                    "ref_block_prefix": 24295139,
                    "max_net_usage_words": 0,
                    "max_cpu_usage_ms": 0,
                    "delay_sec": 0,
                    "context_free_actions": [],
                    "actions": [
                        {
                            "account": "eosio",
                            "name": "newaccount",
                            "authorization": [
                                {
                                    "actor": "liulian12345",
                                    "permission": "active"
                                }
                            ],
                            "data": {
                                "creator": "liulian12345",
                                "name": "testhahaha12",
                                "owner": {
                                    "threshold": 1,
                                    "keys": [
                                        {
                                            "key": "EOS6BQZmWhPKZLVg4YDcR6KdrYb1dqjDAWxyGPHRTD6mpqJ1dACAp",
                                            "weight": 1
                                        }
                                    ],
                                    "accounts": [],
                                    "waits": []
                                },
                                "active": {
                                    "threshold": 1,
                                    "keys": [
                                        {
                                            "key": "EOS6BQZmWhPKZLVg4YDcR6KdrYb1dqjDAWxyGPHRTD6mpqJ1dACAp",
                                            "weight": 1
                                        }
                                    ],
                                    "accounts": [],
                                    "waits": []
                                }
                            },
                            "hex_data": "50c810611a17b58b208269a69996b1ca01000000010002aa1bf6ea68794eb7ad364ea9e3438171866a557f1db49f596c67eab2a6a0d7230100000001000000010002aa1bf6ea68794eb7ad364ea9e3438171866a557f1db49f596c67eab2a6a0d72301000000"
                        },
                        {
                            "account": "eosio",
                            "name": "buyram",
                            "authorization": [
                                {
                                    "actor": "liulian12345",
                                    "permission": "active"
                                }
                            ],
                            "data": {
                                "payer": "liulian12345",
                                "receiver": "testhahaha12",
                                "quant": "10.0000 EOS"
                            },
                            "hex_data": "50c810611a17b58b208269a69996b1caa08601000000000004454f5300000000"
                        },
                        {
                            "account": "eosio",
                            "name": "delegatebw",
                            "authorization": [
                                {
                                    "actor": "liulian12345",
                                    "permission": "active"
                                }
                            ],
                            "data": {
                                "from": "liulian12345",
                                "receiver": "testhahaha12",
                                "stake_net_quantity": "10.0000 EOS",
                                "stake_cpu_quantity": "10.0000 EOS",
                                "transfer": 1
                            },
                            "hex_data": "50c810611a17b58b208269a69996b1caa08601000000000004454f5300000000a08601000000000004454f530000000001"
                        }
                    ],
                    "transaction_extensions": []
                }
            }
        }
    ],
    "block_extensions": [],
    "id": "00d06975eb6fb6c7c281c3f68819489c27a2f876e3820f8bc37bb09f09f6d4ef",
    "block_num": 13658485,
    "ref_block_prefix": 4140007874
}
09/10/2018 16:40 下午 posted in  EOS

EOS开发资料备注

09/06/2018 15:26 下午 posted in  EOS

区块链中,交易被如何打包进区块

R:https://www.tangshuang.net/4097.html

大部分材料都详细分析了挖矿过程,介绍了区块是如何产生的。然而,区块的产生并不是区块链的最终目的,保存交易信息才是区块链的最终目的。所以,更重要的一点是要理解,交易信息是如何被打包进区块链的。

##输入和输出

一个交易在系统里被输入和输出表示。输入是指这笔交易所要进行转移的币来自之前的哪些输出。输出是指这些币将会被发送给哪些地址。在区块链上记账,不是告诉你一个账号现在有多少钱,而是告诉你这个账号当前得到了哪些输出。比如一个地址xsw0923sdfew2389dsfw它的相关记录里面,有A、B、C三个输出的目标地址是它,那么它实际上的余额就是这三个输出的金额的总和。

但是现在这个地址的用户要转账了,转账过程不是直接从总和数字中取出一部分进行转移,而是分别从A、B、C三个输出中取出部分或全部,加起来为想要转移的总和的币,进行转移。这个“取出”过程中,A、B、C就变成了输入,转账目标记录才是这次交易的输出。

这样的设计,保证了区块链中的钱从哪里来,到哪里去,一清二楚,绝不含糊。跟会计做账一样,花一笔钱,不单单要记录它花到哪里去了,还要记录这笔钱是从哪里来的,整个资金链是可追溯的,这也保证了交易不可伪造,资金既不会凭空消失,也不会无中生有。

##hash

将交易加入到区块里面需要涉及三个hash,一个是交易本身的hash,另一个是当前这个区块所有交易的merkle hash root,还有一个就是区块hash。

上面已经说过输入与输出了,一个交易可能包含多个输入输出,通过将这些输入输出信息进行排列并进行hash运算,就得到一个交易的唯一hash值。

一个区块里面包含了多个交易,包括挖矿奖励交易,这些交易都被通过一个merkle运算,得到一个hash root所包含,对于merkle可以阅读《区块链如何运用merkle tree验证交易真实性》了解。

区块都hash运算里面,merkle hash root作为一个参数,因此,所有交易的信息都体现在了一个区块的hash里面。

Q:
挖矿成功产生的hash值代表的区块的所有信息,应该包含了交易信息。为了保证区块未被篡改。在区块接收到上一个区块广播出的hash验证合理后,开始计算下一块hash,根据情况加入收到的交易信息。直到完成POW证明。代表区块已经产生(挖矿成功)

##挖矿

挖矿过程就是计算上述区块hash的过程,几乎所有的机器都可以挖矿成功。关键在于谁先挖到矿,因为当一台机器挖矿成功就向网络广播,其他挖矿在对这个hash进行校验之后,就停止自己的挖矿,开始基于这个区块挖新的矿。而每一个被挖到区块中记录的第一笔交易是给挖到这个区块的矿工自己的奖励金,所以抢到第一个挖矿成功名额对于矿工来说至关重要。

前面说过,计算区块hash过程里面,会以区块包含的交易的merkle hash root作为计算的一个参数,因此,挖矿时,矿工会事先从自己本地的交易信息里面提炼出merkle hash root,也就是说,在挖矿之前,对于当前这个矿工来说,新区块会包含哪些交易就已经确定好了的。关于这个过程,可以阅读《Merkle Tree》。

##打包交易记录

挖矿成功之后,矿工需要将完整的区块向网络广播,这个时候,这个区块里面就打包了所有上述对应的交易。

现在有三个问题:

在打包开始之前,这些交易记录是以什么方式存在于网络?
打包是否会把所有交易记录打包进区块?怎么可能保证所有交易都不被遗漏?
如何防止矿工伪造交易?将伪造的交易打包进区块?
##手续费

这里需要知道另外一个概念,即“手续费”。手续费是发起交易的节点决定给的,和小费性质一样。比如A打算转给B0.5个BTC,A手上有一个完整的(来自一个输入)BTC,这时A将这1BTC作为输入,输出包含两条,一条是给B0.5BTC,另一条是给自己0.49BTC(这个过程叫“找零”)。那么这个交易中就有0.01BTC是消失了,消失了的BTC将作为小费奖赏给矿工。

现在我们把自己的角色转换为矿工,当我们从自己的内存中收集所有交易,准备打包区块时,发现这里有一条交易有0.01BTC的小费,于是我把它作为本次打包优先考虑的交易记录。由于每个区块的大小限制在1M左右,所有我只选了那些给小费的交易打包进这次区块。而那些未给交易费的交易,在优先考虑完这些有交易费的交易之后,我才会考虑把它们加进来。

这也就是为什么有些交易被确认很快,有些交易被确认很慢。

##确认

“确认”这个概念也要解释一下,一个区块产生之后,它不是立即可信的,网络上的节点总是相信最长的区块链,当一条交易记录被打包进一个区块之后,就有了一个确认,而这个区块所在的链后面被再加入一个区块,就是第二个确认,如此下去,一个交易有了6个确认,我们就认为这个交易已经确定了,会被永远记录在区块链中。为什么是6个确认呢?因为每一个确认就是一个挖矿过程,都需要提供非常严格的计算,因此,这6个区块被同一个矿工创建的可能性微乎其微(可以说是不可能),因此矿工伪造交易也基本不可能。

由于比特币的区块平均产生时间是10分钟,所以一个交易要1小时左右才能保证成功(最快),不过也不是所有的系统都这样认为,有些网站在接受比特币支付时,认为4个确认就可以给客户发货了。如果不幸这个交易在创建的时候,没有被打包进最近的那个区块,那就要延迟10分钟,如此下去,如果后面过了好几个区块,交易都没有被打包进区块链,那就悲剧了。

##广播交易

不过也不用着急,比特币系统中只留给了这种优先级高的交易50k的存储空间,即使你没有给交易费,也可能在24小时内被打包进区块。不过也不一定,有些交易可能永远都进不了区块,因为矿工是从自己都内存中获取自己暂存的交易信息,一旦这些内存被释放,那么这些交易信息就会被清空。为了解决这个问题,比特币钱包需要不断对自己发起的交易进行检查,如果发现没有被打包进最新的区块,就要对网络广播,这样,这个交易就会在网络里不断被提起,矿工又可以把这笔交易写进自己的内存里暂时放着,等到下次打包区块时,选择是否把它打包进去。

Q:
是否将某个交易打包进挖的那个区块确实是矿工自己决定的,所以才会有51%攻击,当你手上控制了超过51%的算力,你就决定了整个网络哪些交易可以被加入到区块链,哪些可能永远都不会。
这和“只保留50K”并不矛盾,那50K是给有手续费的交易的,即使你机器里面还有其他包含了手续费的交易,你也加不到这个块里面来,剩下的大部分空间都是留给你机器上其他交易记录的。因为一次打包不一定可以把当前网络未进链的交易都装进来,所以还有一些交易被留在了网络中,当新块产生之后,这些交易都发起者会去检查,如果自己都交易没有进块,客户端会再次广播自己的交易。一般情况下,产生时间越早的交易会被优先打包,(虽然有些矿工可能把某个节点列入了黑名单,但其他矿工也有可能抢到记账权),24小时足以给这些交易进链的时间。要是正常网速情况下,过了24小时没进链,就说明整个网络可能面临风险,因为现在好像还无法取消一笔发出的交易。

##小结

本文讲解了对于一个交易而言,“创建(输入输出)-广播-挖矿-打包-确认”的整个过程,读完你应该可以理解交易是怎么被打包进区块的了。

09/04/2018 15:16 下午 posted in  Bitcoin

秘钥盒子

存储数字钱包的秘钥 通过蓝牙进行秘钥加密和传输

08/30/2018 16:33 下午 posted in  Bitcoin

BTC 比特币资料集

BTC 区块链浏览器

blockchain
https://testnet.blockchain.info
https://blockchain.info
https://www.blockchain.com/explorer
助记词转换器

Mnemonic Code Converter
https://iancoleman.io
https://iancoleman.io/bip39/
BTC 矿工费

https://bitcoinfees.earn.com/#fees
BTC 测试币领取

戳这里领取 BTC 测试币
请使用测试网的 BTC 地址(Bitcoin Testnet Address)!!!

BTC 钱包客户端

https://bitcoin.org/en/choose-your-wallet
http://webhdwallet.github.io
BTC 钱包开源项目

Bitcoin Github 搜索结果
开源库:

bitcoinj:
A library for working with Bitcoin
https://bitcoinj.github.io/
https://github.com/bitcoinj/bitcoinj

CoreBitcoin:
Awesome Bitcoin toolkit for ObjC and Swift
https://github.com/oleganza/CoreBitcoin

Bitcoin:
Bitcoin Core integration/staging tree
https://github.com/bitcoin

bitheri:
http://bither.net
https://github.com/bither/bitheri

BitcoinKit
Bitcoin protocol toolkit for Swift
https://github.com/kishikawakatsumi/BitcoinKit
https://github.com/Jax1993/BitcoinKit

Bitcoin Developer Reference:
https://bitcoin.org/en/developer-reference
https://bitcoin.org/en/developer-guide
https://bitcoin.org/zh_CN/
https://bitcoin.org/zh_CN/development#more

BitcoinDeveloperGuide_zhcn:比特币开发者指南(中文版)
https://github.com/vacing/BitcoinDeveloperGuide_zhcn
https://www.yiyibooks.cn/Gamma/bitcoin/developer-guide.html

Bitcoin Projects:
http://www.bitcoinprojects.net
https://bitcoin.org/en/wallets/mobile/ios/
BitcoinSwift:
A native framework for working with Bitcoin on iOS and OSX
https://github.com/DoubleSha/BitcoinSwift
https://github.com/blocktree/BitcoinSwift
iOS BTC Wallet:

breadwallet-ios:
bread - bitcoin wallet
http://breadapp.com
https://github.com/voisine/breadwallet-ios
BitStore-iOS:
Native iOS Bitcoin wallet
http://bitstoreapp.com
https://github.com/BitStore/BitStore-iOS

iOS_Blockchain-Merchant:
Blockchain, merchant, QR code, Bitcoin wallet, BTC, Payment integration
https://github.com/chuch0805/iOS_Blockchain-Merchant

chance_btc_wallet:
Chance Bitcoin Wallet — swift opensource bitcoin wallet
https://github.com/zhiquan911/chance_btc_wallet

Airbitz
Airbitz iOS GUI
https://github.com/Airbitz/airbitz-ios-gui-private

WalletCordova
GreenAddress' open source Android (Cordova) client https://greenaddress.it/
https://github.com/greenaddress/WalletCordova

CoinSpace
Coin.Space Digital currency wallet https://www.coin.space
https://github.com/CoinSpace/CoinSpace

Bither
Bither - a simple and secure Bitcoin wallet! http://bither.net
https://github.com/bither/bither-ios

arcbit-ios
arcbit - iOS bitcoin wallet http://arcbit.io
https://github.com/arcbit/arcbit-ios

mycelium-wallet-ios
Mycelium Bitcoin Wallet for iOS. http://mycelium.com
https://github.com/mycelium-com/wallet-ios

Android BTC Wallet:

samourai-wallet-android
Samourai Bitcoin Wallet for Android. https://samouraiwallet.com
https://github.com/Samourai-Wallet/samourai-wallet-android

mycelium-wallet-android
Mycelium Bitcoin Wallet for Android http://mycelium.com
https://github.com/mycelium-com/wallet-ios

BTC API 文档:

BTC.com:
https://btc.com/api-doc
BIP:

比特币改进方案 Bitcoin Improvement Proposals
https://github.com/bitcoin/bips

比特币改进协议BIP32(翻译)
https://blog.csdn.net/pony_maggie/article/details/76178228

区块链钱包之BIP32, BIP39, BIP44
https://blog.csdn.net/qq634416025/article/details/79686015

BTC 电子书:

《精通比特币》:
http://zhibimo.com/read/wang-miao/mastering-bitcoin/index.html

《中本聪(Satoshi Nakamoto)比特币白皮书英文版》
https://github.com/GammaGao/bitcoinwhitepaper/blob/master/bitcoin_en.pdf

《中本聪(Satoshi Nakamoto)比特币白皮书中文版》
https://github.com/GammaGao/bitcoinwhitepaper/blob/master/bitcoin_cn.pdf

《比特币开发者指南英文版》
https://bitcoin.org/en/developer-guide

《比特币开发者指南中文版》
https://www.yiyibooks.cn/Gamma/bitcoin/developer-guide.html
https://github.com/vacing/BitcoinDeveloperGuide_zhcn
https://legacy.gitbook.com/book/vacing/bitcoindeveloperguide_zhcn/details

《The Internet of Money》(第1卷)中文版
https://github.com/BtcGroupCn/TheInternetOfMoney_1

BTC 文章:

比太钱包的博客
http://blog.sina.com.cn/bither

nuptuser的专栏
https://blog.csdn.net/gammag/article/category/6954035

老杨QQ122209017的博客
https://blog.csdn.net/sinat_34070003/article/category/7582246

浅析比特币的找零机制
https://blog.csdn.net/wo541075754/article/details/53560061

比特币 区块链 几种交易标准详解 P2PKH、P2PK、MS、P2SH加密方式
https://blog.csdn.net/jerry81333/article/details/56824166

BIP16 P2SH交易脚本
https://blog.csdn.net/sinat_34070003/article/details/79871044

比特币系统的脚本(Script)——交易生成和验证的原理(第一部分)(初稿)
https://blog.csdn.net/taifei/article/details/73321293

多签名交易和P2SH
http://blog.sina.com.cn/s/blog_130223eeb0101ipwi.html

理解比特币脚本
https://zhuanlan.zhihu.com/p/25461051

比特币脚本及交易分析 - 智能合约雏形
https://blockflow.net/t/topic/196

Linuxest
https://blog.csdn.net/ddk3001/article/category/7736876

一文看懂怎样用 Python 创建比特币交易
https://blog.csdn.net/Blockchain_lemon/article/details/79798913

隔离见证
https://blog.csdn.net/taifei/article/details/73479998
https://www.jianshu.com/p/2a5e038074a0
https://baike.baidu.com/item/隔离见证/22448459?fr=aladdin
https://blog.csdn.net/sinat_34070003/article/details/79893114

作者:AlleniCoder
链接:https://www.jianshu.com/p/5521eecb0ad0
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

08/28/2018 13:21 下午 posted in  Bitcoin

Testnet测试网络配置

08/28/2018 13:20 下午 posted in  Bitcoin

EOS 超级节点的五个使命

在EOS系统中,有“两股势力”是整个系统最关键的因素,那就是项目方和见证人。很多人觉得EOS这个项目“奇葩”,就奇葩在项目方和见证人的关系上。EOS的项目方是BlockOne公司,创始人是BlockOne公司的首席技术官(CTO)Daniel Larimer,坊间称BM(ByteMaster)。BlockOne公司曾经多次声明,其公司只开发EOS软件,并免费提供给任何想使用软件的人,公司不负责任何EOS网络启动。更令投资者难以接受的是,BlockOne公司公开宣称,其公司众筹的EOS代币未来可能不具有任何价值和效用,公司不为EOS代币的价值负责,令很多人觉得BlockOne公司不“靠谱”。

其实BlockOne公司这样的声明是有意为之的,众所周知美国对众筹的管理非常严格,一旦众筹行为所分发的代币被美国政府定义为公司的股份或证券,就会立即被界定为非法集资。比特币就是因为被界定为了资产,而不是股份,才能在美国合法进行交易。为了让EOS代币被界定为资产,BlockOne公司只好宣布不为EOS代币负责,也不负责启动真正的EOS系统。

BlockOne公司如此的定位,就给EOS系统的见证人更多的责任和使命,EOS系统见证人的角色可以从以下几点来理解:

##1.硬件资源

任何计算机系统都是由软件和硬件两大部分组成的,BlockOne公司提供软件,硬件部分则由见证人提供,双方各司其职,组成了一个完整的计算机系统。

##2.社区建设

对于BlockOne公司来说,管理和建设如此大规模的社区是不现实的,而见证人分布于世界合格国家,了解本地的EOS代币持有者。见证人在当地进行宣传活动,吸引选票的同时,也相当于建立了一个当地的EOS社区。所有的见证人社区组合起来,就建立了世界最大的一个区块链社区。

##3.社区治理

从社区治理来看,见证人相当于议会,每个见证人都是由当地选民选举出来的,非常像西方的代议制民主。见证人中的大多数(三分之二)通过的决策,就代表了大多数选民(EOS代币持有者)的观点。见证人组织就是EOS系统的最高权力。但BlockOne官方也可以制衡见证人,因为BlockOne官方尤其是BM本人在EOS代币持有者中有巨大影响力,可以左右选民的投票。而且BlockOne官方和BM本人也持有大量EOS代币,他们可以直接进行投票来影响见证人选举,双方互相制约。

##4.去中心化

从整个EOS系统的去中心化机制来看,见证人担负着系统去中心化的使命。BlockOne公司是“中心化”的,理论上可以被强力控制。EOS见证人分散在时间各地,运行和维护EOS系统,共同保存系统账本,维护各地的EOS社区。理论上来讲,只要系统还剩下最后一个见证人,EOS系统都不会崩溃,都可以等到其他见证人恢复再重新加入系统。几十个见证人+后备见证人保证了EOS系统的高度去中心化。也保证了系统的冗余度和稳定性。

##5.应用生态

一个去中心化智能合约系统,最核心的就是智能合约(Dapp)本身的开发。没有大量优秀Dapp的智能合约系统,就像是iOS没有了大的第三方应用,整个系统就没有任何价值。BlockOne官方的精力主要集中在在系统的开发,那么Dapp的开发生态的培养,一方面需要BlockOne官方设立基金,激励Dapp开发团队,另一方面就需要见证人了。目前EOS系统的发展也是这样的,很多见证人都发布了自己的DApp开发计划,他们与BlockOne的基金一起培育EOS的开发者生态。

见证人对系统做出贡献的同时,当然也会获得相应的奖励。目前见证人的奖励分为出块奖励和得票奖励,前者根据出块的数量,后者根据选举时的得票数。目前EOS系统每年通货膨胀5%,这里面的五分之一会作为见证人的奖励。按总量10以枚计算,第一年用于见证人奖励的代币为1000万枚。按照15美元单价计算,总金额为1.5亿美元。这样的奖励规模使得见证人有足够的资源去维护自己的硬件开支,并持续壮大EOS社区。

BlockOne公司、EOS见证人、EOS代币持有者这三者互相制衡、互相支撑、互相渗透,在区块链世界里独树一帜。

08/15/2018 17:00 下午 posted in  EOS

使用RPC接口新建EOS账户

##1、POST http://127.0.0.1:8888/v1/chain/abi_json_to_bin (序列化新建账号的 json)

{
	"code": "eosio",
	"action": "newaccount",
	"args": {
		"creator": "bitcoin",
		"name": "eason",
		"owner": {
			"threshold": 1,
			"keys": [{
				"key": "EOS4ufZoTw95yHJS6Cyz3h4w5a2W4cyYpMYRnd7gbFZuCfPxUFS6r",
				"weight": 1
			}],
			"accounts": [],
			"waits": []
		},
		"active": {
			"threshold": 1,
			"keys": [{
				"key": "EOS4ufZoTw95yHJS6Cyz3h4w5a2W4cyYpMYRnd7gbFZuCfPxUFS6r",
				"weight": 1
			}],
			"accounts": [],
			"waits": []
		}
	}
}

##2、POST http://127.0.0.1:8888/v1/wallet/sign_transaction(签名新建账号的交易)

[
  {
    "expiration" : "2018-05-17T09:54:06.500",
    "signatures" : [

    ],
    "actions" : [
      {
        "account" : "eosio",
        "data" : "000000603a8ab23b000000ca3d364dfb0100000001000202aba1b7d9fc5de9dd93308dc5ebedcb066c8e5b36970bfd82ae715d9e8c3060010000000100000001000202aba1b7d9fc5de9dd93308dc5ebedcb066c8e5b36970bfd82ae715d9e8c306001000000",
        "authorization" : [
          {
            "actor" : "bitcoin",
            "permission" : "active"
          }
        ],
        "name" : "newaccount"
      }
    ],
    "ref_block_prefix" : 4033496171,
    "ref_block_num" : 363759
  },
  [
    "EOS5wQ4HaFFDxyfc23dZNXUTGBHepM1vXGfr1vkfWHfRfvAMXP7VV"
  ],
  ""
]

##3、http://127.0.0.1:8888/v1/chain/push_transaction (把签名后的交易push 推送到 EOS 系统中,即新建账号完成

{
  "compression" : "none",
  "transaction" : {
    "ref_block_prefix" : 4033496171,
    "actions" : [
      {
        "account" : "eosio",
        "data" : "000000603a8ab23b000000ca3d364dfb0100000001000202aba1b7d9fc5de9dd93308dc5ebedcb066c8e5b36970bfd82ae715d9e8c3060010000000100000001000202aba1b7d9fc5de9dd93308dc5ebedcb066c8e5b36970bfd82ae715d9e8c306001000000",
        "authorization" : [
          {
            "actor" : "bitcoin",
            "permission" : "active"
          }
        ],
        "name" : "newaccount"
      }
    ],
    "expiration" : "2018-05-17T09:54:06.500",
    "ref_block_num" : 363759
  },
  "signatures" : [
    "SIG_K1_KY58QhP4jWLJWr7cVkahgL3JAjC8QMK5jnHurFUmn8xU71v6Mh4DmgjY75DxmWE6Je457N6MRM7GapxU43hywnAWKEmC1W"
  ]
}

(代币转账 和 新建账号的 sign_transaction、push_transaction 类似,主要就是 智能合约的不同 和 调用的action 的不同 以及 action 中具体的参数不同。)

新建账号(newaccount)需要用 「已有的账号」 创建「 新账号」

内部是 已有账号 调用系统智能合约eosio中的 newaccount 的 action

新建账号的交易需要用 创建者 的 私钥签名交易(sign_transaction),然后 推送签名后的交易 (push_transaction)到区块链中。

a. sign_transaction 图示

b. push_transaction 图示

#具体接口

1、POST http://127.0.0.1:8888/v1/chain/abi_json_to_bin (序列化新建账号的 json)

请求参数

请求参数:

参数名称 参数类型 描述
code string 系统智能合约,默认填写“eosio”
action string 智能合约中的action,默认填写“newaccount”
creator string 创建者
name string 新建账号名
key string 新建账号的公钥
请求示例:

{
  "code": "eosio",
  "action": "newaccount",
  "args": {
    "creator": "bitcoin",
    "name": "eason",
    "owner": {
      "threshold": 1,
      "keys": [
        {
          "key": "EOS4ufZoTw95yHJS6Cyz3h4w5a2W4cyYpMYRnd7gbFZuCfPxUFS6r", //owner public key
          "weight": 1
        }
      ],
      "accounts": [],   
      "waits": []      
    },
    "active": {
      "threshold": 1,
      "keys": [
        {
          "key": "EOS4ufZoTw95yHJS6Cyz3h4w5a2W4cyYpMYRnd7gbFZuCfPxUFS6r", //active public key
          "weight": 1
        }
      ],
      "accounts": [],    
      "waits": []        
    }
  }
}

响应参数

参数名称 参数类型 描述
binargs string 序列化的结果,在sign_transaction 和 push_transaction 中作为 data 请求参数
响应示例

{
    "binargs": "000000603a8ab23b000000ca3d364dfb0100000001000202aba1b7d9fc5de9dd93308dc5ebedcb066c8e5b36970bfd82ae715d9e8c3060010000000100000001000202aba1b7d9fc5de9dd93308dc5ebedcb066c8e5b36970bfd82ae715d9e8c306001000000"
}

2、GET http://127.0.0.1:8888/v1/chain/get_info (获取 EOS 区块链的最新区块号)

响应参数

参数名称 参数类型 描述
head_block_num number 最新区块号
响应示例

{
    "server_version": "13952d45",
    "head_block_num": 359934,
    "last_irreversible_block_num": 359934,
    "last_irreversible_block_id": "a69af2c4aa56b5c4bd1cdf9c2acb1a7796bbc3043954e36da182a144ddcf58fb",
    "head_block_id": "a69af2c4aa56b5c4bd1cdf9c2acb1a7796bbc3043954e36da182a144ddcf58fb",
    "head_block_time": "2018-05-17T09:02:12",
    "head_block_producer": "eosio",
    "virtual_block_cpu_limit": 100000000,
    "virtual_block_net_limit": 1048576000,
    "block_cpu_limit": 99900,
    "block_net_limit": 1048576
}

3、POST http://127.0.0.1:8888/v1/chain/get_block (获取最新区块的具体信息)

请求参数

参数名称 参数类型 描述
block_num_or_id number 最新区块号,上一个响应结果中的 head_block_num

{
  "block_num_or_id":359934
}

响应参数

参数名称 参数类型 描述
timestamp string 最新区块的生成时间
block_num number 区块号,作为sign_transaction 和 push_transaction中的 ref_block_num请求参数
ref_block_prefix number 作为sign_transaction 和 push_transaction中的 ref_block_prefix 请求参数

响应示例

{
    "timestamp": "2018-05-17T09:02:12.500",
    "producer": "eosio",
    "confirmed": 0,
    "previous": "00057dfd5044aba0d750eff1fbb84ac92cbf29db1354968816fd2a9aefb0a0b4",
    "transaction_mroot": "0000000000000000000000000000000000000000000000000000000000000000",
    "action_mroot": "dee87e5d025383574ac12c310faf6b759fba52bd19977399b7ebf6ccdd81c7fa",
    "schedule_version": 0,
    "header_extensions": [],
    "producer_signature": "SIG_K1_KVX3RRTS4ch9m6bWDctsAhDWtFydTrg3mW7PaqCXnBZZWezBW23enggeW4ijuWBHBVsDoxzjMvspoFtPsU5nmau4ZYomZo",
    "transactions": [],
    "block_extensions": [],
    "id": "a69af2c4aa56b5c4bd1cdf9c2acb1a7796bbc3043954e36da182a144ddcf58fb",
    "block_num": 359934,
    "ref_block_prefix": 1943477914
}

4、POST http://127.0.0.1:8888/v1/wallet/unlock (解锁钱包,签名交易前,需要解锁账号所在的钱包)

请求参数

参数名称 参数类型 描述
string 钱包名称
string 钱包密码
请求示例

["liu","PW5KjWHnhL5kSRxpWyHQj321dFsZN62HAbZjVSqnDvzKMuEKBZ1T9"]

响应示例

{}  //成功解锁钱包,返回{}

5、POST http://127.0.0.1:8888/v1/wallet/sign_transaction(签名新建账号的交易)

请求的参数

参数名称 参数类型 描述
ref_block_num number 上面获得的最新区块号
ref_block_prefix number 上面获得的最新区块号相关信息
expiration string 过期时间 = timestamp 加上 一段时间 ,例如1分钟
account string 调用系统智能合约账号名,默认为 eosio
name string 新建账号的action,默认为 newaccount
actor string 创建者 账户名
data string abi_json_to_bin 序列化后的 值 binargs
string 创建者的 公钥
请求示例

[
  {
    "ref_block_num": 363759,
    "ref_block_prefix": 4033496171,
    "expiration": "2018-05-17T09:54:06.500",
    "actions": [
      {
        "account": "eosio",  //有 newaccount 的 action 的智能合约账号
        "name": "newaccount",
        "authorization": [
          {
            "actor": "bitcoin",
            "permission": "active"
          }
        ],
        "data": "000000603a8ab23b000000ca3d364dfb0100000001000202aba1b7d9fc5de9dd93308dc5ebedcb066c8e5b36970bfd82ae715d9e8c3060010000000100000001000202aba1b7d9fc5de9dd93308dc5ebedcb066c8e5b36970bfd82ae715d9e8c306001000000" // //abi_json_to_bin 的响应参数 binargs
      }
    ],
    "signatures": []
  },
  [
    "EOS5wQ4HaFFDxyfc23dZNXUTGBHepM1vXGfr1vkfWHfRfvAMXP7VV" //创建者的公钥(交易发起者的公钥),其实是用的公钥对应的私钥进行签名的,签名前需要先解锁包含此私钥的钱包
  ],
  ""
]

响应参数

参数名称 参数类型 描述
signatures string 新建账号的交易 的签名结果,最后 push_transaction 中使用
响应示例

{
    "expiration": "2018-05-17T09:54:06",
    "ref_block_num": 36079,
    "ref_block_prefix": 4033496171,
    "max_net_usage_words": 0,
    "max_cpu_usage_ms": 0,
    "delay_sec": 0,
    "context_free_actions": [],
    "actions": [
        {
            "account": "eosio",
            "name": "newaccount",
            "authorization": [
                {
                    "actor": "bitcoin",
                    "permission": "active"
                }
            ],
            "data": "000000603a8ab23b000000ca3d364dfb0100000001000202aba1b7d9fc5de9dd93308dc5ebedcb066c8e5b36970bfd82ae715d9e8c3060010000000100000001000202aba1b7d9fc5de9dd93308dc5ebedcb066c8e5b36970bfd82ae715d9e8c306001000000" 
        }
    ],
    "transaction_extensions": [],
    "signatures": [
    "SIG_K1_KY58QhP4jWLJWr7cVkahgL3JAjC8QMK5jnHurFUmn8xU71v6Mh4DmgjY75DxmWE6Je457N6MRM7GapxU43hywnAWKEmC1W"   // 签名 用在 push_transaction 中
    ],
    "context_free_data": []
}

6、http://127.0.0.1:8888/v1/chain/push_transaction (把签名后的交易push 推送到 EOS 系统中,即新建账号完成)

请求参数

参数名称 参数类型 描述
compression string 默认 none
data string abi_json_to_bin 序列化后的 值 binargs
signatures string 交易签名后的结果
请求示例

{
  "compression": "none",
  "transaction": {
    "expiration": "2018-05-17T09:54:06.500",
    "ref_block_num": 363759,
    "ref_block_prefix": 4033496171,
    "actions": [
      {
        "account": "eosio",
        "name": "newaccount",
        "authorization": [
          {
            "actor": "bitcoin",
            "permission": "active"
          }
        ],
        "data": "000000603a8ab23b000000ca3d364dfb0100000001000202aba1b7d9fc5de9dd93308dc5ebedcb066c8e5b36970bfd82ae715d9e8c3060010000000100000001000202aba1b7d9fc5de9dd93308dc5ebedcb066c8e5b36970bfd82ae715d9e8c306001000000"    //abi_json_to_bin 的响应参数 binargs
      }
    ]
  },
  "signatures": ["SIG_K1_KY58QhP4jWLJWr7cVkahgL3JAjC8QMK5jnHurFUmn8xU71v6Mh4DmgjY75DxmWE6Je457N6MRM7GapxU43hywnAWKEmC1W"]
}

响应示例

{
    "transaction_id": "2047702bfdc4678aabe123f335b4b5f604203edf7b4de8e42fa2c9211d4de075",
    "processed": {
        "id": "2047702bfdc4678aabe123f335b4b5f604203edf7b4de8e42fa2c9211d4de075",
        "receipt": {
            "status": "executed",
            "cpu_usage_us": 390,
            "net_usage_words": 25
        },
        "elapsed": 390,
        "net_usage": 200,
        "scheduled": false,
        "action_traces": [
            {
                "receipt": {
                    "receiver": "eosio",
                    "act_digest": "ae18e275184e7defe81be175711cd24206990518963f857715e98755f713957c",
                    "global_sequence": 365444,
                    "recv_sequence": 365419,
                    "auth_sequence": [
                        [
                            "bitcoin",
                            27
                        ]
                    ]
                },
                "act": {
                    "account": "eosio",
                    "name": "newaccount",
                    "authorization": [
                        {
                            "actor": "bitcoin",
                            "permission": "active"
                        }
                    ],
                    "data": {
                        "creator": "bitcoin",
                        "name": "zhangjie",
                        "owner": {
                            "threshold": 1,
                            "keys": [
                                {
                                    "key": "EOS4ufZoTw95yHJS6Cyz3h4w5a2W4cyYpMYRnd7gbFZuCfPxUFS6r",
                                    "weight": 1
                                }
                            ],
                            "accounts": [],
                            "waits": []
                        },
                        "active": {
                            "threshold": 1,
                            "keys": [
                                {
                                    "key": "EOS4ufZoTw95yHJS6Cyz3h4w5a2W4cyYpMYRnd7gbFZuCfPxUFS6r",
                                    "weight": 1
                                }
                            ],
                            "accounts": [],
                            "waits": []
                        }
                    },
                    "hex_data": "000000603a8ab23b000000ca3d364dfb0100000001000202aba1b7d9fc5de9dd93308dc5ebedcb066c8e5b36970bfd82ae715d9e8c3060010000000100000001000202aba1b7d9fc5de9dd93308dc5ebedcb066c8e5b36970bfd82ae715d9e8c306001000000"
                },
                "elapsed": 163,
                "cpu_usage": 0,
                "console": "",
                "total_cpu_usage": 0,
                "trx_id": "2047702bfdc4678aabe123f335b4b5f604203edf7b4de8e42fa2c9211d4de075",
                "inline_traces": []
            }
        ],
        "except": null
    }
}

From:使用RPC接口新建EOS账户 - 实战

08/15/2018 16:23 下午 posted in  EOS