Skip to content

Commit

Permalink
update Java and Swift tests
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f committed Oct 17, 2024
1 parent 008bc2f commit b6d6dbe
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
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.PrivateKey
import wallet.core.java.AnySigner
import wallet.core.jni.CoinType
import wallet.core.jni.CoinType.PACTUS
import wallet.core.jni.proto.Pactus
import wallet.core.jni.proto.Pactus.SigningOutput
import com.trustwallet.core.app.utils.Numeric
import org.junit.Assert.assertArrayEquals

class TestPactusSigner {

Expand All @@ -21,42 +24,36 @@ class TestPactusSigner {
}

@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()

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

val output: Pactus.SigningOutput = PactusSigner.sign(signingInput)
signingInput.apply {
privateKey = ByteString.copyFrom(PrivateKey("4e51f1f3721f644ac7a193be7f5e7b8c2abaa3467871daf4eacb5d3af080e5d6".toHexByteArray()).data())
transaction = Pactus.TransactionMessage.newBuilder().apply {
lockTime = 0x00030201
fee = 1000
memo = "test"
transfer = Pactus.TransferPayload.newBuilder().apply {
sender = "pc1rwzvr8rstdqypr80ag3t6hqrtnss9nwymcxy3lr"
receiver = "pc1r0g22ufzn8qtw0742dmfglnw73e260hep0k3yra"
amount = 20000
}.build()
}.build()
}

val output = AnySigner.sign(signingInput.build(), PACTUS, SigningOutput.parser())

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

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

assertEquals(
"000101020300e807047465737401037098338e0b6808119dfd4457ab806b9c2059b89b037a14ae24533816e7faaa6ed28fcdde8e55a7df21a09c0150ac25c7125271489b0cd230549257c93fb8c6265f2914a988ba7b81c1bc47fff027412dd59447867911035ff69742d171060a1f132ac38b95acc6e39ec0bd0995794161374b22c696dabb98e93f6ca9300b22f3b904921fbf560bb72145f4fa",
"0x000101020300e807047465737401037098338e0b6808119dfd4457ab806b9c2059b89b037a14ae24533816e7faaa6ed28fcdde8e55a7df21a09c0150ac25c7125271489b0cd230549257c93fb8c6265f2914a988ba7b81c1bc47fff027412dd59447867911035ff69742d171060a1f132ac38b95acc6e39ec0bd0995794161374b22c696dabb98e93f6ca9300b22f3b904921fbf560bb72145f4fa",
Numeric.toHexString(output.signedTransactionData.toByteArray())
)
}
Expand Down
4 changes: 2 additions & 2 deletions rust/chains/tw_pactus/src/encoder/var_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use byteorder::ReadBytesExt;
use super::{error::Error, Decodable};
use crate::encoder::Encodable;

/// A type of variable-length integer commonly used in the Bitcoin P2P protocol and Bitcoin serialized data structures.
/// A type of variable-length integer used in the Pactus blockchain to serialize a variable-length integer.
#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct VarInt(u64);

Expand Down Expand Up @@ -105,7 +105,7 @@ mod tests {
VarInt::from(0xffff_usize).encode(&mut w).unwrap();
VarInt::from(0x01_0000_usize).encode(&mut w).unwrap();
VarInt::from(0xffff_ffff_usize).encode(&mut w).unwrap();
VarInt::from(0x01_0000_0000_usize).encode(&mut w).unwrap();
VarInt(0x01_0000_0000_u64).encode(&mut w).unwrap();

let expected = vec![
0x00, // 0x00
Expand Down
2 changes: 1 addition & 1 deletion rust/frameworks/tw_utxo/src/encode/compact_integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod tests {
.append(&CompactInteger::from(0xffff_usize))
.append(&CompactInteger::from(0x01_0000_usize))
.append(&CompactInteger::from(0xffff_ffff_usize))
.append(&CompactInteger::from(0x01_0000_0000_usize));
.append(&CompactInteger(0x01_0000_0000_u64));

let expected = vec![
0x00, // 0x00
Expand Down
32 changes: 15 additions & 17 deletions swift/Tests/Blockchains/PactusTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//
// Copyright © 2017 Trust Wallet.

import WalletCore
import XCTest
import WalletCore

class PactusTests: XCTestCase {

Expand All @@ -18,29 +18,27 @@ class PactusTests: XCTestCase {
}

func testSign() {
let privateKeyData = Data(hexString: "5d996aa76b3212142792d9130796cd2e11e3c445a93118c08414df4f66bc60ec")!
let transfer = PactusTransferPayload.with {
$0.sender = "pc1rwzvr8rstdqypr80ag3t6hqrtnss9nwymcxy3lr"
$0.receiver = "pc1r0g22ufzn8qtw0742dmfglnw73e260hep0k3yra"
$0.amount = 20000
}
let transaction = PactusTransactionMessage.with {
$0.lockTime(0x00030201)
$0.fee(1000)
$0.memo("test")
$0.transfer(transfer)
}
let input = PactusSigningInput.with {
$0.privateKey = privateKeyData
$0.transaction = transfer
$0.privateKey = Data(hexString: "5d996aa76b3212142792d9130796cd2e11e3c445a93118c08414df4f66bc60ec")!
$0.transaction = PactusTransactionMessage.with {
$0.lockTime = 0x00030201
$0.fee = 1000
$0.memo = "test"
$0.transfer = PactusTransferPayload.with {
$0.sender = "pc1rwzvr8rstdqypr80ag3t6hqrtnss9nwymcxy3lr"
$0.receiver = "pc1r0g22ufzn8qtw0742dmfglnw73e260hep0k3yra"
$0.amount = 20000
}
}
}

let output: PactusSigningOutput = AnySigner.sign(input: input, coin: .Pactus)
let output: PactusSigningOutput = AnySigner.sign(input: input, coin: .pactus)

let expectedTransactionID = "34cd4656a98f7eb996e83efdc384cefbe3a9c52dca79a99245b4eacc0b0b4311"
let expectedSignature = "50ac25c7125271489b0cd230549257c93fb8c6265f2914a988ba7b81c1bc47fff027412dd59447867911035ff69742d171060a1f132ac38b95acc6e39ec0bd09"
let expectedSignedData = "000101020300e807047465737401037098338e0b6808119dfd4457ab806b9c2059b89b037a14ae24533816e7faaa6ed28fcdde8e55a7df21a09c0150ac25c7125271489b0cd230549257c93fb8c6265f2914a988ba7b81c1bc47fff027412dd59447867911035ff69742d171060a1f132ac38b95acc6e39ec0bd0995794161374b22c696dabb98e93f6ca9300b22f3b904921fbf560bb72145f4fa"
XCTAssertEqual(output.transactionID.hexString, expectedTransactionID)
XCTAssertEqual(output.signature.hexString, expectedSignature)
XCTAssertEqual(output.signedTransactionData.hexString, expectedSiexpectedSignedDatanedTx)
XCTAssertEqual(output.signedTransactionData.hexString, expectedSignedData)
}
}

0 comments on commit b6d6dbe

Please sign in to comment.