|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// |
| 3 | +// Copyright © 2017 Trust Wallet. |
| 4 | + |
| 5 | +package com.trustwallet.core.app.blockchains.pactus |
| 6 | + |
| 7 | +import com.google.protobuf.ByteString |
| 8 | +import com.trustwallet.core.app.utils.toHexByteArray |
| 9 | +import org.junit.Assert.assertEquals |
| 10 | +import org.junit.Test |
| 11 | +import wallet.core.jni.PrivateKey |
| 12 | +import wallet.core.java.AnySigner |
| 13 | +import wallet.core.jni.CoinType |
| 14 | +import wallet.core.jni.CoinType.PACTUS |
| 15 | +import wallet.core.jni.proto.Pactus |
| 16 | +import wallet.core.jni.proto.Pactus.SigningOutput |
| 17 | +import com.trustwallet.core.app.utils.Numeric |
| 18 | +import org.junit.Assert.assertArrayEquals |
| 19 | + |
| 20 | +class TestPactusSigner { |
| 21 | + |
| 22 | + init { |
| 23 | + System.loadLibrary("TrustWalletCore") |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + fun testPactusTransferSigning() { |
| 28 | + // Successfully broadcasted transaction: |
| 29 | + // https://pacviewer.com/transaction/1b6b7226f7935a15f05371d1a1fefead585a89704ce464b7cc1d453d299d235f |
| 30 | + // |
| 31 | + val signingInput = Pactus.SigningInput.newBuilder() |
| 32 | + signingInput.apply { |
| 33 | + privateKey = ByteString.copyFrom( |
| 34 | + PrivateKey("4e51f1f3721f644ac7a193be7f5e7b8c2abaa3467871daf4eacb5d3af080e5d6" |
| 35 | + .toHexByteArray()).data() |
| 36 | + ) |
| 37 | + transaction = Pactus.TransactionMessage.newBuilder().apply { |
| 38 | + lockTime = 2335524 |
| 39 | + fee = 10000000 |
| 40 | + memo = "wallet-core" |
| 41 | + transfer = Pactus.TransferPayload.newBuilder().apply { |
| 42 | + sender = "pc1rwzvr8rstdqypr80ag3t6hqrtnss9nwymcxy3lr" |
| 43 | + receiver = "pc1r0g22ufzn8qtw0742dmfglnw73e260hep0k3yra" |
| 44 | + amount = 200000000 |
| 45 | + }.build() |
| 46 | + }.build() |
| 47 | + } |
| 48 | + |
| 49 | + val output = AnySigner.sign(signingInput.build(), PACTUS, SigningOutput.parser()) |
| 50 | + |
| 51 | + assertEquals( |
| 52 | + "0x1b6b7226f7935a15f05371d1a1fefead585a89704ce464b7cc1d453d299d235f", |
| 53 | + Numeric.toHexString(output.transactionId.toByteArray()) |
| 54 | + ) |
| 55 | + |
| 56 | + assertEquals( |
| 57 | + "0x4ed8fee3d8992e82660dd05bbe8608fc56ceabffdeeee61e3213b9b49d33a0fc8dea6d79ee7ec60f66433f189ed9b3c50b2ad6fa004e26790ee736693eda8506", |
| 58 | + Numeric.toHexString(output.signature.toByteArray()) |
| 59 | + ) |
| 60 | + |
| 61 | + assertEquals( |
| 62 | + "0x000124a3230080ade2040b77616c6c65742d636f726501037098338e0b6808119dfd4457ab806b9c2059b89b037a14ae24533816e7faaa6ed28fcdde8e55a7df218084af5f4ed8fee3d8992e82660dd05bbe8608fc56ceabffdeeee61e3213b9b49d33a0fc8dea6d79ee7ec60f66433f189ed9b3c50b2ad6fa004e26790ee736693eda850695794161374b22c696dabb98e93f6ca9300b22f3b904921fbf560bb72145f4fa", |
| 63 | + Numeric.toHexString(output.signedTransactionData.toByteArray()) |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun testPactusBondWithPublicKeySigning() { |
| 69 | + // Successfully broadcasted transaction: |
| 70 | + // https://pacviewer.com/transaction/d194b445642a04ec78ced4448696e50b733f2f0b517a23871882c0eefaf1c28f |
| 71 | + // |
| 72 | + val signingInput = Pactus.SigningInput.newBuilder() |
| 73 | + signingInput.apply { |
| 74 | + privateKey = ByteString.copyFrom( |
| 75 | + PrivateKey("4e51f1f3721f644ac7a193be7f5e7b8c2abaa3467871daf4eacb5d3af080e5d6" |
| 76 | + .toHexByteArray()).data() |
| 77 | + ) |
| 78 | + transaction = Pactus.TransactionMessage.newBuilder().apply { |
| 79 | + lockTime = 2339009 |
| 80 | + fee = 10000000 |
| 81 | + memo = "wallet-core" |
| 82 | + bond = Pactus.BondPayload.newBuilder().apply { |
| 83 | + sender = "pc1rwzvr8rstdqypr80ag3t6hqrtnss9nwymcxy3lr" |
| 84 | + receiver = "pc1p9y5gmu9l002tt60wak9extgvwm69rq3a9ackrl" |
| 85 | + stake = 1000000000 |
| 86 | + publicKey = "public1pnz75msstqdrq5eguvcwanug0zauhqjw2cc4flmez3qethnp68y64ehc4k69amapj7x4na2uda0snqz4yxujgx3jsse4f64fgy7jkh0xauvhrc5ts09vfk48g85t0js66hvajm6xruemsvlxqv3xvkyur8v9v0mtn" |
| 87 | + }.build() |
| 88 | + }.build() |
| 89 | + } |
| 90 | + |
| 91 | + val output = AnySigner.sign(signingInput.build(), PACTUS, SigningOutput.parser()) |
| 92 | + |
| 93 | + assertEquals( |
| 94 | + "0xd194b445642a04ec78ced4448696e50b733f2f0b517a23871882c0eefaf1c28f", |
| 95 | + Numeric.toHexString(output.transactionId.toByteArray()) |
| 96 | + ) |
| 97 | + |
| 98 | + assertEquals( |
| 99 | + "0x0d7bc6d94927534b89e2f53bcfc9fc849e0e2982438955eda55b4338328adac79d4ee3216d143f0e1629764ab650734f8ba188e716d71f9eff65e39ce7006300", |
| 100 | + Numeric.toHexString(output.signature.toByteArray()) |
| 101 | + ) |
| 102 | + |
| 103 | + assertEquals( |
| 104 | + "0x0001c1b0230080ade2040b77616c6c65742d636f726502037098338e0b6808119dfd4457ab806b9c2059b89b0129288df0bf7bd4b5e9eeed8b932d0c76f451823d6098bd4dc20b03460a651c661dd9f10f17797049cac62a9fef228832bbcc3a39355cdf15b68bddf432f1ab3eab8debe1300aa43724834650866a9d552827a56bbcdde32e3c517079589b54e83d16f9435abb3b2de8c3e677067cc0644ccb13833b8094ebdc030d7bc6d94927534b89e2f53bcfc9fc849e0e2982438955eda55b4338328adac79d4ee3216d143f0e1629764ab650734f8ba188e716d71f9eff65e39ce700630095794161374b22c696dabb98e93f6ca9300b22f3b904921fbf560bb72145f4fa", |
| 105 | + Numeric.toHexString(output.signedTransactionData.toByteArray()) |
| 106 | + ) |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + fun testPactusBondWithoutPublicKeySigning() { |
| 111 | + // Successfully broadcasted transaction: |
| 112 | + // https://pacviewer.com/transaction/f83f583a5c40adf93a90ea536a7e4b467d30ca4f308d5da52624d80c42adec80 |
| 113 | + // |
| 114 | + val signingInput = Pactus.SigningInput.newBuilder() |
| 115 | + signingInput.apply { |
| 116 | + privateKey = ByteString.copyFrom( |
| 117 | + PrivateKey("4e51f1f3721f644ac7a193be7f5e7b8c2abaa3467871daf4eacb5d3af080e5d6" |
| 118 | + .toHexByteArray()).data() |
| 119 | + ) |
| 120 | + transaction = Pactus.TransactionMessage.newBuilder().apply { |
| 121 | + lockTime = 2335580 |
| 122 | + fee = 10000000 |
| 123 | + memo = "wallet-core" |
| 124 | + bond = Pactus.BondPayload.newBuilder().apply { |
| 125 | + sender = "pc1rwzvr8rstdqypr80ag3t6hqrtnss9nwymcxy3lr" |
| 126 | + receiver = "pc1p6taz5l2kq5ppnxv4agnqj48svnvsy797xpe6wd" |
| 127 | + stake = 1000000000 |
| 128 | + }.build() |
| 129 | + }.build() |
| 130 | + } |
| 131 | + |
| 132 | + val output = AnySigner.sign(signingInput.build(), PACTUS, SigningOutput.parser()) |
| 133 | + |
| 134 | + assertEquals( |
| 135 | + "0xf83f583a5c40adf93a90ea536a7e4b467d30ca4f308d5da52624d80c42adec80", |
| 136 | + Numeric.toHexString(output.transactionId.toByteArray()) |
| 137 | + ) |
| 138 | + |
| 139 | + assertEquals( |
| 140 | + "0x9e6279fb64067c7d7316ac74630bbb8589df268aa4548f1c7d85c087a8748ff0715b9149afbd94c5d8ee6b37c787ec63e963cbb38be513ebc436aa58f9a8f00d", |
| 141 | + Numeric.toHexString(output.signature.toByteArray()) |
| 142 | + ) |
| 143 | + |
| 144 | + assertEquals( |
| 145 | + "0x00015ca3230080ade2040b77616c6c65742d636f726502037098338e0b6808119dfd4457ab806b9c2059b89b01d2fa2a7d560502199995ea260954f064d90278be008094ebdc039e6279fb64067c7d7316ac74630bbb8589df268aa4548f1c7d85c087a8748ff0715b9149afbd94c5d8ee6b37c787ec63e963cbb38be513ebc436aa58f9a8f00d95794161374b22c696dabb98e93f6ca9300b22f3b904921fbf560bb72145f4fa", |
| 146 | + Numeric.toHexString(output.signedTransactionData.toByteArray()) |
| 147 | + ) |
| 148 | + } |
| 149 | +} |
0 commit comments