Skip to content

Commit

Permalink
Refactor ontology transaction (#327)
Browse files Browse the repository at this point in the history
* refactor ontology transaction

* fix android CI
  • Loading branch information
NashMiao authored and hewigovens committed Apr 16, 2019
1 parent c9a6fc1 commit 2d30894
Show file tree
Hide file tree
Showing 16 changed files with 232 additions and 281 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ class TestOntologySigning {
contract = "ONG"
method = "balanceOf"
queryAddress = "ANDfjwrUroaVtvBguDtrWKRMyxFwvVwnZD"
nonce = 3486522027.toInt()
}.build()

val result = OntologySigner.sign(input).encoded.toByteArray()
val hex = Numeric.toHexString(result, 0, result.size, false)

assertEquals(
86,
hex.indexOf("1446b1a18af6b7c9f8a4602f9f73eeb3030f0c29b70962616c616e63654f661400000000000000000000000000000000000000020068164f6e746f6c6f67792e4e61746976652e496e766f6b65")
"00d1ab1ad0cf0000000000000000000000000000000000000000000000000000000000000000000000004d1446b1a18af6b7c9f8a4602f9f73eeb3030f0c29b70962616c616e63654f661400000000000000000000000000000000000000020068164f6e746f6c6f67792e4e61746976652e496e766f6b650000",
hex
)
}

Expand All @@ -37,15 +38,15 @@ class TestOntologySigning {
contract = "ONT"
method = "balanceOf"
queryAddress = "ANDfjwrUroaVtvBguDtrWKRMyxFwvVwnZD"
nonce = 3959576200.toInt()
}.build()

val result = OntologySigner.sign(input).encoded.toByteArray()
val hex = Numeric.toHexString(result, 0, result.size, false)

assertEquals(0, hex.indexOf("00d1"))
assertEquals(
86,
hex.indexOf("1446b1a18af6b7c9f8a4602f9f73eeb3030f0c29b70962616c616e63654f66140000000000000000000000000000000000000001")
"00d1885602ec0000000000000000000000000000000000000000000000000000000000000000000000004d1446b1a18af6b7c9f8a4602f9f73eeb3030f0c29b70962616c616e63654f661400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e496e766f6b650000",
hex
)
}

Expand All @@ -61,26 +62,15 @@ class TestOntologySigning {
gasPrice = 500
gasLimit = 20000
payerPrivateKey = "4646464646464646464646464646464646464646464646464646464646464652".toHexBytesInByteString()
nonce = 2338116610.toInt()
}.build()

val result = OntologySigner.sign(input).encoded.toByteArray()
val hex = Numeric.toHexString(result, 0, result.size, false)

val verifyPosition1 = hex.indexOf("00d1")
val verifyPosition2 = hex.indexOf("401000000000000204e000000000000")
val verifyPosition3 = hex.indexOf("00c66b14fbacc8214765d457c8e3f2b5a1d3c4981a2e9d2a6a7cc814feec06b79ed299ea06fcb94abac41aaf3ead76586a7cc8516a7cc86c51c1087472616e73666572")
val verifyPosition4 = hex.indexOf("1400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e496e766f6b")
val verifyPosition5 = hex.indexOf("031bec1250aa8f78275f99a6663688f31085848d0ed92f1203e447125f927b7486")
val verifyPosition6 = hex.indexOf("03d9fd62df332403d9114f3fa3da0d5aec9dfa42948c2f50738d52470469a1a1ee")


assertEquals(724, hex.length)
assertEquals(0, verifyPosition1)
assertEquals(13, verifyPosition2)
assertEquals(86, verifyPosition3)
assertEquals(220, verifyPosition4)
assertEquals(452, verifyPosition5)
assertEquals(656, verifyPosition6)
assertEquals(
"00d102d45c8bf401000000000000204e00000000000057e9d1a61f9aafa798b6c7fbeae35639681d7df67100c66b14fbacc8214765d457c8e3f2b5a1d3c4981a2e9d2a6a7cc814feec06b79ed299ea06fcb94abac41aaf3ead76586a7cc8516a7cc86c51c1087472616e736665721400000000000000000000000000000000000000010068164f6e746f6c6f67792e4e61746976652e496e766f6b6500024140301766d925382a6ebb2ebeb18d3741954c9370dcf6d9c45b34ce7b18bc42dcdb7cff28ddaf7f1048822c0ca21a0c4926323a2497875b963f3b8cbd3717aa6e7c2321031bec1250aa8f78275f99a6663688f31085848d0ed92f1203e447125f927b7486ac414038466b25ac49a22ba8c301328ef049a61711b257987e85e25d63e0444a14e860305a4cd3bb6ea2fe80fd293abb3c592e679c42c546cbf3baa051a07b28b374a6232103d9fd62df332403d9114f3fa3da0d5aec9dfa42948c2f50738d52470469a1a1eeac",
hex)
}

@Test
Expand All @@ -95,26 +85,15 @@ class TestOntologySigning {
gasPrice = 500
gasLimit = 20000
payerPrivateKey = "4646464646464646464646464646464646464646464646464646464646464652".toHexBytesInByteString()
nonce = 2827104669.toInt()
}.build()

val result = OntologySigner.sign(input).encoded.toByteArray()
val hex = Numeric.toHexString(result, 0, result.size, false)

val verifyPosition1 = hex.indexOf("00d1")
val verifyPosition2 = hex.indexOf("401000000000000204e000000000000")
val verifyPosition3 = hex.indexOf("00c66b14fbacc8214765d457c8e3f2b5a1d3c4981a2e9d2a6a7cc814feec06b79ed299ea06fcb94abac41aaf3ead76586a7cc8516a7cc86c51c1087472616e73666572")
val verifyPosition4 = hex.indexOf("1400000000000000000000000000000000000000020068164f6e746f6c6f67792e4e61746976652e496e766f6b")
val verifyPosition5 = hex.indexOf("031bec1250aa8f78275f99a6663688f31085848d0ed92f1203e447125f927b7486")
val verifyPosition6 = hex.indexOf("03d9fd62df332403d9114f3fa3da0d5aec9dfa42948c2f50738d52470469a1a1ee")


assertEquals(724, hex.length)
assertEquals(0, verifyPosition1)
assertEquals(13, verifyPosition2)
assertEquals(86, verifyPosition3)
assertEquals(220, verifyPosition4)
assertEquals(452, verifyPosition5)
assertEquals(656, verifyPosition6)
assertEquals(
"00d19d3182a8f401000000000000204e00000000000057e9d1a61f9aafa798b6c7fbeae35639681d7df67100c66b14fbacc8214765d457c8e3f2b5a1d3c4981a2e9d2a6a7cc814feec06b79ed299ea06fcb94abac41aaf3ead76586a7cc8516a7cc86c51c1087472616e736665721400000000000000000000000000000000000000020068164f6e746f6c6f67792e4e61746976652e496e766f6b6500024140e27e935b87855efad62bb76b21c7b591f445f867eff86f888ca6ee1870ecd80f73b8ab199a4d757b4c7b9ed46c4ff8cfa8aefaa90b7fb6485e358034448cba752321031bec1250aa8f78275f99a6663688f31085848d0ed92f1203e447125f927b7486ac4140450047b2efb384129a16ec4c707790e9379b978cc7085170071d8d7c5c037d743b078bd4e21bb4404c0182a32ee05260e22454dffb34dacccf458dfbee6d32db232103d9fd62df332403d9114f3fa3da0d5aec9dfa42948c2f50738d52470469a1a1eeac",
hex)
}

}
9 changes: 5 additions & 4 deletions src/Ontology/Asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ class Asset {
public:
virtual Data contractAddress() = 0;

virtual Transaction decimals() = 0;
virtual Transaction decimals(uint32_t nonce) = 0;

virtual Transaction balanceOf(const Address& address) = 0;
virtual Transaction balanceOf(const Address &address, uint32_t nonce) = 0;

virtual Transaction transfer(const Signer& from, const Address& to, uint64_t amount,
const Signer& payer, uint64_t gasPrice, uint64_t gasLimit) = 0;
virtual Transaction transfer(const Signer &from, const Address &to, uint64_t amount,
const Signer &payer, uint64_t gasPrice, uint64_t gasLimit,
uint32_t nonce) = 0;
};
} // namespace TW::Ontology
32 changes: 16 additions & 16 deletions src/Ontology/Ong.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,50 @@

#include <list>

#include <TrezorCrypto/rand.h>

using namespace TW;
using namespace TW::Ontology;

Transaction Ong::decimals() {
Transaction Ong::decimals(uint32_t nonce) {
auto builder = ParamsBuilder();
auto invokeCode =
ParamsBuilder::buildNativeInvokeCode(contractAddress(), version, "decimals", Data());
auto tx = Transaction(version, txType, random32(), (uint64_t)0, (uint64_t)0, (std::string) "",
invokeCode);
auto tx =
Transaction(version, txType, nonce, (uint64_t)0, (uint64_t)0, (std::string) "", invokeCode);
return tx;
}

Transaction Ong::balanceOf(const Address& address) {
Transaction Ong::balanceOf(const Address &address, uint32_t nonce) {
auto builder = ParamsBuilder();
auto invokeCode =
ParamsBuilder::buildNativeInvokeCode(contractAddress(), version, "balanceOf", address.data);
auto tx = Transaction(version, txType, random32(), (uint64_t)0, (uint64_t)0, (std::string) "",
invokeCode);
auto tx =
Transaction(version, txType, nonce, (uint64_t)0, (uint64_t)0, (std::string) "", invokeCode);
return tx;
}

Transaction Ong::transfer(const Signer& from, const Address& to, uint64_t amount,
const Signer& payer, uint64_t gasPrice, uint64_t gasLimit) {
Transaction Ong::transfer(const Signer &from, const Address &to, uint64_t amount,
const Signer &payer, uint64_t gasPrice, uint64_t gasLimit,
uint32_t nonce) {
std::list<boost::any> transferParam{from.getAddress().data, to.data, amount};
std::vector<boost::any> args{transferParam};
auto invokeCode =
ParamsBuilder::buildNativeInvokeCode(contractAddress(), 0x00, "transfer", args);
auto tx = Transaction(version, txType, random32(), gasPrice, gasLimit,
payer.getAddress().string(), invokeCode);
auto tx = Transaction(version, txType, nonce, gasPrice, gasLimit, payer.getAddress().string(),
invokeCode);
from.sign(tx);
payer.addSign(tx);
return tx;
}

Transaction Ong::withdraw(const Signer& claimer, const Address& receiver, uint64_t amount,
const Signer& payer, uint64_t gasPrice, uint64_t gasLimit) {
Transaction Ong::withdraw(const Signer &claimer, const Address &receiver, uint64_t amount,
const Signer &payer, uint64_t gasPrice, uint64_t gasLimit,
uint32_t nonce) {
auto ontContract = Address("AFmseVrdL9f9oyCzZefL9tG6UbvhUMqNMV");
std::list<boost::any> args{claimer.getAddress().data, ontContract.data, receiver.data, amount};
auto invokeCode =
ParamsBuilder::buildNativeInvokeCode(contractAddress(), 0x00, "transferFrom", args);
auto tx = Transaction(version, txType, random32(), gasPrice, gasLimit,
payer.getAddress().string(), invokeCode);
auto tx = Transaction(version, txType, nonce, gasPrice, gasLimit, payer.getAddress().string(),
invokeCode);
claimer.sign(tx);
payer.addSign(tx);
return tx;
Expand Down
13 changes: 7 additions & 6 deletions src/Ontology/Ong.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ class Ong : public Asset {
public:
Data contractAddress() override { return ongContract; }

Transaction decimals() override;
Transaction decimals(uint32_t nonce) override;

Transaction balanceOf(const Address& address) override;
Transaction balanceOf(const Address &address, uint32_t nonce) override;

Transaction transfer(const Signer& from, const Address& to, uint64_t amount,
const Signer& payer, uint64_t gasPrice, uint64_t gasLimit) override;
Transaction transfer(const Signer &from, const Address &to, uint64_t amount,
const Signer &payer, uint64_t gasPrice, uint64_t gasLimit,
uint32_t nonce) override;

Transaction withdraw(const Signer& claimer, const Address& receiver, uint64_t amount,
const Signer& payer, uint64_t gasPrice, uint64_t gasLimit);
Transaction withdraw(const Signer &claimer, const Address &receiver, uint64_t amount,
const Signer &payer, uint64_t gasPrice, uint64_t gasLimit, uint32_t nonce);
};

} // namespace TW::Ontology
30 changes: 15 additions & 15 deletions src/Ontology/OngTxBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,57 @@
using namespace TW;
using namespace TW::Ontology;

TW_Ontology_Proto_SigningOutput OngTxBuilder::decimals() {
auto transaction = Ong().decimals();
TW_Ontology_Proto_SigningOutput OngTxBuilder::decimals(const Ontology::Proto::SigningInput &input) {
auto transaction = Ong().decimals(input.nonce());
auto encoded = transaction.serialize();
auto protoOutput = Proto::SigningOutput();
protoOutput.set_encoded(encoded.data(), encoded.size());
auto serialized = protoOutput.SerializeAsString();
return TWDataCreateWithBytes(reinterpret_cast<const uint8_t*>(serialized.data()),
return TWDataCreateWithBytes(reinterpret_cast<const uint8_t *>(serialized.data()),
serialized.size());
}

TW_Ontology_Proto_SigningOutput
OngTxBuilder::balanceOf(const Ontology::Proto::SigningInput& input) {
OngTxBuilder::balanceOf(const Ontology::Proto::SigningInput &input) {
auto queryAddress = Address(input.query_address());
auto transaction = Ong().balanceOf(queryAddress);
auto transaction = Ong().balanceOf(queryAddress, input.nonce());
auto encoded = transaction.serialize();
auto protoOutput = Proto::SigningOutput();
protoOutput.set_encoded(encoded.data(), encoded.size());
auto serialized = protoOutput.SerializeAsString();
return TWDataCreateWithBytes(reinterpret_cast<const uint8_t*>(serialized.data()),
return TWDataCreateWithBytes(reinterpret_cast<const uint8_t *>(serialized.data()),
serialized.size());
}

TW_Ontology_Proto_SigningOutput OngTxBuilder::transfer(const Ontology::Proto::SigningInput& input) {
TW_Ontology_Proto_SigningOutput OngTxBuilder::transfer(const Ontology::Proto::SigningInput &input) {
auto payer = Signer(PrivateKey(input.payer_private_key()));
auto owner = Signer(PrivateKey(input.owner_private_key()));
auto toAddress = Address(input.to_address());
auto transaction = Ong().transfer(owner, toAddress, input.amount(), payer, input.gas_price(),
input.gas_limit());
input.gas_limit(), input.nonce());
auto encoded = transaction.serialize();
auto protoOutput = Proto::SigningOutput();
protoOutput.set_encoded(encoded.data(), encoded.size());
auto serialized = protoOutput.SerializeAsString();
return TWDataCreateWithBytes(reinterpret_cast<const uint8_t*>(serialized.data()),
return TWDataCreateWithBytes(reinterpret_cast<const uint8_t *>(serialized.data()),
serialized.size());
}

TW_Ontology_Proto_SigningOutput OngTxBuilder::withdraw(const Ontology::Proto::SigningInput& input) {
TW_Ontology_Proto_SigningOutput OngTxBuilder::withdraw(const Ontology::Proto::SigningInput &input) {
auto payer = Signer(PrivateKey(input.payer_private_key()));
auto owner = Signer(PrivateKey(input.owner_private_key()));
auto toAddress = Address(input.to_address());
auto transaction = Ong().withdraw(owner, toAddress, input.amount(), payer, input.gas_price(),
input.gas_limit());
input.gas_limit(), input.nonce());
auto encoded = transaction.serialize();
auto protoOutput = Proto::SigningOutput();
protoOutput.set_encoded(encoded.data(), encoded.size());
auto serialized = protoOutput.SerializeAsString();
return TWDataCreateWithBytes(reinterpret_cast<const uint8_t*>(serialized.data()),
return TWDataCreateWithBytes(reinterpret_cast<const uint8_t *>(serialized.data()),
serialized.size());
}

TW_Ontology_Proto_SigningOutput OngTxBuilder::build(const Ontology::Proto::SigningInput& input) {
TW_Ontology_Proto_SigningOutput OngTxBuilder::build(const Ontology::Proto::SigningInput &input) {
auto method = std::string(input.method().begin(), input.method().end());
if (method == "transfer") {
return OngTxBuilder::transfer(input);
Expand All @@ -68,9 +68,9 @@ TW_Ontology_Proto_SigningOutput OngTxBuilder::build(const Ontology::Proto::Signi
return OngTxBuilder::balanceOf(input);
}
if (method == "decimals") {
return OngTxBuilder::decimals();
return OngTxBuilder::decimals(input);
}
if (method == "withdraw"){
if (method == "withdraw") {
return OngTxBuilder::withdraw(input);
}
std::vector<uint8_t> nullData;
Expand Down
2 changes: 1 addition & 1 deletion src/Ontology/OngTxBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace TW::Ontology {
class OngTxBuilder {

public:
static TW_Ontology_Proto_SigningOutput decimals();
static TW_Ontology_Proto_SigningOutput decimals(const Ontology::Proto::SigningInput &input);

static TW_Ontology_Proto_SigningOutput balanceOf(const Ontology::Proto::SigningInput& input);

Expand Down
19 changes: 9 additions & 10 deletions src/Ontology/Ont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,37 @@
#include "Data.h"
#include "ParamsBuilder.h"

#include <TrezorCrypto/rand.h>

#include <unordered_map>

using namespace TW;
using namespace TW::Ontology;

Transaction Ont::decimals() {
Transaction Ont::decimals(uint32_t nonce) {
auto builder = ParamsBuilder();
auto invokeCode =
ParamsBuilder::buildNativeInvokeCode(contractAddress(), version, "decimals", Data());
auto tx = Transaction((uint8_t)0, txType, random32(), (uint64_t)0, (uint64_t)0,
(std::string) "", invokeCode);
auto tx = Transaction((uint8_t)0, txType, nonce, (uint64_t)0, (uint64_t)0, (std::string) "",
invokeCode);
return tx;
}

Transaction Ont::balanceOf(const Address& address) {
Transaction Ont::balanceOf(const Address &address, uint32_t nonce) {
auto builder = ParamsBuilder();
auto invokeCode =
ParamsBuilder::buildNativeInvokeCode(contractAddress(), version, "balanceOf", address.data);
auto tx = Transaction((uint8_t)0, txType, random32(), (uint64_t)0, (uint64_t)0,
auto tx = Transaction((uint8_t)0, txType, nonce, (uint64_t)0, (uint64_t)0,
(std::string) "", invokeCode);
return tx;
}

Transaction Ont::transfer(const Signer& from, const Address& to, uint64_t amount,
const Signer& payer, uint64_t gasPrice, uint64_t gasLimit) {
Transaction Ont::transfer(const Signer &from, const Address &to, uint64_t amount,
const Signer &payer, uint64_t gasPrice, uint64_t gasLimit,
uint32_t nonce) {
std::list<boost::any> transferParam{from.getAddress().data, to.data, amount};
std::vector<boost::any> args{transferParam};
auto invokeCode =
ParamsBuilder::buildNativeInvokeCode(contractAddress(), 0x00, "transfer", args);
auto tx = Transaction(version, txType, random32(), gasPrice, gasLimit,
auto tx = Transaction(version, txType, nonce, gasPrice, gasLimit,
payer.getAddress().string(), invokeCode);
from.sign(tx);
payer.addSign(tx);
Expand Down
9 changes: 5 additions & 4 deletions src/Ontology/Ont.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ class Ont : public Asset {
public:
Data contractAddress() override { return ontContract; }

Transaction decimals() override;
Transaction decimals(uint32_t nonce) override;

Transaction balanceOf(const Address& address) override;
Transaction balanceOf(const Address &address, uint32_t nonce) override;

Transaction transfer(const Signer& from, const Address& to, uint64_t amount,
const Signer& payer, uint64_t gasPrice, uint64_t gasLimit) override;
Transaction transfer(const Signer &from, const Address &to, uint64_t amount,
const Signer &payer, uint64_t gasPrice, uint64_t gasLimit,
uint32_t nonce) override;
};

} // namespace TW::Ontology
Loading

0 comments on commit 2d30894

Please sign in to comment.