-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Sui]: Add SUI Implementation (#2883)
- Loading branch information
Showing
38 changed files
with
858 additions
and
162 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
35 changes: 35 additions & 0 deletions
35
android/app/src/androidTest/java/com/trustwallet/core/app/blockchains/sui/TestSuiAddress.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.sui | ||
|
||
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 TestSuiAddress { | ||
|
||
init { | ||
System.loadLibrary("TrustWalletCore") | ||
} | ||
|
||
@Test | ||
fun testAddress() { | ||
val any = AnyAddress("0x061ce2b2100a71bb7aa0da98998887ad82597948", CoinType.SUI) | ||
assertEquals(any.coin(), CoinType.SUI) | ||
assertEquals(any.description(), "0x061ce2b2100a71bb7aa0da98998887ad82597948") | ||
|
||
Assert.assertFalse( | ||
AnyAddress.isValid( | ||
"0xMQqpqMQgCBuiPkoXfgZZsJvuzCeI1zc00z6vHJj4", | ||
CoinType.SUI | ||
) | ||
) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
android/app/src/androidTest/java/com/trustwallet/core/app/blockchains/sui/TestSuiSigner.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,42 @@ | ||
// 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.sui | ||
|
||
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.CoinType | ||
import wallet.core.jni.proto.Sui | ||
|
||
class TestSuiSigner { | ||
|
||
init { | ||
System.loadLibrary("TrustWalletCore") | ||
} | ||
|
||
@Test | ||
fun SuiTransactionSigning() { | ||
// Successfully broadcasted https://explorer.sui.io/transaction/rxLgxcAqgMg8gphp6eCsSGQcdZnwFYx2SRdwEhnAUC4 | ||
val txBytes = """ | ||
AAUCLiNiMy/EzosKCk5EZr5QQZmMVLnvAAAAAAAAACDqj/OT+1+qyLZKV4YLw8kpK3/bTZKspTUmh1pBuUfHPLb0crwkV1LQcBARaxER8XhTNJmK7wAAAAAAAAAgaQEguOdXa+m16IM536nsveakQ4u/GYJAc1fpYGGKEvgBQUP35yxF+cEL5qm153kw18dVeuYB6AMAAAAAAAAttQCskZzd41GsNuNxHYMsbbl2aS4jYjMvxM6LCgpORGa+UEGZjFS57wAAAAAAAAAg6o/zk/tfqsi2SleGC8PJKSt/202SrKU1JodaQblHxzwBAAAAAAAAAOgDAAAAAAAA | ||
""".trimIndent() | ||
val key = | ||
"3823dce5288ab55dd1c00d97e91933c613417fdb282a0b8b01a7f5f5a533b266".toHexBytesInByteString() | ||
val signDirect = Sui.SignDirect.newBuilder().setUnsignedTxMsg(txBytes).build() | ||
val signingInput = | ||
Sui.SigningInput.newBuilder().setSignDirectMessage(signDirect).setPrivateKey(key).build() | ||
val result = AnySigner.sign(signingInput, CoinType.SUI, Sui.SigningOutput.parser()) | ||
val expectedSignature = "AIYRmHDpQesfAx3iWBCMwInf3MZ56ZQGnPWNtECFjcSq0ssAgjRW6GLnFCX24tfDNjSm9gjYgoLmn1No15iFJAtqfN7sFqdcD/Z4e8I1YQlGkDMCK7EOgmydRDqfH8C9jg==" | ||
assertEquals(result.unsignedTx, txBytes); | ||
assertEquals(result.signature, expectedSignature) | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,3 @@ | ||
# Whether to add a `#pragma once` guard | ||
# default: doesn't emit a `#pragma once` | ||
pragma_once = true |
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,115 @@ | ||
// Copyright © 2017-2023 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. | ||
|
||
use base64::{Engine as _, engine::{general_purpose}}; | ||
use std::ffi::{CStr, CString}; | ||
use std::os::raw::c_char; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn encode_base64(data: *const u8, len: usize, is_url: bool) -> *mut c_char { | ||
let data = unsafe { std::slice::from_raw_parts(data, len) }; | ||
let encoded = if is_url { | ||
general_purpose::URL_SAFE.encode(data) | ||
} else { | ||
general_purpose::STANDARD.encode(data) | ||
}; | ||
CString::new(encoded).unwrap().into_raw() | ||
} | ||
|
||
#[repr(C)] | ||
pub struct CByteArray { | ||
data: *mut u8, | ||
size: usize, | ||
} | ||
|
||
#[no_mangle] | ||
pub extern "C" fn decode_base64(data: *const c_char, is_url: bool) -> CByteArray { | ||
if data.is_null() { | ||
return CByteArray { data: std::ptr::null_mut(), size: 0 }; | ||
} | ||
let c_str = unsafe { CStr::from_ptr(data) }; | ||
let str_slice = c_str.to_str().unwrap(); | ||
let decoded = if is_url { | ||
general_purpose::URL_SAFE | ||
.decode(str_slice) | ||
} else { | ||
general_purpose::STANDARD | ||
.decode(str_slice) | ||
}; | ||
let decoded = match decoded { | ||
Ok(decoded) => decoded, | ||
Err(_) => return CByteArray { data: std::ptr::null_mut(), size: 0 } | ||
}; | ||
let size = decoded.len(); | ||
let mut decoded_vec = decoded.to_vec(); | ||
let ptr = decoded_vec.as_mut_ptr(); | ||
std::mem::forget(decoded_vec); | ||
CByteArray { data: ptr, size } | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::ffi::CString; | ||
use crate::encoding::{decode_base64, encode_base64}; | ||
|
||
#[test] | ||
fn test_encode_base64_ffi() { | ||
let data = b"hello world"; | ||
let encoded = unsafe { | ||
std::ffi::CStr::from_ptr(encode_base64(data.as_ptr(), data.len(), false)) | ||
}; | ||
let expected = "aGVsbG8gd29ybGQ="; | ||
assert_eq!(encoded.to_str().unwrap(), expected); | ||
} | ||
|
||
#[test] | ||
fn test_encode_base64_url_ffi() { | ||
let data = b"+'?ab"; | ||
let encoded = unsafe { | ||
std::ffi::CStr::from_ptr(encode_base64(data.as_ptr(), data.len(), true)) | ||
}; | ||
let expected = "Kyc_YWI="; | ||
assert_eq!(encoded.to_str().unwrap(), expected); | ||
} | ||
|
||
#[test] | ||
fn test_decode_base64_url() { | ||
let encoded = "Kyc_YWI="; | ||
let expected = b"+'?ab"; | ||
|
||
let encoded_c_str = CString::new(encoded).unwrap(); | ||
let encoded_ptr = encoded_c_str.as_ptr(); | ||
|
||
let decoded_ptr = decode_base64(encoded_ptr, true); | ||
let decoded_slice = unsafe { std::slice::from_raw_parts(decoded_ptr.data, decoded_ptr.size) }; | ||
|
||
assert_eq!(decoded_slice, expected); | ||
} | ||
|
||
#[test] | ||
fn test_decode_base64() { | ||
let encoded = "aGVsbG8gd29ybGQh"; | ||
let expected = b"hello world!"; | ||
|
||
let encoded_c_str = CString::new(encoded).unwrap(); | ||
let encoded_ptr = encoded_c_str.as_ptr(); | ||
|
||
let decoded_ptr = decode_base64(encoded_ptr, false); | ||
let decoded_slice = unsafe { std::slice::from_raw_parts(decoded_ptr.data, decoded_ptr.size) }; | ||
|
||
assert_eq!(decoded_slice, expected); | ||
} | ||
|
||
#[test] | ||
fn test_decode_base64_invalid() { | ||
let invalid_encoded = "_This_is_an_invalid_base64_"; | ||
let encoded_c_str = CString::new(invalid_encoded).unwrap(); | ||
let encoded_ptr = encoded_c_str.as_ptr(); | ||
let decoded_ptr = decode_base64(encoded_ptr, false); | ||
assert_eq!(decoded_ptr.data.is_null(), true); | ||
} | ||
} |
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,3 +7,4 @@ | |
pub mod move_parser; | ||
pub mod memory; | ||
pub mod starknet; | ||
pub mod encoding; |
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
Oops, something went wrong.