Search in sources :

Example 1 with Faucet

use of dev.jlibra.faucet.Faucet in project jlibra by ketola.

the class MintExample method main.

public static void main(String[] args) {
    AuthenticationKey authenticationKey = AuthenticationKey.fromHexString("8f30171675008e598af4d57c159dad7240fc2800a33e1790730244ab931630a2");
    Faucet faucet = Faucet.builder().build();
    faucet.mint(authenticationKey, 100L * 1_000_000L, CURRENCY);
    DiemClient client = DiemClient.builder().withUrl("http://testnet.diem.com/v1").build();
    Wait.until(accountHasPositiveBalance(AccountAddress.fromAuthenticationKey(authenticationKey), client));
    Account account = client.getAccount(AccountAddress.fromAuthenticationKey(authenticationKey));
    logger.info("Balance: {} {}", account.balances().get(0).amount() / 1_000_000, account.balances().get(0).currency());
}
Also used : DiemClient(dev.jlibra.client.DiemClient) Account(dev.jlibra.client.views.Account) AuthenticationKey(dev.jlibra.AuthenticationKey) Faucet(dev.jlibra.faucet.Faucet)

Example 2 with Faucet

use of dev.jlibra.faucet.Faucet in project jlibra by ketola.

the class CreateChildVaspAccountExample method main.

/**
 * Based on Diem white paper, VASPs are: "Virtual Asset Service Providers
 * (exchanges and custodial wallets) that are registered or licensed as VASPs in
 * a Financial Action Task Force (FATF) member jurisdiction, or are registered
 * or licensed in a FATF member jurisdiction and are permitted to perform VASP
 * activities under such license or registration (Regulated VASPs)"
 */
public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    DiemClient client = DiemClient.builder().withUrl("https://testnet.diem.com/v1").build();
    Faucet faucet = Faucet.builder().build();
    KeyPair parentVaspKeyPair = generateKeyPair();
    // In the testnet new VASP accounts can be created by minting money to a new
    // address:
    AuthenticationKey parentVaspAuthKey = AuthenticationKey.fromPublicKey(parentVaspKeyPair.getPublic());
    faucet.mint(parentVaspAuthKey, 100L * 1_000_000L, CURRENCY);
    Wait.until(accountHasPositiveBalance(AccountAddress.fromAuthenticationKey(parentVaspAuthKey), client));
    int parentVaspSequenceNumber = 0;
    logger.info("Parent vasp authentication key: {} address: {}", parentVaspAuthKey, AccountAddress.fromAuthenticationKey(parentVaspAuthKey));
    // A child account for the VASP account is created by the parent account using
    // the
    // "create_child_vasp_account" move script
    // 
    // The parameters are the new account address, auth key prefix and a boolean
    // telling if all the supported currencies should be added to the new account
    KeyPair childVaspAccountKeyPair = generateKeyPair();
    AuthenticationKey childVaspAccountAuthKey = AuthenticationKey.fromPublicKey(childVaspAccountKeyPair.getPublic());
    AccountAddress childVaspAccountAddress = AccountAddress.fromAuthenticationKey(childVaspAccountAuthKey);
    logger.info("Child vasp authentication key: {} address: {}", childVaspAccountAuthKey, childVaspAccountAddress);
    AccountAddressArgument childAccountArgument = AccountAddressArgument.from(childVaspAccountAddress);
    U8VectorArgument authKeyPrefixArgument = U8VectorArgument.from(childVaspAccountAuthKey.prefix());
    BoolArgument createAllCurrenciesArgument = BoolArgument.from(false);
    U64Argument initialBalanceArgument = U64Argument.from(1_000_000);
    Transaction transaction = ImmutableTransaction.builder().sequenceNumber(parentVaspSequenceNumber).maxGasAmount(640000).gasUnitPrice(1).gasCurrencyCode(CURRENCY).sender(AccountAddress.fromAuthenticationKey(parentVaspAuthKey)).expirationTimestampSecs(Instant.now().getEpochSecond() + 60).payload(ImmutableScript.builder().typeArguments(asList(Struct.typeTagForCurrency(CURRENCY))).code(Move.createChildVaspAccount()).addArguments(childAccountArgument, authKeyPrefixArgument, createAllCurrenciesArgument, initialBalanceArgument).build()).chainId(ChainId.DEVNET).build();
    SignedTransaction signedTransaction = ImmutableSignedTransaction.builder().authenticator(ImmutableTransactionAuthenticatorEd25519.builder().publicKey(PublicKey.fromPublicKey(parentVaspKeyPair.getPublic())).signature(Signature.signTransaction(transaction, parentVaspKeyPair.getPrivate())).build()).transaction(transaction).build();
    client.submit(signedTransaction);
    Wait.until(transactionFound(AccountAddress.fromAuthenticationKey(parentVaspAuthKey), parentVaspSequenceNumber, client));
    logger.info("----------------------------");
    logger.info("Parent VASP account: {}", client.getAccount(AccountAddress.fromAuthenticationKey(parentVaspAuthKey)));
    logger.info("----------------------------");
    logger.info("----------------------------");
    logger.info("Child VASP account: {}", client.getAccount(childVaspAccountAddress));
    logger.info("----------------------------");
}
Also used : BoolArgument(dev.jlibra.transaction.argument.BoolArgument) KeyPair(java.security.KeyPair) AccountAddressArgument(dev.jlibra.transaction.argument.AccountAddressArgument) U8VectorArgument(dev.jlibra.transaction.argument.U8VectorArgument) AccountAddress(dev.jlibra.AccountAddress) DiemClient(dev.jlibra.client.DiemClient) ImmutableTransaction(dev.jlibra.transaction.ImmutableTransaction) ImmutableSignedTransaction(dev.jlibra.transaction.ImmutableSignedTransaction) SignedTransaction(dev.jlibra.transaction.SignedTransaction) Transaction(dev.jlibra.transaction.Transaction) AuthenticationKey(dev.jlibra.AuthenticationKey) Faucet(dev.jlibra.faucet.Faucet) U64Argument(dev.jlibra.transaction.argument.U64Argument) ImmutableSignedTransaction(dev.jlibra.transaction.ImmutableSignedTransaction) SignedTransaction(dev.jlibra.transaction.SignedTransaction)

Example 3 with Faucet

use of dev.jlibra.faucet.Faucet in project jlibra by ketola.

the class KeyRotationExample method main.

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    DiemClient client = DiemClient.builder().withUrl("https://testnet.diem.com/v1").build();
    Faucet faucet = Faucet.builder().build();
    /*
         * Create a key pair, calculate the libra address and add some coins to the
         * account
         */
    KeyPairGenerator kpGen = KeyPairGenerator.getInstance("Ed25519", "BC");
    KeyPair keyPairOriginal = kpGen.generateKeyPair();
    BCEdDSAPrivateKey privateKeyOriginal = (BCEdDSAPrivateKey) keyPairOriginal.getPrivate();
    BCEdDSAPublicKey publicKeyOriginal = (BCEdDSAPublicKey) keyPairOriginal.getPublic();
    AuthenticationKey authenticationKeyOriginal = AuthenticationKey.fromPublicKey(publicKeyOriginal);
    AccountAddress addressOriginal = AccountAddress.fromAuthenticationKey(authenticationKeyOriginal);
    logger.info("Account address: {}", addressOriginal.toString());
    logger.info("Authentication key: {}", authenticationKeyOriginal.toString());
    faucet.mint(authenticationKeyOriginal, 100L * 1_000_000L, CURRENCY);
    Wait.until(accountHasPositiveBalance(addressOriginal, client));
    /*
         * Get the account state to verify that the account exists and the coins were
         * transferred. Notice, that at this point the address == authentication key.
         */
    logger.info("-----------------------------------------------------------------------------------------------");
    logger.info("Get the account state for the new account");
    getAccountState(addressOriginal, client);
    logger.info("-----------------------------------------------------------------------------------------------\n");
    /*
         * Create a new key pair. This keypair will be changed to be the signing keys of
         * the account created earlier
         */
    KeyPair keyPairNew = kpGen.generateKeyPair();
    BCEdDSAPrivateKey privateKeyNew = (BCEdDSAPrivateKey) keyPairNew.getPrivate();
    BCEdDSAPublicKey publicKeyNew = (BCEdDSAPublicKey) keyPairNew.getPublic();
    /*
         * Send the transaction for changing the signing keys
         */
    logger.info("Change the signing keys..");
    rotateAuthenticationKey(privateKeyOriginal, publicKeyOriginal, addressOriginal, publicKeyNew, 0, client);
    Wait.until(transactionFound(addressOriginal, 0, client));
    logger.info("-----------------------------------------------------------------------------------------------");
    logger.info("Get the account state for the account");
    getAccountState(addressOriginal, client);
    logger.info("-----------------------------------------------------------------------------------------------\n");
    /*
         * In this step a new key pair is created and an attempt is made to change these
         * to be the new signing keys. This should fail because we are using the
         * original signing keys which should not work anymore because the keys were
         * changed.
         */
    KeyPair keyPairNew2 = kpGen.generateKeyPair();
    BCEdDSAPrivateKey privateKeyNew2 = (BCEdDSAPrivateKey) keyPairNew2.getPrivate();
    BCEdDSAPublicKey publicKeyNew2 = (BCEdDSAPublicKey) keyPairNew2.getPublic();
    logger.info("Change the signing keys..");
    try {
        rotateAuthenticationKey(privateKeyOriginal, publicKeyOriginal, addressOriginal, publicKeyNew2, 1, client);
    } catch (DiemRuntimeException e) {
        logger.error(e.getMessage());
        logger.error("This failed because the the original keys cannot be used anymore");
    }
    logger.info("-----------------------------------------------------------------------------------------------");
    logger.info("Get the account state for the account");
    getAccountState(addressOriginal, client);
    logger.info("-----------------------------------------------------------------------------------------------\n");
    /*
         * Now a new attempt is done using the correct keys and this time the
         * transaction should go through.
         */
    logger.info("Change the signing keys..");
    rotateAuthenticationKey(privateKeyNew, publicKeyNew, addressOriginal, publicKeyNew2, 1, client);
    logger.info("This succeeded because now the updated keys were used.");
    Wait.until(transactionFound(addressOriginal, 1, client));
    /*
         * Get the account state to verify that the authentication key was changed.
         */
    logger.info("-----------------------------------------------------------------------------------------------");
    getAccountState(addressOriginal, client);
    logger.info("-----------------------------------------------------------------------------------------------");
}
Also used : DiemClient(dev.jlibra.client.DiemClient) DiemRuntimeException(dev.jlibra.DiemRuntimeException) KeyPair(java.security.KeyPair) AuthenticationKey(dev.jlibra.AuthenticationKey) Faucet(dev.jlibra.faucet.Faucet) KeyPairGenerator(java.security.KeyPairGenerator) BCEdDSAPrivateKey(org.bouncycastle.jcajce.provider.asymmetric.edec.BCEdDSAPrivateKey) BCEdDSAPublicKey(org.bouncycastle.jcajce.provider.asymmetric.edec.BCEdDSAPublicKey) AccountAddress(dev.jlibra.AccountAddress)

Example 4 with Faucet

use of dev.jlibra.faucet.Faucet in project jlibra by ketola.

the class MultisigTransactionTest method testMultisigTransaction.

@Test
public void testMultisigTransaction() {
    Faucet faucet = Faucet.builder().build();
    // source account, multisig account with 32 keypairs, 30 threshold
    List<KeyPair> keyPairs = generateKeyPairs(32);
    MultiSignaturePublicKey multiPubKey = MultiSignaturePublicKey.create(keyPairs.stream().map(kp -> PublicKey.fromPublicKey(kp.getPublic())).collect(toList()), 30);
    AuthenticationKey authenticationKey = AuthenticationKey.fromMultiSignaturePublicKey(multiPubKey);
    AccountAddress accountAddress = AccountAddress.fromAuthenticationKey(authenticationKey);
    faucet.mint(authenticationKey, 10 * 1_000_000, CURRENCY);
    Wait.until(accountHasPositiveBalance(accountAddress, client));
    // target account
    KeyPair targetAccount = generateKeyPairs(1).get(0);
    createChildVaspAccount(keyPairs, targetAccount);
    PublicKey publicKeyTarget = PublicKey.fromPublicKey(targetAccount.getPublic());
    AuthenticationKey authenticationKeyTarget = AuthenticationKey.fromPublicKey(publicKeyTarget);
    long transferAmount = 2_000_000;
    // Arguments for the peer to peer transaction
    U64Argument amountArgument = U64Argument.from(transferAmount);
    AccountAddressArgument addressArgument = AccountAddressArgument.from(AccountAddress.fromAuthenticationKey(authenticationKeyTarget));
    U8VectorArgument metadataArgument = U8VectorArgument.from(ByteArray.from(new byte[0]));
    U8VectorArgument signatureArgument = U8VectorArgument.from(ByteArray.from(new byte[0]));
    logger.info("Sender auth key {}, sender address {}", authenticationKey, accountAddress);
    logger.info("Receiver auth key {}, sender address {}", authenticationKeyTarget, AccountAddress.fromAuthenticationKey(authenticationKeyTarget));
    int sequenceNumber = 1;
    Transaction transaction = ImmutableTransaction.builder().sequenceNumber(sequenceNumber).maxGasAmount(2_000_000).gasCurrencyCode(CURRENCY).gasUnitPrice(1).sender(accountAddress).expirationTimestampSecs(Instant.now().getEpochSecond() + 60).payload(ImmutableScript.builder().code(Move.peerToPeerTransferWithMetadata()).typeArguments(asList(Struct.typeTagForCurrency(CURRENCY))).addArguments(addressArgument, amountArgument, metadataArgument, signatureArgument).build()).chainId(ChainId.DEVNET).build();
    Signature signature = createSignature(keyPairs, transaction);
    SignedTransaction signedTransaction = ImmutableSignedTransaction.builder().authenticator(ImmutableTransactionAuthenticatorMultiEd25519.builder().publicKey(multiPubKey).signature(signature).build()).transaction(transaction).build();
    client.submit(signedTransaction);
    Wait.until(transactionFound(accountAddress, sequenceNumber, client));
    Account targetAccountState = client.getAccount(AccountAddress.fromAuthenticationKey(authenticationKeyTarget));
    assertThat(targetAccountState.balances().get(0).amount(), is(transferAmount));
}
Also used : Account(dev.jlibra.client.views.Account) KeyPair(java.security.KeyPair) AccountAddressArgument(dev.jlibra.transaction.argument.AccountAddressArgument) U8VectorArgument(dev.jlibra.transaction.argument.U8VectorArgument) MultiSignaturePublicKey(dev.jlibra.MultiSignaturePublicKey) MultiSignaturePublicKey(dev.jlibra.MultiSignaturePublicKey) PublicKey(dev.jlibra.PublicKey) AccountAddress(dev.jlibra.AccountAddress) ImmutableTransaction(dev.jlibra.transaction.ImmutableTransaction) ImmutableSignedTransaction(dev.jlibra.transaction.ImmutableSignedTransaction) SignedTransaction(dev.jlibra.transaction.SignedTransaction) Transaction(dev.jlibra.transaction.Transaction) AuthenticationKey(dev.jlibra.AuthenticationKey) Signature(dev.jlibra.transaction.Signature) Faucet(dev.jlibra.faucet.Faucet) U64Argument(dev.jlibra.transaction.argument.U64Argument) ImmutableSignedTransaction(dev.jlibra.transaction.ImmutableSignedTransaction) SignedTransaction(dev.jlibra.transaction.SignedTransaction) Test(org.junit.jupiter.api.Test)

Example 5 with Faucet

use of dev.jlibra.faucet.Faucet in project jlibra by ketola.

the class AsyncExample method createAccount.

private static void createAccount(AuthenticationKey authKey) {
    DiemClient client = DiemClient.builder().withUrl("https://testnet.diem.com/v1").build();
    Faucet faucet = Faucet.builder().build();
    faucet.mint(authKey, 100L * 1_000_000L, CURRENCY);
    Wait.until(Conditions.accountExists(AccountAddress.fromAuthenticationKey(authKey), client));
}
Also used : DiemClient(dev.jlibra.client.DiemClient) Faucet(dev.jlibra.faucet.Faucet)

Aggregations

Faucet (dev.jlibra.faucet.Faucet)6 AuthenticationKey (dev.jlibra.AuthenticationKey)5 DiemClient (dev.jlibra.client.DiemClient)5 AccountAddress (dev.jlibra.AccountAddress)4 KeyPair (java.security.KeyPair)4 Account (dev.jlibra.client.views.Account)3 PublicKey (dev.jlibra.PublicKey)2 ImmutableSignedTransaction (dev.jlibra.transaction.ImmutableSignedTransaction)2 ImmutableTransaction (dev.jlibra.transaction.ImmutableTransaction)2 SignedTransaction (dev.jlibra.transaction.SignedTransaction)2 Transaction (dev.jlibra.transaction.Transaction)2 AccountAddressArgument (dev.jlibra.transaction.argument.AccountAddressArgument)2 U64Argument (dev.jlibra.transaction.argument.U64Argument)2 U8VectorArgument (dev.jlibra.transaction.argument.U8VectorArgument)2 KeyPairGenerator (java.security.KeyPairGenerator)2 DiemRuntimeException (dev.jlibra.DiemRuntimeException)1 MultiSignaturePublicKey (dev.jlibra.MultiSignaturePublicKey)1 DualAttestation (dev.jlibra.transaction.DualAttestation)1 ImmutableDualAttestation (dev.jlibra.transaction.ImmutableDualAttestation)1 Signature (dev.jlibra.transaction.Signature)1