Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper functions to sign authorization entries. #537

Merged
merged 10 commits into from
Sep 19, 2023

Conversation

overcat
Copy link
Member

@overcat overcat commented Sep 19, 2023

Closes #489

The following is an example.

  public void sign()
      throws SorobanRpcErrorResponse,
          IOException,
          AccountNotFoundException,
          PrepareTransactionException {
    SorobanServer sorobanServer = new SorobanServer("http://127.0.0.1:8000/soroban/rpc");
    Network network = Network.STANDALONE;
    String contractId = "CDCYWK73YTYFJZZSJ5V7EDFNHYBG4QN3VUNG2IGD27KJDDPNCZKBCBXK";
    KeyPair sourceKp =
        KeyPair.fromSecretSeed("SAAPYAPTTRZMCUZFPG3G66V4ZMHTK4TWA6NS7U4F7Z3IMUD52EK4DDEV");
    KeyPair opInvokeKp =
        KeyPair.fromSecretSeed("SAEZSI6DY7AXJFIYA4PM6SIBNEYYXIEM2MSOTHFGKHDW32MBQ7KVO6EN");

    InvokeHostFunctionOperation op =
        InvokeHostFunctionOperation.invokeContractFunctionOperationBuilder(
                contractId,
                "increment",
                Arrays.asList(Scv.toAddress(opInvokeKp.getAccountId()), Scv.toUint32(10)))
            .build();

    TransactionBuilderAccount source = sorobanServer.getAccount(sourceKp.getAccountId());

    Transaction transaction =
        new TransactionBuilder(source, network)
            .addOperation(op)
            .addPreconditions(
                TransactionPreconditions.builder()
                    .timeBounds(
                        new TimeBounds(BigInteger.ZERO, TransactionPreconditions.TIMEOUT_INFINITE))
                    .build())
            .setBaseFee(50000)
            .build();

    System.out.println(transaction.toEnvelopeXdrBase64());

    SimulateTransactionResponse simulateTransactionResponse =
        sorobanServer.simulateTransaction(transaction);
    // you need to check simulateTransactionResponse
    InvokeHostFunctionOperation invokeOp =
        (InvokeHostFunctionOperation) transaction.getOperations()[0];
    // add all auth to invoke host func op
    simulateTransactionResponse
        .getResults()
        .get(0)
        .getAuth()
        .forEach(
            auth -> {
              SorobanAuthorizationEntry e =
                  Auth.authorizeEntry(
                      auth, opInvokeKp, simulateTransactionResponse.getLatestLedger() + 3, network);
              invokeOp.getAuth().add(e);
            });

    Transaction preparedTx =
        sorobanServer.prepareTransaction(transaction, simulateTransactionResponse);
    preparedTx.sign(sourceKp);
    System.out.println(preparedTx.toEnvelopeXdrBase64());

    SendTransactionResponse sendTransaction = sorobanServer.sendTransaction(preparedTx);
    System.out.println(sendTransaction);
  }

@overcat
Copy link
Member Author

overcat commented Sep 19, 2023

Hi @sreuland, let's include this PR in the new release.

/** This class contains helper methods to sign {@link SorobanAuthorizationEntry}. */
public class Auth {
/**
* Actually authorizes an existing authorization entry using the given the credentials and
Copy link
Contributor

@sreuland sreuland Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for consideration in a later pr, could remove this duplicated docs by using @see instead:

/**
 * @see Auth#authorizeEntry(String, KeyPair, Long, Network) authorizeEntry
 */

and maybe re-evaluate the overloaded signatures of authorizeEntry and see if that can be narrowed?

}

/** An interface for signing a {@link HashIDPreimage} to produce a signature. */
public interface Signer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice usage of functional interface!

}

@Test
public void testSignAuthorizeEntryWithBase64EntryAndFunctionSigner() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for consideration later, the duplicate test case variants for base64 and xdr object graph parameters could probably be reduced to just the base64 parameters, as that will by default exercise the xdr object path internally as well correct? would just. be less test code to maintain over time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will remove it later.

Copy link
Contributor

@sreuland sreuland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great work, lot of complexity to auth, looks clean.

@sreuland sreuland merged commit 8eb8c4f into lightsail-network:soroban Sep 19, 2023
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants