Skip to content

Commit

Permalink
fix: resolve PR review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f committed Oct 15, 2024
1 parent 5a8e183 commit fab7d9c
Show file tree
Hide file tree
Showing 15 changed files with 355 additions and 168 deletions.
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.pactus

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

class TestPactusAddress {

init {
System.loadLibrary("TrustWalletCore")
}

@Test
fun testAddress() {
val key = PrivateKey("4e51f1f3721f644ac7a193be7f5e7b8c2abaa3467871daf4eacb5d3af080e5d6".toHexByteArray())
val pubkey = key.publicKeyEd25519
val address = AnyAddress(pubkey, CoinType.PACTUS)
val expected = AnyAddress("pc1rwzvr8rstdqypr80ag3t6hqrtnss9nwymcxy3lr", CoinType.PACTUS)

assertEquals(pubkey.data().toHex(), "0x95794161374b22c696dabb98e93f6ca9300b22f3b904921fbf560bb72145f4fa")
assertEquals(address.description(), expected.description())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

package com.trustwallet.core.app.blockchains.pactus

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.jni.PactusSigner
import wallet.core.jni.proto.Pactus

class TestPactusSigner {

init {
System.loadLibrary("TrustWalletCore")
}

@Test
fun PactusTransactionSigning() {
val privateKey =
"4e51f1f3721f644ac7a193be7f5e7b8c2abaa3467871daf4eacb5d3af080e5d6".toHexBytesInByteString()

val transfer = Pactus.TransferPayload.newBuilder()
.setSender("pc1rwzvr8rstdqypr80ag3t6hqrtnss9nwymcxy3lr")
.setReceiver("pc1r0g22ufzn8qtw0742dmfglnw73e260hep0k3yra")
.setAmount(20000)
.build()

val transaction = Pactus.TransactionMessage.newBuilder()
.setLockTime(0x00030201)
.setFee(1000)
.setMemo("test")
.setTransfer(transfer)
.build()

val signingInput = Pactus.SigningInput.newBuilder()
.setPrivateKey(privateKey)
.setTransaction(transaction)
.build()

val output: Pactus.SigningOutput = PactusSigner.sign(signingInput)

assertEquals(
"34cd4656a98f7eb996e83efdc384cefbe3a9c52dca79a99245b4eacc0b0b4311",
Numeric.toHexString(output.transactionId.toByteArray())
)

assertEquals(
"50ac25c7125271489b0cd230549257c93fb8c6265f2914a988ba7b81c1bc47fff027412dd59447867911035ff69742d171060a1f132ac38b95acc6e39ec0bd09",
Numeric.toHexString(output.signature.toByteArray())
)

assertEquals(
"000101020300e807047465737401037098338e0b6808119dfd4457ab806b9c2059b89b037a14ae24533816e7faaa6ed28fcdde8e55a7df21a09c0150ac25c7125271489b0cd230549257c93fb8c6265f2914a988ba7b81c1bc47fff027412dd59447867911035ff69742d171060a1f132ac38b95acc6e39ec0bd0995794161374b22c696dabb98e93f6ca9300b22f3b904921fbf560bb72145f4fa",
Numeric.toHexString(output.signedTransactionData.toByteArray())
)
}
}
6 changes: 3 additions & 3 deletions rust/chains/tw_pactus/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tw_keypair::ed25519;
use tw_proto::Pactus::Proto;
use tw_proto::TxCompiler::Proto as CompilerProto;

use crate::transaction::Transaction;
use crate::modules::tx_builder::TxBuilder;

pub struct PactusCompiler;

Expand All @@ -28,7 +28,7 @@ impl PactusCompiler {
_coin: &dyn CoinContext,
input: Proto::SigningInput<'_>,
) -> SigningResult<CompilerProto::PreSigningOutput<'static>> {
let trx = Transaction::from_proto(&input)?;
let trx = TxBuilder::from_proto(&input)?;
let sign_bytes = trx.sign_bytes()?;

let output = CompilerProto::PreSigningOutput {
Expand Down Expand Up @@ -67,7 +67,7 @@ impl PactusCompiler {
let public_key = ed25519::sha512::PublicKey::try_from(public_key_bytes.as_slice())?;
let signature = ed25519::Signature::try_from(signature_bytes.as_slice())?;

let mut trx = Transaction::from_proto(&input)?;
let mut trx = TxBuilder::from_proto(&input)?;
trx.set_signatory(public_key.to_owned(), signature.to_owned());

let data = trx.to_bytes()?;
Expand Down
2 changes: 1 addition & 1 deletion rust/chains/tw_pactus/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ use tw_keypair::tw::PublicKey;
use tw_proto::Pactus::Proto;
use tw_proto::TxCompiler::Proto as CompilerProto;

use crate::address::Address;
use crate::compiler::PactusCompiler;
use crate::modules::transaction_util::PactusTransactionUtil;
use crate::signer::PactusSigner;
use crate::types::Address;

pub struct PactusEntry;

Expand Down
3 changes: 1 addition & 2 deletions rust/chains/tw_pactus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
//
// Copyright © 2017 Trust Wallet.

pub mod address;
pub mod amount;
pub mod compiler;
pub mod encoder;
pub mod entry;
pub mod modules;
pub mod signer;
pub mod transaction;
pub mod types;
1 change: 1 addition & 0 deletions rust/chains/tw_pactus/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
// Copyright © 2017 Trust Wallet.

pub mod transaction_util;
pub mod tx_builder;
42 changes: 42 additions & 0 deletions rust/chains/tw_pactus/src/modules/tx_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.

use crate::transaction::payload::TransferPayload;
use crate::transaction::Transaction;
use crate::types::{Address, Amount};
use std::str::FromStr;
use tw_coin_entry::error::prelude::*;
use tw_proto::Pactus;

pub struct TxBuilder;

impl TxBuilder {
pub fn from_proto(input: &Pactus::Proto::SigningInput) -> SigningResult<Transaction> {
match &input.transaction {
None => SigningError::err(SigningErrorType::Error_invalid_params),
Some(trx) => {
let payload = match &trx.payload {
Pactus::Proto::mod_TransactionMessage::OneOfpayload::transfer(pld) => {
let sender = Address::from_str(&pld.sender)?;
let receiver = Address::from_str(&pld.receiver)?;
Box::new(TransferPayload::new(sender, receiver, Amount(pld.amount)))
},
Pactus::Proto::mod_TransactionMessage::OneOfpayload::bond(_pld) => {
return SigningError::err(SigningErrorType::Error_not_supported)
},
Pactus::Proto::mod_TransactionMessage::OneOfpayload::None => {
return SigningError::err(SigningErrorType::Error_invalid_params)
},
};

Ok(Transaction::new(
trx.lock_time,
Amount(trx.fee),
trx.memo.to_string(),
payload,
))
},
}
}
}
4 changes: 2 additions & 2 deletions rust/chains/tw_pactus/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tw_keypair::ed25519;
use tw_keypair::traits::KeyPairTrait;
use tw_proto::Pactus::Proto;

use crate::transaction::Transaction;
use crate::modules::tx_builder::TxBuilder;

pub struct PactusSigner;

Expand All @@ -26,7 +26,7 @@ impl PactusSigner {
_coin: &dyn CoinContext,
input: Proto::SigningInput<'_>,
) -> SigningResult<Proto::SigningOutput<'static>> {
let mut trx = Transaction::from_proto(&input)?;
let mut trx = TxBuilder::from_proto(&input)?;
let key_pair = ed25519::sha512::KeyPair::try_from(input.private_key.as_ref())?;
let signature = trx.sign(key_pair.private())?;

Expand Down
Loading

0 comments on commit fab7d9c

Please sign in to comment.