1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#include "Signer.h"
8
#include "HexCoding.h"
9
#include "SigData.h"
10
#include "../Ontology/OngTxBuilder.h"
11
#include "../Ontology/OntTxBuilder.h"
12

13
#include "../Hash.h"
14

15
#include <stdexcept>
16

17
using namespace TW;
18
using namespace TW::Ontology;
19

20 1
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
21 1
    auto contract = std::string(input.contract().begin(), input.contract().end());
22 1
    auto output = Proto::SigningOutput();
23
    try {
24 1
        if (contract == "ONT") {
25 1
            auto encoded = OntTxBuilder::build(input);
26 1
            output.set_encoded(encoded.data(), encoded.size());
27 1
        } else if (contract == "ONG") {
28 1
            auto encoded = OngTxBuilder::build(input);
29 1
            output.set_encoded(encoded.data(), encoded.size());
30
        }
31 1
    } catch (...) {
32
    }
33 1
    return output;
34
}
35

36 1
Signer::Signer(TW::PrivateKey priKey) : privateKey(std::move(priKey)) {
37 1
    auto pubKey = privateKey.getPublicKey(TWPublicKeyTypeNIST256p1);
38 1
    publicKey = pubKey.bytes;
39 1
    address = Address(pubKey).string();
40
}
41

42 1
PrivateKey Signer::getPrivateKey() const {
43 1
    return privateKey;
44
}
45

46 1
PublicKey Signer::getPublicKey() const {
47 1
    return PublicKey(publicKey, TWPublicKeyTypeNIST256p1);
48
}
49

50 1
Address Signer::getAddress() const {
51 1
    return Address(address);
52
}
53

54 1
void Signer::sign(Transaction& tx) const {
55 1
    if (tx.sigVec.size() >= Transaction::sigVecLimit) {
56 0
        throw std::runtime_error("the number of transaction signatures should not be over 16.");
57
    }
58 1
    auto signature = getPrivateKey().sign(Hash::sha256(tx.txHash()), TWCurveNIST256p1);
59 1
    signature.pop_back();
60 1
    tx.sigVec.emplace_back(publicKey, signature, 1);
61
}
62

63 1
void Signer::addSign(Transaction& tx) const {
64 1
    if (tx.sigVec.size() >= Transaction::sigVecLimit) {
65 0
        throw std::runtime_error("the number of transaction signatures should not be over 16.");
66
    }
67 1
    auto signature = getPrivateKey().sign(Hash::sha256(tx.txHash()), TWCurveNIST256p1);
68 1
    signature.pop_back();
69 1
    tx.sigVec.emplace_back(publicKey, signature, 1);
70
}

Read our documentation on viewing source code .

Loading