forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Hedera support (trustwallet#2680)
- Loading branch information
Showing
41 changed files
with
3,050 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...app/src/androidTest/java/com/trustwallet/core/app/blockchains/hedera/TestHederaAddress.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright © 2017-2022 Trust Wallet. | ||
// | ||
// This file is part of Trust. The full Trust copyright notice, including | ||
// terms governing use, modification, and redistribution, is contained in the | ||
// file LICENSE at the root of the source code distribution tree. | ||
|
||
package com.trustwallet.core.app.blockchains.hedera | ||
|
||
import com.trustwallet.core.app.utils.toHex | ||
import com.trustwallet.core.app.utils.toHexByteArray | ||
import org.junit.Assert | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
import wallet.core.jni.* | ||
|
||
class TestHederaAddress { | ||
|
||
init { | ||
System.loadLibrary("TrustWalletCore") | ||
} | ||
|
||
@Test | ||
fun testAddress() { | ||
val any = AnyAddress("0.0.1377988", CoinType.HEDERA) | ||
assertEquals(any.coin(), CoinType.HEDERA) | ||
assertEquals(any.description(), "0.0.1377988") | ||
|
||
Assert.assertFalse( | ||
AnyAddress.isValid( | ||
"0.0.a", | ||
CoinType.HEDERA | ||
) | ||
) | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
.../app/src/androidTest/java/com/trustwallet/core/app/blockchains/hedera/TestHederaSigner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright © 2017-2022 Trust Wallet. | ||
// | ||
// This file is part of Trust. The full Trust copyright notice, including | ||
// terms governing use, modification, and redistribution, is contained in the | ||
// file LICENSE at the root of the source code distribution tree. | ||
|
||
package com.trustwallet.core.app.blockchains.hedera | ||
|
||
import com.google.protobuf.ByteString | ||
import com.trustwallet.core.app.utils.Numeric | ||
import com.trustwallet.core.app.utils.toHexByteArray | ||
import com.trustwallet.core.app.utils.toHexBytes | ||
import com.trustwallet.core.app.utils.toHexBytesInByteString | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
import wallet.core.java.AnySigner | ||
import wallet.core.jni.* | ||
import wallet.core.jni.proto.Hedera | ||
|
||
class TestHederaSigner { | ||
|
||
init { | ||
System.loadLibrary("TrustWalletCore") | ||
} | ||
|
||
@Test | ||
fun HederaTransactionSimpleTransferSigning() { | ||
// Successfully broadcasted: https://hashscan.io/testnet/transaction/0.0.48694347-1667222879-749068449?t=1667222891.440398729&p=1 | ||
val key = | ||
"e87a5584c0173263e138db689fdb2a7389025aaae7cb1a18a1017d76012130e8".toHexBytesInByteString() | ||
|
||
val transfer = Hedera.TransferMessage | ||
.newBuilder() | ||
.setAmount(100000000) | ||
.setFrom("0.0.48694347") | ||
.setTo("0.0.48462050") | ||
.build() | ||
|
||
val timestamp = Hedera.Timestamp | ||
.newBuilder() | ||
.setSeconds(1667222879) | ||
.setNanos(749068449) | ||
.build() | ||
|
||
val transactionID = Hedera.TransactionID | ||
.newBuilder() | ||
.setTransactionValidStart(timestamp) | ||
.setAccountID("0.0.48694347") | ||
.build() | ||
|
||
val body = Hedera.TransactionBody | ||
.newBuilder() | ||
.setMemo("") | ||
.setTransfer(transfer) | ||
.setTransactionID(transactionID) | ||
.setNodeAccountID("0.0.9") | ||
.setTransactionFee(100000000) | ||
.setTransactionValidDuration(120) | ||
.build() | ||
|
||
val signingInput = Hedera.SigningInput | ||
.newBuilder() | ||
.setPrivateKey(key) | ||
.setBody(body).build() | ||
|
||
val result = AnySigner.sign(signingInput, CoinType.HEDERA, Hedera.SigningOutput.parser()) | ||
assertEquals( | ||
Numeric.cleanHexPrefix(Numeric.toHexString(result.encoded.toByteArray())), | ||
"0a440a150a0c08df9aff9a0610a1c197e502120518cb889c17120218091880c2d72f22020878721e0a1c0a0c0a0518e2f18d17108084af5f0a0c0a0518cb889c1710ff83af5f12660a640a205d3a70d08b2beafb72c7a68986b3ff819a306078b8c359d739e4966e82e6d40e1a40612589c3b15f1e3ed6084b5a3a5b1b81751578cac8d6c922f31731b3982a5bac80a22558b2197276f5bae49b62503a4d39448ceddbc5ef3ba9bee4c0f302f70c" | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
#pragma once | ||
|
||
#include "Data.h" | ||
#include "../PublicKey.h" | ||
#include "PublicKey.h" | ||
|
||
#include <string> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright © 2017-2022 Trust Wallet. | ||
// | ||
// This file is part of Trust. The full Trust copyright notice, including | ||
// terms governing use, modification, and redistribution, is contained in the | ||
// file LICENSE at the root of the source code distribution tree. | ||
|
||
#include "Address.h" | ||
#include "HexCoding.h" | ||
#include "DER.h" | ||
#include "algorithm/string.hpp" | ||
|
||
#include <charconv> | ||
#include <regex> | ||
|
||
namespace TW::Hedera::internal { | ||
static const std::regex gEntityIDRegex{"(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)(?:-([a-z]{5}))?$"}; | ||
} | ||
|
||
namespace TW::Hedera { | ||
|
||
|
||
Alias::Alias(std::optional<PublicKey> alias) noexcept : mPubKey(std::move(alias)) { | ||
|
||
} | ||
|
||
std::string Alias::string() const noexcept { | ||
std::string pubkeyBytes = ""; | ||
if (mPubKey.has_value()) { | ||
pubkeyBytes = hex(mPubKey.value().bytes); | ||
} | ||
return gHederaDerPrefixPublic + pubkeyBytes; | ||
} | ||
|
||
bool Address::isValid(const std::string& string) { | ||
using namespace internal; | ||
std::smatch match; | ||
auto isValid = std::regex_match(string, match, gEntityIDRegex); | ||
if (!isValid) { | ||
auto parts = TW::ssplit(string, '.'); | ||
if (parts.size() != 3) { | ||
return false; | ||
} | ||
auto isNumberFunctor = [](std::string_view input) { | ||
return input.find_first_not_of("0123456789") == std::string::npos; | ||
}; | ||
if (!isNumberFunctor(parts[0]) || !isNumberFunctor(parts[1])) { | ||
return false; | ||
} | ||
isValid = hasDerPrefix(parts[2]); | ||
} | ||
return isValid; | ||
} | ||
|
||
Address::Address(const std::string& string) { | ||
if (!isValid(string)) { | ||
throw std::invalid_argument("Invalid address string"); | ||
} | ||
|
||
auto toInt = [](std::string_view s) -> std::optional<std::size_t> { | ||
if (std::size_t value = 0; std::from_chars(s.begin(), s.end(), value).ec == std::errc{}) { | ||
return value; | ||
} else { | ||
return std::nullopt; | ||
} | ||
}; | ||
|
||
// When creating an Address by string - we assume to only sent to 0.0.1 format, alias is internal. | ||
auto parts = TW::ssplit(string, '.'); | ||
mShard = *toInt(parts[0]); | ||
mRealm = *toInt(parts[1]); | ||
mNum = *toInt(parts[2]); | ||
} | ||
|
||
Address::Address(const PublicKey& publicKey) | ||
: Address(0, 0, 0, publicKey) { | ||
} | ||
|
||
std::string Address::string() const { | ||
std::string out = std::to_string(mShard) + "." + std::to_string(mRealm) + "."; | ||
if (mAlias.mPubKey.has_value()) { | ||
return out + mAlias.string(); | ||
} | ||
return out + std::to_string(mNum); | ||
} | ||
|
||
Address::Address(std::size_t shard, std::size_t realm, std::size_t num, std::optional<PublicKey> alias) | ||
: mShard(shard), mRealm(realm), mNum(num), mAlias(std::move(alias)) { | ||
} | ||
|
||
} // namespace TW::Hedera |
Oops, something went wrong.