Skip to content

Commit

Permalink
Merge branch 'master' into s/bitcoin-v2-compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan authored Aug 9, 2024
2 parents f89b891 + a05c01a commit e434b18
Show file tree
Hide file tree
Showing 192 changed files with 7,118 additions and 1,648 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ class TestCoinType {
fun testDerivationPath() {
var res = CoinType.createFromValue(CoinType.BITCOIN.value()).derivationPath().toString()
assertEquals(res, "m/84'/0'/0'/0/0")
res = CoinType.createFromValue(CoinType.BITCOIN.value()).derivationPathWithDerivation(Derivation.LEGACY).toString()
res = CoinType.createFromValue(CoinType.BITCOIN.value()).derivationPathWithDerivation(Derivation.BITCOINLEGACY).toString()
assertEquals(res, "m/44'/0'/0'/0/0")
res = CoinType.createFromValue(CoinType.SOLANA.value()).derivationPathWithDerivation(Derivation.SOLANA).toString()
res = CoinType.createFromValue(CoinType.SOLANA.value()).derivationPathWithDerivation(Derivation.SOLANASOLANA).toString()
assertEquals(res, "m/44'/501'/0'/0'")
}

@Test
fun testDeriveAddressFromPublicKeyAndDerivation() {
val publicKey = PublicKey("0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798".toHexByteArray(), PublicKeyType.SECP256K1)

val address = CoinType.BITCOIN.deriveAddressFromPublicKeyAndDerivation(publicKey, Derivation.SEGWIT)
val address = CoinType.BITCOIN.deriveAddressFromPublicKeyAndDerivation(publicKey, Derivation.BITCOINSEGWIT)
assertEquals(address, "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class TestTheOpenNetworkAddress {
fun testGenerateJettonAddress() {
val mainAddress = "UQBjKqthWBE6GEcqb_epTRFrQ1niS6Z1Z1MHMwR-mnAYRoYr"
val mainAddressBoc = TONAddressConverter.toBoc(mainAddress)
assertEquals(mainAddressBoc, "te6ccgICAAEAAQAAACQAAABDgAxlVWwrAidDCOVN/vUpoi1oazxJdM6s6mDmYI/TTgMI0A==")
assertEquals(mainAddressBoc, "te6cckEBAQEAJAAAQ4AMZVVsKwInQwjlTf71KaItaGs8SXTOrOpg5mCP004DCNAptHQU")

// curl --location 'https://toncenter.com/api/v2/runGetMethod' --header 'Content-Type: application/json' --data \
// '{"address":"EQAvlWFDxGF2lXm67y4yzC17wYKD9A0guwPkMs1gOsM__NOT","method":"get_wallet_address","method":"get_wallet_address","stack":[["tvm.Slice","te6ccgICAAEAAQAAACQAAABDgAxlVWwrAidDCOVN/vUpoi1oazxJdM6s6mDmYI/TTgMI0A=="]]}'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ class TestTheOpenNetworkSigner {
.setWalletVersion(TheOpenNetwork.WalletVersion.WALLET_V4_R2)
.setDest("EQBm--PFwDv1yCeS-QTJ-L8oiUpqo9IT1BwgVptlSq3ts90Q")
.setAmount(10)
.setSequenceNumber(6)
.setMode(TheOpenNetwork.SendMode.PAY_FEES_SEPARATELY_VALUE or TheOpenNetwork.SendMode.IGNORE_ACTION_PHASE_ERRORS_VALUE)
.setExpireAt(1671132440)
.setBounceable(true)
.build()

val input = TheOpenNetwork.SigningInput.newBuilder()
.setTransfer(transfer)
.setPrivateKey(ByteString.copyFrom(privateKey.data()))
.addMessages(transfer)
.setSequenceNumber(6)
.setExpireAt(1671132440)
.build()

val output = AnySigner.sign(input, CoinType.TON, SigningOutput.parser())

// tx: https://tonscan.org/tx/3Z4tHpXNLyprecgu5aTQHWtY7dpHXEoo11MAX61Xyg0=
val expectedString = "te6ccgICAAQAAQAAALAAAAFFiAGwt/q8k4SrjbFbQCjJZfQr64ExRxcUMsWqaQODqTUijgwAAQGcEUPkil2aZ4s8KKparSep/OKHMC8vuXafFbW2HGp/9AcTRv0J5T4dwyW1G0JpHw+g5Ov6QI3Xo0O9RFr3KidICimpoxdjm3UYAAAABgADAAIBYmIAM33x4uAd+uQTyXyCZPxflESlNVHpCeoOECtNsqVW9tmIUAAAAAAAAAAAAAAAAAEAAwAA"
val expectedString = "te6cckEBBAEArQABRYgBsLf6vJOEq42xW0AoyWX0K+uBMUcXFDLFqmkDg6k1Io4MAQGcEUPkil2aZ4s8KKparSep/OKHMC8vuXafFbW2HGp/9AcTRv0J5T4dwyW1G0JpHw+g5Ov6QI3Xo0O9RFr3KidICimpoxdjm3UYAAAABgADAgFiYgAzffHi4B365BPJfIJk/F+URKU1UekJ6g4QK02ypVb22YhQAAAAAAAAAAAAAAAAAQMAAA08Nzs="

assertEquals(output.encoded, expectedString)
}
Expand All @@ -51,33 +51,77 @@ class TestTheOpenNetworkSigner {
fun TheOpenNetworkJettonTransferSigning() {
val privateKey = PrivateKey("c054900a527538c1b4325688a421c0469b171c29f23a62da216e90b0df2412ee".toHexByteArray())

val transferData = TheOpenNetwork.Transfer.newBuilder()
val jettonTransfer = TheOpenNetwork.JettonTransfer.newBuilder()
.setJettonAmount(500 * 1000 * 1000)
.setToOwner("EQAFwMs5ha8OgZ9M4hQr80z9NkE7rGxUpE1hCFndiY6JnDx8")
.setResponseAddress("EQBaKIMq5Am2p_rfR1IFTwsNWHxBkOpLTmwUain5Fj4llTXk")
.setForwardAmount(1)
.build()

val transfer = TheOpenNetwork.Transfer.newBuilder()
.setWalletVersion(TheOpenNetwork.WalletVersion.WALLET_V4_R2)
.setDest("EQBiaD8PO1NwfbxSkwbcNT9rXDjqhiIvXWymNO-edV0H5lja")
.setAmount(100 * 1000 * 1000)
.setSequenceNumber(1)
.setMode(TheOpenNetwork.SendMode.PAY_FEES_SEPARATELY_VALUE or TheOpenNetwork.SendMode.IGNORE_ACTION_PHASE_ERRORS_VALUE)
.setExpireAt(1787693046)
.setComment("test comment")
.setBounceable(true)
.setJettonTransfer(jettonTransfer)

val jettonTransfer = TheOpenNetwork.JettonTransfer.newBuilder()
.setTransfer(transferData)
.setJettonAmount(500 * 1000 * 1000)
.setToOwner("EQAFwMs5ha8OgZ9M4hQr80z9NkE7rGxUpE1hCFndiY6JnDx8")
.setResponseAddress("EQBaKIMq5Am2p_rfR1IFTwsNWHxBkOpLTmwUain5Fj4llTXk")
.setForwardAmount(1)
val input = TheOpenNetwork.SigningInput.newBuilder()
.setPrivateKey(ByteString.copyFrom(privateKey.data()))
.addMessages(transfer)
.setSequenceNumber(1)
.setExpireAt(1787693046)
.build()

val output = AnySigner.sign(input, CoinType.TON, SigningOutput.parser())

// tx: https://testnet.tonscan.org/tx/Er_oT5R3QK7D-qVPBKUGkJAOOq6ayVls-mgEphpI9Ck=
val expectedString = "te6cckECBAEAARUAAUWIALRRBlXIE21P9b6OpAqeFhqw+IMh1Jac2CjUU/IsfEsqDAEBnGiFlaLItV573gJqBvctP5j3jVKlLuxmO+pnW0QGlXjXgzjw5YeTNwRG9upJHOl6GA3pFetKNojqGzfkxku+owUpqaMXao4H9gAAAAEAAwIBaGIAMTQfh52puD7eKUmDbhqfta4cdUMRF662Uxp3zzqug/MgL68IAAAAAAAAAAAAAAAAAAEDAMoPin6lAAAAAAAAAABB3NZQCAALgZZzC14dAz6ZxChX5pn6bIJ3WNipSJrCELO7Ex0TOQAWiiDKuQJtqf630dSBU8LDVh8QZDqS05sFGop+RY+JZUICAAAAAHRlc3QgY29tbWVudG/bd5c="

assertEquals(output.encoded, expectedString)
}

@Test
fun TheOpenNetworkTransferCustomPayload() {
val privateKey = PrivateKey("5525e673087587bc0efd7ab09920ef7d3c1bf6b854a661430244ca59ab19e9d1".toHexByteArray())

// Doge chatbot contract payload to be deployed.
// Docs: https://docs.ton.org/develop/dapps/ton-connect/transactions#smart-contract-deployment
val dogeChatbotStateInit = "te6cckEBBAEAUwACATQBAgEU/wD0pBP0vPLICwMAEAAAAZDrkbgQAGrTMAGCCGlJILmRMODQ0wMx+kAwi0ZG9nZYcCCAGMjLBVAEzxaARfoCE8tqEssfAc8WyXP7AO4ioYU="
// Doge chatbot's address after the contract is deployed.
val dogeChatbotDeployingAddress = "0:3042cd5480da232d5ac1d9cbe324e3c9eb58f167599f6b7c20c6e638aeed0335"

// The comment has nothing to do with Doge chatbot.
// It's just used to attach the following ASCII comment to the transaction:
// "This transaction deploys Doge Chatbot contract"
val commentPayload = "te6cckEBAQEANAAAZAAAAABUaGlzIHRyYW5zYWN0aW9uIGRlcGxveXMgRG9nZSBDaGF0Ym90IGNvbnRyYWN0v84vSg=="

val customPayload = TheOpenNetwork.CustomPayload.newBuilder()
.setStateInit(dogeChatbotStateInit)
.setPayload(commentPayload)
.build()

val transfer = TheOpenNetwork.Transfer.newBuilder()
.setWalletVersion(TheOpenNetwork.WalletVersion.WALLET_V4_R2)
.setDest(dogeChatbotDeployingAddress)
// 0.069 TON
.setAmount(69_000_000)
.setMode(TheOpenNetwork.SendMode.PAY_FEES_SEPARATELY_VALUE or TheOpenNetwork.SendMode.IGNORE_ACTION_PHASE_ERRORS_VALUE)
.setBounceable(false)
.setCustomPayload(customPayload)

val input = TheOpenNetwork.SigningInput.newBuilder()
.setJettonTransfer(jettonTransfer)
.setPrivateKey(ByteString.copyFrom(privateKey.data()))
.addMessages(transfer)
.setSequenceNumber(4)
.setExpireAt(1721939714)
.build()

val output = AnySigner.sign(input, CoinType.TON, SigningOutput.parser())

// tx: https://testnet.tonscan.org/tx/Er_oT5R3QK7D-qVPBKUGkJAOOq6ayVls-mgEphpI9Ck=
val expectedString = "te6ccgICAAQAAQAAARgAAAFFiAC0UQZVyBNtT/W+jqQKnhYasPiDIdSWnNgo1FPyLHxLKgwAAQGcaIWVosi1XnveAmoG9y0/mPeNUqUu7GY76mdbRAaVeNeDOPDlh5M3BEb26kkc6XoYDekV60o2iOobN+TGS76jBSmpoxdqjgf2AAAAAQADAAIBaGIAMTQfh52puD7eKUmDbhqfta4cdUMRF662Uxp3zzqug/MgL68IAAAAAAAAAAAAAAAAAAEAAwDKD4p+pQAAAAAAAAAAQdzWUAgAC4GWcwteHQM+mcQoV+aZ+myCd1jYqUiawhCzuxMdEzkAFoogyrkCban+t9HUgVPCw1YfEGQ6ktObBRqKfkWPiWVCAgAAAAB0ZXN0IGNvbW1lbnQ="
// Successfully broadcasted: https://tonviewer.com/transaction/f4b7ed2247b1adf54f33dd2fd99216fbd61beefb281542d0b330ccea9b8d0338
val expectedString = "te6cckECCAEAATcAAUWIAfq4NsPLegfou/MPhtHE9YuzV3gnI/q6jm3MRJh2PtpaDAEBnPbyCSsWrOZpEjb7ZFxz5yYi+an6M6Lnq7rI7TFWdDS76LEtGBrVVrhMGziwxuy6LCVtsMBikI7RPVQ89FCIAAYpqaMXZqK3AgAAAAQAAwICaUIAGCFmqkBtEZatYOzl8ZJx5PWseLOsz7W+EGNzHFd2gZqgIObaAAAAAAAAAAAAAAAAAAPAAwQCATQFBgBkAAAAAFRoaXMgdHJhbnNhY3Rpb24gZGVwbG95cyBEb2dlIENoYXRib3QgY29udHJhY3QBFP8A9KQT9LzyyAsHABAAAAGQ65G4EABq0zABgghpSSC5kTDg0NMDMfpAMItGRvZ2WHAggBjIywVQBM8WgEX6AhPLahLLHwHPFslz+wAa2r/S"

assertEquals(output.encoded, expectedString)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

package com.trustwallet.core.app.blockchains.theopennetwork

import com.trustwallet.core.app.utils.toHexByteArray
import org.junit.Assert.assertEquals
import org.junit.Test
import wallet.core.jni.PublicKey
import wallet.core.jni.PublicKeyType
import wallet.core.jni.TONWallet

class TestTheOpenNetworkWallet {
init {
System.loadLibrary("TrustWalletCore")
}

@Test
fun TheOpenNetworkWalletBuildV4R2StateInit() {
val publicKey = PublicKey("f229a9371fa7c2108b3d90ea22c9be705ff5d0cfeaee9cbb9366ff0171579357".toHexByteArray(), PublicKeyType.ED25519)
val baseWorkchain = 0
val defaultWalletId = 0x29a9a317
val stateInit = TONWallet.buildV4R2StateInit(publicKey, baseWorkchain, defaultWalletId)

val expected = "te6cckECFgEAAwQAAgE0AQIBFP8A9KQT9LzyyAsDAFEAAAAAKamjF/IpqTcfp8IQiz2Q6iLJvnBf9dDP6u6cu5Nm/wFxV5NXQAIBIAQFAgFIBgcE+PKDCNcYINMf0x/THwL4I7vyZO1E0NMf0x/T//QE0VFDuvKhUVG68qIF+QFUEGT5EPKj+AAkpMjLH1JAyx9SMMv/UhD0AMntVPgPAdMHIcAAn2xRkyDXSpbTB9QC+wDoMOAhwAHjACHAAuMAAcADkTDjDQOkyMsfEssfy/8ICQoLAubQAdDTAyFxsJJfBOAi10nBIJJfBOAC0x8hghBwbHVnvSKCEGRzdHK9sJJfBeAD+kAwIPpEAcjKB8v/ydDtRNCBAUDXIfQEMFyBAQj0Cm+hMbOSXwfgBdM/yCWCEHBsdWe6kjgw4w0DghBkc3RyupJfBuMNDA0CASAODwBu0gf6ANTUIvkABcjKBxXL/8nQd3SAGMjLBcsCIs8WUAX6AhTLaxLMzMlz+wDIQBSBAQj0UfKnAgBwgQEI1xj6ANM/yFQgR4EBCPRR8qeCEG5vdGVwdIAYyMsFywJQBs8WUAT6AhTLahLLH8s/yXP7AAIAbIEBCNcY+gDTPzBSJIEBCPRZ8qeCEGRzdHJwdIAYyMsFywJQBc8WUAP6AhPLassfEss/yXP7AAAK9ADJ7VQAeAH6APQEMPgnbyIwUAqhIb7y4FCCEHBsdWeDHrFwgBhQBMsFJs8WWPoCGfQAy2kXyx9SYMs/IMmAQPsABgCKUASBAQj0WTDtRNCBAUDXIMgBzxb0AMntVAFysI4jghBkc3Rygx6xcIAYUAXLBVADzxYj+gITy2rLH8s/yYBA+wCSXwPiAgEgEBEAWb0kK29qJoQICga5D6AhhHDUCAhHpJN9KZEM5pA+n/mDeBKAG3gQFImHFZ8xhAIBWBITABG4yX7UTQ1wsfgAPbKd+1E0IEBQNch9AQwAsjKB8v/ydABgQEI9ApvoTGACASAUFQAZrc52omhAIGuQ64X/wAAZrx32omhAEGuQ64WPwEXtMkg="
assertEquals(stateInit, expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ class TestAnyAddress {
fun testCreateWithPublicKeyDerivation() {
val coin = CoinType.BITCOIN
val pubkey = PublicKey(any_address_test_pubkey.toHexByteArray(), PublicKeyType.SECP256K1)
val address1 = AnyAddress(pubkey, coin, Derivation.SEGWIT)
val address1 = AnyAddress(pubkey, coin, Derivation.BITCOINSEGWIT)
assertEquals(address1.description(), any_address_test_address)

val address2 = AnyAddress(pubkey, coin, Derivation.LEGACY)
val address2 = AnyAddress(pubkey, coin, Derivation.BITCOINLEGACY)
assertEquals(address2.description(), "1JvRfEQFv5q5qy9uTSAezH7kVQf4hqnHXx")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.trustwallet.core.app.utils

import com.trustwallet.core.app.utils.toHexBytes
import com.trustwallet.core.app.utils.toHex
import org.junit.Assert.*
import org.junit.Test
import wallet.core.jni.*

class TestCryptoBox {
init {
System.loadLibrary("TrustWalletCore")
}

@Test
fun testEncryptDecryptEasy() {
val mySecret = CryptoBoxSecretKey()
val myPubkey = mySecret.publicKey

val otherSecret = CryptoBoxSecretKey()
val otherPubkey = otherSecret.publicKey

val message = "Well done is better than well said. -Benjamin Franklin"
val encrypted = CryptoBox.encryptEasy(mySecret, otherPubkey, message.toByteArray())

// Step 2. Make sure the Box can be decrypted by the other side.
val decrypted = CryptoBox.decryptEasy(otherSecret, myPubkey, encrypted)
assertEquals(decrypted.toString(Charsets.UTF_8), message)
}

@Test
fun testSecretKeyFromToBytes() {
val secretBytesHex = "0xdd87000d4805d6fbd89ae1352f5e4445648b79d5e901c92aebcb610e9be468e4"
val secretBytes = secretBytesHex.toHexByteArray()
assert(CryptoBoxSecretKey.isValid(secretBytes))
val secret = CryptoBoxSecretKey(secretBytes)
assertEquals(secret.data().toHex(), secretBytesHex)
}

@Test
fun testPublicKeyFromToBytes() {
val publicBytesHex = "0xafccabc5b28a8a1fd1cd880516f9c854ae2498d0d1b978b53a59f38e4ae55747"
val publicBytes = publicBytesHex.toHexByteArray()
assert(CryptoBoxPublicKey.isValid(publicBytes))
val pubkey = CryptoBoxPublicKey(publicBytes)
assertEquals(pubkey.data().toHex(), publicBytesHex)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ class TestHDWallet {
val coin = CoinType.BITCOIN
val wallet = HDWallet(words, password)

val key1 = wallet.getKeyDerivation(coin, Derivation.SEGWIT)
val key1 = wallet.getKeyDerivation(coin, Derivation.BITCOINSEGWIT)
assertEquals(key1.data().toHex(), "0x1901b5994f075af71397f65bd68a9fff8d3025d65f5a2c731cf90f5e259d6aac")

val key2 = wallet.getKeyDerivation(coin, Derivation.LEGACY)
val key2 = wallet.getKeyDerivation(coin, Derivation.BITCOINLEGACY)
assertEquals(key2.data().toHex(), "0x28071bf4e2b0340db41b807ed8a5514139e5d6427ff9d58dbd22b7ed187103a4")

val key3 = wallet.getKeyDerivation(coin, Derivation.TESTNET)
val key3 = wallet.getKeyDerivation(coin, Derivation.BITCOINTESTNET)
assertEquals(key3.data().toHex(), "0xca5845e1b43e3adf577b7f110b60596479425695005a594c88f9901c3afe864f")
}

Expand All @@ -137,13 +137,13 @@ class TestHDWallet {
val coin = CoinType.BITCOIN
val wallet = HDWallet(words, password)

val address1 = wallet.getAddressDerivation(coin, Derivation.SEGWIT)
val address1 = wallet.getAddressDerivation(coin, Derivation.BITCOINSEGWIT)
assertEquals(address1, "bc1qumwjg8danv2vm29lp5swdux4r60ezptzz7ce85")

val address2 = wallet.getAddressDerivation(coin, Derivation.LEGACY)
val address2 = wallet.getAddressDerivation(coin, Derivation.BITCOINLEGACY)
assertEquals(address2, "1PeUvjuxyf31aJKX6kCXuaqxhmG78ZUdL1")

val address3 = wallet.getAddressDerivation(coin, Derivation.TESTNET)
val address3 = wallet.getAddressDerivation(coin, Derivation.BITCOINTESTNET)
assertEquals(address3, "tb1qwgpxgwn33z3ke9s7q65l976pseh4edrzfmyvl0")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
// Copyright © 2017 Trust Wallet.

use tw_any_coin::test_utils::address_utils::{
test_address_get_data, test_address_invalid, test_address_normalization, test_address_valid,
test_address_derive, test_address_get_data, test_address_invalid, test_address_normalization,
test_address_valid,
};
use tw_coin_registry::coin_type::CoinType;

#[test]
fn test_{COIN_ID}_address_derive() {
test_address_derive(CoinType::{COIN_TYPE}, "PRIVATE_KEY", "EXPECTED ADDRESS");
}

#[test]
fn test_{COIN_ID}_address_normalization() {
test_address_normalization(CoinType::{COIN_TYPE}, "DENORMALIZED", "EXPECTED");
Expand Down
5 changes: 2 additions & 3 deletions codegen/bin/coins
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def self.derivation_name(deriv)
deriv['name'].downcase
end

def self.derivation_enum_name(deriv)
def self.derivation_enum_name(deriv, coin)
return "TWDerivationDefault" if deriv['name'].nil?
"TWDerivation" + camel_case(deriv['name'])
"TWDerivation" + format_name(coin['name']) + camel_case(deriv['name'])
end

def self.coin_img(coin)
Expand All @@ -53,7 +53,6 @@ coins = JSON.parse(json_string).sort_by { |x| x['coinId'] }

# used in some cases for numbering enum values
enum_count = 0
derivations = {}

erbs = [
{'template' => 'TWDerivation.h.erb', 'folder' => 'include/TrustWalletCore', 'file' => 'TWDerivation.h'},
Expand Down
2 changes: 1 addition & 1 deletion codegen/lib/templates/CoinInfoData.cpp.erb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const CoinInfo getCoinInfo(TWCoinType coin) {
TWCurve<%= format_name(coin['curve']) %>,
{
<% coin['derivation'].each do |deriv| -%>{
<%= derivation_enum_name(deriv) %>,
<%= derivation_enum_name(deriv, coin) %>,
"<%= deriv['path'] %>",
"<%= derivation_name(deriv) %>",
TWHDVersion<% if deriv['xpub'].nil? -%>None<% else -%><%= format_name(deriv['xpub']) %><% end -%>,
Expand Down
5 changes: 2 additions & 3 deletions codegen/lib/templates/TWDerivation.h.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ enum TWDerivation {
<% enum_count += 1 -%>
<% coins.each do |coin| -%>
<% coin['derivation'].each_with_index do |deriv, index| -%>
<% if !deriv['name'].nil? and !derivations.has_key?(deriv['name']) -%>
<% derivations[deriv['name']] = true -%>
<%= derivation_enum_name(deriv) %> = <% enum_count += 1 -%><%= enum_count %>,
<% if index > 0 or !deriv['name'].nil? -%>
<%= derivation_enum_name(deriv, coin) %> = <% enum_count += 1 -%><%= enum_count %>,
<% end -%>
<% end -%>
<% end -%>
Expand Down
40 changes: 40 additions & 0 deletions include/TrustWalletCore/TWCryptoBox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

#pragma once

#include "TWBase.h"
#include "TWCryptoBoxPublicKey.h"
#include "TWCryptoBoxSecretKey.h"
#include "TWData.h"
#include "TWString.h"

TW_EXTERN_C_BEGIN

/// `crypto_box` encryption algorithms.
TW_EXPORT_STRUCT
struct TWCryptoBox;

/// Encrypts message using `my_secret` and `other_pubkey`.
/// The output will have a randomly generated nonce prepended to it.
/// The output will be Overhead + 24 bytes longer than the original.
///
/// \param mySecret *non-null* pointer to my secret key.
/// \param otherPubkey *non-null* pointer to other's public key.
/// \param message *non-null* pointer to the message to be encrypted.
/// \return *nullable* pointer to the encrypted message with randomly generated nonce prepended to it.
TW_EXPORT_STATIC_METHOD
TWData* _Nonnull TWCryptoBoxEncryptEasy(struct TWCryptoBoxSecretKey* _Nonnull mySecret, struct TWCryptoBoxPublicKey* _Nonnull otherPubkey, TWData* _Nonnull message);

/// Decrypts box produced by `TWCryptoBoxEncryptEasy`.
/// We assume a 24-byte nonce is prepended to the encrypted text in box.
///
/// \param mySecret *non-null* pointer to my secret key.
/// \param otherPubkey *non-null* pointer to other's public key.
/// \param encrypted *non-null* pointer to the encrypted message with nonce prepended to it.
/// \return *nullable* pointer to the decrypted message.
TW_EXPORT_STATIC_METHOD
TWData* _Nullable TWCryptoBoxDecryptEasy(struct TWCryptoBoxSecretKey* _Nonnull mySecret, struct TWCryptoBoxPublicKey* _Nonnull otherPubkey, TWData* _Nonnull encrypted);

TW_EXTERN_C_END
Loading

0 comments on commit e434b18

Please sign in to comment.