|
| 1 | +package jota; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.GsonBuilder; |
| 5 | +import jota.error.InvalidAddressException; |
| 6 | +import jota.error.InvalidBundleException; |
| 7 | +import jota.error.InvalidTransferException; |
| 8 | +import jota.model.Bundle; |
| 9 | +import jota.model.Transaction; |
| 10 | +import jota.model.Transfer; |
| 11 | +import jota.pow.JCurl; |
| 12 | +import jota.utils.Converter; |
| 13 | +import jota.utils.Multisig; |
| 14 | +import jota.utils.Signing; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +/** |
| 24 | + * Created by Adrian on 18.02.2017. |
| 25 | + */ |
| 26 | +public class IotaMultisigTest { |
| 27 | + |
| 28 | + private static final String TEST_SEED1 = "ABCDFG"; |
| 29 | + private static final String TEST_SEED2 = "FDSAG"; |
| 30 | + private static final String REMAINDER_ADDRESS = "NZRALDYNVGJWUVLKDWFKJVNYLWQGCWYCURJIIZRLJIKSAIVZSGEYKTZRDBGJLOA9AWYJQB9IPWRAKUC9FBDRZJZXZG"; |
| 31 | + private static final String RECEIVE_ADDRESS = "ZGHXPZYDKXPEOSQTAQOIXEEI9K9YKFKCWKYYTYAUWXK9QZAVMJXWAIZABOXHHNNBJIEBEUQRTBWGLYMTX"; |
| 32 | + |
| 33 | + private static Gson gson = new GsonBuilder().create(); |
| 34 | + private IotaAPI iotaClient; |
| 35 | + |
| 36 | + @Before |
| 37 | + public void createApiClientInstance() { |
| 38 | + iotaClient = new IotaAPI.Builder().build(); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void basicMultiSigTest() throws InvalidBundleException, InvalidTransferException, InvalidAddressException { |
| 43 | + |
| 44 | + Multisig ms = new Multisig(); |
| 45 | + |
| 46 | + // First co-signer uses security level 3 and index 0 for the private key |
| 47 | + String digestOne = ms.getDigest(TEST_SEED1, 3, 0); |
| 48 | + |
| 49 | + // We initiate the multisig address generation by absorbing the key digest |
| 50 | + String initiatedMultisigDigests = ms.addAddressDigest(digestOne, ""); |
| 51 | + |
| 52 | + // Second cosigner also uses security level 3 and index 0 for the private key |
| 53 | + String digestTwo = ms.getDigest(TEST_SEED2, 3, 0); |
| 54 | + |
| 55 | + // Add the multisig by absorbing the second cosigners key digest |
| 56 | + String finalMultisigDigests = ms.addAddressDigest(digestTwo, initiatedMultisigDigests); |
| 57 | + |
| 58 | + // finally we generate the multisig address itself |
| 59 | + String multiSigAddress = ms.finalizeAddress(finalMultisigDigests); |
| 60 | + |
| 61 | + System.out.println("MultisigAddress = " + multiSigAddress); |
| 62 | + |
| 63 | + boolean isValidMultisigAddress = ms.validateAddress(multiSigAddress, new int[][]{Converter.trits(digestOne), Converter.trits(digestTwo)}); |
| 64 | + |
| 65 | + System.out.println("Is a valid multisig address " + isValidMultisigAddress); |
| 66 | + |
| 67 | + assertTrue("Address is not a valid multisigaddress", isValidMultisigAddress); |
| 68 | + Transfer transfer = new Transfer(RECEIVE_ADDRESS, 999, "", ""); |
| 69 | + List<Transfer> transfers = new ArrayList<>(); |
| 70 | + transfers.add(transfer); |
| 71 | + |
| 72 | + List<Transaction> trxs = iotaClient.initiateTransfer(6, multiSigAddress, REMAINDER_ADDRESS, transfers, true); |
| 73 | + |
| 74 | + Bundle bundle = new Bundle(trxs, trxs.size()); |
| 75 | + |
| 76 | + bundle = ms.addSignature(bundle, multiSigAddress, ms.getKey(TEST_SEED1, 0, 3)); |
| 77 | + |
| 78 | + bundle = ms.addSignature(bundle, multiSigAddress, ms.getKey(TEST_SEED2, 0, 3)); |
| 79 | + |
| 80 | + |
| 81 | + Signing sgn = new Signing(new JCurl()); |
| 82 | + |
| 83 | + boolean isValidSignature = sgn.validateSignatures(bundle, multiSigAddress); |
| 84 | + assertTrue("MultiSignature not valid", isValidSignature); |
| 85 | + System.out.println("Result of multi-signature validation is " + isValidSignature); |
| 86 | + } |
| 87 | +} |
0 commit comments