-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates soroban-client version, updates use of helpers (#13)
* updates soroban-client version, updates use of helpers * cleans up not needed xdr conversions and duplicate logic * port new helper for signing auth entries * uses signAuthEntry while SWK adds new api * cleans up wip helper refactors * correctly fetched contract code expiration * uses soroban rpc helpers to check simulation response types
- Loading branch information
1 parent
d579993
commit b8331c2
Showing
8 changed files
with
1,133 additions
and
1,077 deletions.
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
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
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,66 @@ | ||
import { | ||
Address, | ||
StrKey, | ||
xdr, | ||
nativeToScVal, | ||
Keypair, | ||
hash, | ||
} from "soroban-client"; | ||
|
||
// This can be replaced with the same helper in the sdk when it lands | ||
// https://github.com/stellar/js-stellar-base/pull/678 | ||
|
||
export async function authorizeEntry( | ||
entry: xdr.SorobanAuthorizationEntry, | ||
signer: (input: Buffer) => Promise<Buffer>, | ||
validUntilLedgerSeq: any, | ||
networkPassphrase: string, | ||
) { | ||
// no-op | ||
if ( | ||
entry.credentials().switch() !== | ||
xdr.SorobanCredentialsType.sorobanCredentialsAddress() | ||
) { | ||
return entry; | ||
} | ||
|
||
const addrAuth = entry.credentials().address(); | ||
addrAuth.signatureExpirationLedger(validUntilLedgerSeq); | ||
|
||
const networkId = hash(Buffer.from(networkPassphrase)); | ||
const preimage = xdr.HashIdPreimage.envelopeTypeSorobanAuthorization( | ||
new xdr.HashIdPreimageSorobanAuthorization({ | ||
networkId, | ||
nonce: addrAuth.nonce(), | ||
invocation: entry.rootInvocation(), | ||
signatureExpirationLedger: addrAuth.signatureExpirationLedger(), | ||
}), | ||
); | ||
|
||
const signature = await signer(preimage.toXDR()); | ||
const publicKey = Address.fromScAddress(addrAuth.address()).toString(); | ||
|
||
if ( | ||
!Keypair.fromPublicKey(publicKey).verify(hash(preimage.toXDR()), signature) | ||
) { | ||
throw new Error(`signature doesn't match payload`); | ||
} | ||
|
||
const sigScVal = nativeToScVal( | ||
{ | ||
public_key: StrKey.decodeEd25519PublicKey(publicKey), | ||
signature, | ||
}, | ||
{ | ||
// force the keys to be interpreted as symbols (expected for | ||
// Soroban [contracttype]s) | ||
type: { | ||
public_key: ["symbol", null], | ||
signature: ["symbol", null], | ||
} as any, | ||
}, | ||
); | ||
|
||
addrAuth.signature(xdr.ScVal.scvVec([sigScVal])); | ||
return entry; | ||
} |
Oops, something went wrong.