-
Notifications
You must be signed in to change notification settings - Fork 160
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
sreuland
merged 10 commits into
lightsail-network:soroban
from
overcat-deprecation:auth2
Sep 19, 2023
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8aead87
Add a way to sign authorization entries.
overcat e878628
Add a way to sign authorization entries.
overcat 7dfecb3
Add a way to sign authorization entries.
overcat 77016e0
WIP
overcat a32c437
WIP
overcat 1db5f4d
empty commit, trigger a pipeline
overcat 3f036ce
update changelog
overcat 2a7e732
update changelog
overcat d58c786
update docs
overcat 96467c9
Merge branch 'soroban' into auth2
overcat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,303 @@ | ||
package org.stellar.sdk; | ||
|
||
import java.io.IOException; | ||
import java.security.SecureRandom; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import org.stellar.sdk.scval.Scv; | ||
import org.stellar.sdk.xdr.EnvelopeType; | ||
import org.stellar.sdk.xdr.Hash; | ||
import org.stellar.sdk.xdr.HashIDPreimage; | ||
import org.stellar.sdk.xdr.Int64; | ||
import org.stellar.sdk.xdr.SCVal; | ||
import org.stellar.sdk.xdr.SorobanAddressCredentials; | ||
import org.stellar.sdk.xdr.SorobanAuthorizationEntry; | ||
import org.stellar.sdk.xdr.SorobanAuthorizedInvocation; | ||
import org.stellar.sdk.xdr.SorobanCredentials; | ||
import org.stellar.sdk.xdr.SorobanCredentialsType; | ||
import org.stellar.sdk.xdr.Uint32; | ||
import org.stellar.sdk.xdr.XdrUnsignedInteger; | ||
|
||
/** This class contains helper methods to sign {@link SorobanAuthorizationEntry}. */ | ||
public class Auth { | ||
/** | ||
* Actually authorizes an existing authorization entry using the given the credentials and | ||
* expiration details, returning a signed copy. | ||
* | ||
* <p>This "fills out" the authorization entry with a signature, indicating to the {@link | ||
* InvokeHostFunctionOperation} it's attached to that: | ||
* | ||
* <ul> | ||
* <li>a particular identity (i.e. signing {@link KeyPair} or {@link Signer}) | ||
* <li>approving the execution of an invocation tree (i.e. a simulation-acquired {@link | ||
* SorobanAuthorizedInvocation} or otherwise built) | ||
* <li>on a particular network (uniquely identified by its passphrase, see {@link Network}) | ||
* <li>until a particular ledger sequence is reached. | ||
* </ul> | ||
* | ||
* @param entry a base64 encoded unsigned Soroban authorization entry | ||
* @param signer a {@link KeyPair} which should correspond to the address in the `entry` | ||
* @param validUntilLedgerSeq the (exclusive) future ledger sequence number until which this | ||
* authorization entry should be valid (if `currentLedgerSeq==validUntil`, this is expired) | ||
* @param network the network is incorprated into the signature | ||
* @return a signed Soroban authorization entry | ||
*/ | ||
public static SorobanAuthorizationEntry authorizeEntry( | ||
String entry, KeyPair signer, Long validUntilLedgerSeq, Network network) { | ||
SorobanAuthorizationEntry entryXdr; | ||
try { | ||
entryXdr = SorobanAuthorizationEntry.fromXdrBase64(entry); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("Unable to convert entry to SorobanAuthorizationEntry", e); | ||
} | ||
return authorizeEntry(entryXdr, signer, validUntilLedgerSeq, network); | ||
} | ||
|
||
/** | ||
* Actually authorizes an existing authorization entry using the given the credentials and | ||
* expiration details, returning a signed copy. | ||
* | ||
* <p>This "fills out" the authorization entry with a signature, indicating to the {@link | ||
* InvokeHostFunctionOperation} it's attached to that: | ||
* | ||
* <ul> | ||
* <li>a particular identity (i.e. signing {@link KeyPair} or {@link Signer}) | ||
* <li>approving the execution of an invocation tree (i.e. a simulation-acquired {@link | ||
* SorobanAuthorizedInvocation} or otherwise built) | ||
* <li>on a particular network (uniquely identified by its passphrase, see {@link Network}) | ||
* <li>until a particular ledger sequence is reached. | ||
* </ul> | ||
* | ||
* @param entry a base64 encoded unsigned Soroban authorization entry | ||
* @param signer a {@link KeyPair} which should correspond to the address in the `entry` | ||
* @param validUntilLedgerSeq the (exclusive) future ledger sequence number until which this | ||
* authorization entry should be valid (if `currentLedgerSeq==validUntil`, this is expired) | ||
* @param network the network is incorprated into the signature | ||
* @return a signed Soroban authorization entry | ||
*/ | ||
public static SorobanAuthorizationEntry authorizeEntry( | ||
SorobanAuthorizationEntry entry, KeyPair signer, Long validUntilLedgerSeq, Network network) { | ||
Signer entrySigner = | ||
preimage -> { | ||
byte[] data; | ||
try { | ||
data = preimage.toXdrByteArray(); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("Unable to convert preimage to bytes", e); | ||
} | ||
byte[] payload = Util.hash(data); | ||
return signer.sign(payload); | ||
}; | ||
|
||
return authorizeEntry(entry, entrySigner, validUntilLedgerSeq, network); | ||
} | ||
|
||
/** | ||
* Actually authorizes an existing authorization entry using the given the credentials and | ||
* expiration details, returning a signed copy. | ||
* | ||
* <p>This "fills out" the authorization entry with a signature, indicating to the {@link | ||
* InvokeHostFunctionOperation} it's attached to that: | ||
* | ||
* <ul> | ||
* <li>a particular identity (i.e. signing {@link KeyPair} or {@link Signer}) | ||
* <li>approving the execution of an invocation tree (i.e. a simulation-acquired {@link | ||
* SorobanAuthorizedInvocation} or otherwise built) | ||
* <li>on a particular network (uniquely identified by its passphrase, see {@link Network}) | ||
* <li>until a particular ledger sequence is reached. | ||
* </ul> | ||
* | ||
* @param entry a base64 encoded unsigned Soroban authorization entry | ||
* @param signer A function which takes a payload (a {@link HashIDPreimage}) and returns the | ||
* signature of the hash of the raw payload bytes, see {@link Signer} | ||
* @param validUntilLedgerSeq the (exclusive) future ledger sequence number until which this | ||
* authorization entry should be valid (if `currentLedgerSeq==validUntil`, this is expired) | ||
* @param network the network is incorprated into the signature | ||
* @return a signed Soroban authorization entry | ||
*/ | ||
public static SorobanAuthorizationEntry authorizeEntry( | ||
String entry, Signer signer, Long validUntilLedgerSeq, Network network) { | ||
SorobanAuthorizationEntry entryXdr; | ||
try { | ||
entryXdr = SorobanAuthorizationEntry.fromXdrBase64(entry); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("Unable to convert entry to SorobanAuthorizationEntry", e); | ||
} | ||
return authorizeEntry(entryXdr, signer, validUntilLedgerSeq, network); | ||
} | ||
|
||
/** | ||
* Actually authorizes an existing authorization entry using the given the credentials and | ||
* expiration details, returning a signed copy. | ||
* | ||
* <p>This "fills out" the authorization entry with a signature, indicating to the {@link | ||
* InvokeHostFunctionOperation} it's attached to that: | ||
* | ||
* <ul> | ||
* <li>a particular identity (i.e. signing {@link KeyPair} or {@link Signer}) | ||
* <li>approving the execution of an invocation tree (i.e. a simulation-acquired {@link | ||
* SorobanAuthorizedInvocation} or otherwise built) | ||
* <li>on a particular network (uniquely identified by its passphrase, see {@link Network}) | ||
* <li>until a particular ledger sequence is reached. | ||
* </ul> | ||
* | ||
* @param entry an unsigned Soroban authorization entry | ||
* @param signer A function which takes a payload (a {@link HashIDPreimage}) and returns the | ||
* signature of the hash of the raw payload bytes, see {@link Signer} | ||
* @param validUntilLedgerSeq the (exclusive) future ledger sequence number until which this | ||
* authorization entry should be valid (if `currentLedgerSeq==validUntil`, this is expired) | ||
* @param network the network is incorprated into the signature | ||
* @return a signed Soroban authorization entry | ||
*/ | ||
public static SorobanAuthorizationEntry authorizeEntry( | ||
SorobanAuthorizationEntry entry, Signer signer, Long validUntilLedgerSeq, Network network) { | ||
SorobanAuthorizationEntry clone; | ||
try { | ||
clone = SorobanAuthorizationEntry.fromXdrByteArray(entry.toXdrByteArray()); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("Unable to clone SorobanAuthorizationEntry", e); | ||
} | ||
|
||
if (clone.getCredentials().getDiscriminant() | ||
!= SorobanCredentialsType.SOROBAN_CREDENTIALS_ADDRESS) { | ||
return clone; | ||
} | ||
|
||
SorobanAddressCredentials addressCredentials = clone.getCredentials().getAddress(); | ||
addressCredentials.setSignatureExpirationLedger( | ||
new Uint32(new XdrUnsignedInteger(validUntilLedgerSeq))); | ||
|
||
HashIDPreimage preimage = | ||
new HashIDPreimage.Builder() | ||
.discriminant(EnvelopeType.ENVELOPE_TYPE_SOROBAN_AUTHORIZATION) | ||
.sorobanAuthorization( | ||
new HashIDPreimage.HashIDPreimageSorobanAuthorization.Builder() | ||
.networkID(new Hash(network.getNetworkId())) | ||
.nonce(addressCredentials.getNonce()) | ||
.invocation(clone.getRootInvocation()) | ||
.signatureExpirationLedger(addressCredentials.getSignatureExpirationLedger()) | ||
.build()) | ||
.build(); | ||
byte[] signature = signer.sign(preimage); | ||
byte[] publicKey = Address.fromSCAddress(addressCredentials.getAddress()).getBytes(); | ||
|
||
byte[] data; | ||
try { | ||
data = preimage.toXdrByteArray(); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("Unable to convert preimage to bytes", e); | ||
} | ||
byte[] payload = Util.hash(data); | ||
if (!KeyPair.fromPublicKey(publicKey).verify(payload, signature)) { | ||
throw new IllegalArgumentException("signature does not match payload"); | ||
} | ||
|
||
// This structure is defined here: | ||
// https://soroban.stellar.org/docs/fundamentals-and-concepts/invoking-contracts-with-transactions#stellar-account-signatures | ||
SCVal sigScVal = | ||
Scv.toMap( | ||
new LinkedHashMap<SCVal, SCVal>() { | ||
{ | ||
put(Scv.toSymbol("public_key"), Scv.toBytes(publicKey)); | ||
put(Scv.toSymbol("signature"), Scv.toBytes(signature)); | ||
} | ||
}); | ||
addressCredentials.setSignature(Scv.toVec(Collections.singleton(sigScVal))); | ||
return clone; | ||
} | ||
|
||
/** | ||
* This builds an entry from scratch, allowing you to express authorization as a function of: | ||
* | ||
* <ul> | ||
* <li>a particular identity (i.e. signing {@link KeyPair} or {@link Signer}) | ||
* <li>approving the execution of an invocation tree (i.e. a simulation-acquired {@link | ||
* SorobanAuthorizedInvocation} or otherwise built) | ||
* <li>on a particular network (uniquely identified by its passphrase, see {@link Network}) | ||
* <li>until a particular ledger sequence is reached. | ||
* </ul> | ||
* | ||
* <p>This is in contrast to {@link Auth#authorizeEntry}, which signs an existing entry "in | ||
* place". | ||
* | ||
* @param signer a {@link KeyPair} used to sign the entry | ||
* @param validUntilLedgerSeq the (exclusive) future ledger sequence number until which this | ||
* authorization entry should be valid (if `currentLedgerSeq==validUntil`, this is expired) | ||
* @param invocation invocation the invocation tree that we're authorizing (likely, this comes | ||
* from transaction simulation) | ||
* @param network the network is incorprated into the signature | ||
* @return a signed Soroban authorization entry | ||
*/ | ||
public static SorobanAuthorizationEntry authorizeInvocation( | ||
KeyPair signer, | ||
Long validUntilLedgerSeq, | ||
SorobanAuthorizedInvocation invocation, | ||
Network network) { | ||
Signer entrySigner = | ||
preimage -> { | ||
try { | ||
byte[] payload = Util.hash(preimage.toXdrByteArray()); | ||
return signer.sign(payload); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}; | ||
return authorizeInvocation( | ||
entrySigner, signer.getAccountId(), validUntilLedgerSeq, invocation, network); | ||
} | ||
|
||
/** | ||
* This builds an entry from scratch, allowing you to express authorization as a function of: | ||
* | ||
* <ul> | ||
* <li>a particular identity (i.e. signing {@link KeyPair} or {@link Signer}) | ||
* <li>approving the execution of an invocation tree (i.e. a simulation-acquired {@link | ||
* SorobanAuthorizedInvocation} or otherwise built) | ||
* <li>on a particular network (uniquely identified by its passphrase, see {@link Network}) | ||
* <li>until a particular ledger sequence is reached. | ||
* </ul> | ||
* | ||
* <p>This is in contrast to {@link Auth#authorizeEntry}, which signs an existing entry "in | ||
* place". | ||
* | ||
* @param signer A function which takes a payload (a {@link HashIDPreimage}) and returns the | ||
* signature of the hash of the raw payload bytes, see {@link Signer} | ||
* @param publicKey the public identity of the signer | ||
* @param validUntilLedgerSeq the (exclusive) future ledger sequence number until which this | ||
* authorization entry should be valid (if `currentLedgerSeq==validUntil`, this is expired) | ||
* @param invocation invocation the invocation tree that we're authorizing (likely, this comes | ||
* from transaction simulation) | ||
* @param network the network is incorprated into the signature | ||
* @return a signed Soroban authorization entry | ||
*/ | ||
public static SorobanAuthorizationEntry authorizeInvocation( | ||
Signer signer, | ||
String publicKey, | ||
Long validUntilLedgerSeq, | ||
SorobanAuthorizedInvocation invocation, | ||
Network network) { | ||
long nonce = new SecureRandom().nextLong(); | ||
SorobanAuthorizationEntry entry = | ||
new SorobanAuthorizationEntry.Builder() | ||
.credentials( | ||
new SorobanCredentials.Builder() | ||
.discriminant(SorobanCredentialsType.SOROBAN_CREDENTIALS_ADDRESS) | ||
.address( | ||
new SorobanAddressCredentials.Builder() | ||
.address(new Address(publicKey).toSCAddress()) | ||
.nonce(new Int64(nonce)) | ||
.signatureExpirationLedger( | ||
new Uint32(new XdrUnsignedInteger(validUntilLedgerSeq))) | ||
.signature(Scv.toVoid()) | ||
.build()) | ||
.build()) | ||
.rootInvocation(invocation) | ||
.build(); | ||
return authorizeEntry(entry, signer, validUntilLedgerSeq, network); | ||
} | ||
|
||
/** An interface for signing a {@link HashIDPreimage} to produce a signature. */ | ||
public interface Signer { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice usage of functional interface! |
||
byte[] sign(HashIDPreimage preimage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:and maybe re-evaluate the overloaded signatures of
authorizeEntry
and see if that can be narrowed?