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

p10 auth #4

Merged
merged 25 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
262012b
init repo, config files, basic deps and app
aristidesstaffieri Jun 29, 2023
f4e486d
Merge remote-tracking branch 'origin'
aristidesstaffieri Jul 5, 2023
b8cc875
Merge branch 'main' of github.com:stellar/soroban-react-atomic-swap
aristidesstaffieri Jul 6, 2023
068ecde
Merge branch 'main' of github.com:stellar/soroban-react-atomic-swap
aristidesstaffieri Jul 11, 2023
c1227af
updates to current p10 changes in sdk
aristidesstaffieri Jul 11, 2023
aa2be2f
migrates to p10 auth, updates tx helpers
aristidesstaffieri Jul 12, 2023
befb3e9
migrates to sdk helpers for sc val conversions
aristidesstaffieri Jul 12, 2023
b557338
use random nonce for perimage auth, clean up redundant helpers
aristidesstaffieri Jul 13, 2023
00316d3
cleans up formatter, adds entry nonce to auth preimage
aristidesstaffieri Jul 24, 2023
670c4ac
updates to use wallet signer
aristidesstaffieri Jul 24, 2023
bb9d818
swapper signs after tx detail confirmation
aristidesstaffieri Jul 25, 2023
7560c05
correctly build contract ID key
aristidesstaffieri Jul 25, 2023
8bea512
re-prepare tx after botho signers
aristidesstaffieri Jul 25, 2023
32fe7af
upgrade soroban version, correctly build signature args
aristidesstaffieri Jul 25, 2023
c3ee845
upgrade soroban version, correctly build signature args
aristidesstaffieri Jul 25, 2023
bdf5407
cleans up debug code, removes unused broadcast channel
aristidesstaffieri Jul 27, 2023
66a93ea
cleans up use of SC helpers
aristidesstaffieri Jul 27, 2023
bb107d7
moves tx construction to exchange tab
aristidesstaffieri Jul 28, 2023
648497d
adds ID notification for exchange tab
aristidesstaffieri Jul 28, 2023
d4aac17
adds step to set memo and tx fee
aristidesstaffieri Jul 28, 2023
37fef17
adds step to set memo and tx fee
aristidesstaffieri Jul 28, 2023
2897e8e
fixes bad cast to number types for amounts, swaps BigNumber with nati…
aristidesstaffieri Jul 28, 2023
bb81444
refactors helpers to use built in constants and better semantics
aristidesstaffieri Jul 28, 2023
2a4691a
removes invalid operations check
aristidesstaffieri Jul 28, 2023
b81efce
removes invalid operations check
aristidesstaffieri Jul 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/helpers/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export const ERRORS = {
UNABLE_TO_SIGN_TX: "Unable to sign transaction",
WALLET_CONNECTION_REJECTED: "Wallet connection rejected",
BAD_ENVELOPE: "Unable to unpack envelope",
CANNOT_FETCH_LEDGER_ENTRY: "Unable to fetch ledger entry",
};
172 changes: 81 additions & 91 deletions src/helpers/soroban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,99 +206,89 @@ export const buildContractAuth = async (
) => {
const signedAuthEntries = [];

if (authEntries.length) {
for (const entry of authEntries) {
if (entry.credentials().switch().name === "sorobanCredentialsAddress") {
const entryAddress = entry
.credentials()
.address()
.address()
.accountId();
const entryNonce = entry.credentials().address().nonce();
const signerKeyPair = Keypair.fromPublicKey(signerPubKey);

if (
signerKeyPair.xdrPublicKey().toXDR("hex") ===
entryAddress.toXDR("hex")
) {
let expirationLedgerSeq = 0;

const key = xdr.LedgerKey.contractData(
new xdr.LedgerKeyContractData({
contract: new Contract(contractID).address().toScAddress(),
key: xdr.ScVal.scvLedgerKeyContractInstance(),
durability: xdr.ContractDataDurability.persistent(),
bodyType: xdr.ContractEntryBodyType.dataEntry(),
}),
);

// Fetch the current contract ledger seq
// eslint-disable-next-line no-await-in-loop
const entryRes = await server.getLedgerEntries([key]);
if (entryRes.entries && entryRes.entries.length) {
const parsed = xdr.LedgerEntryData.fromXDR(
entryRes.entries[0].xdr,
"base64",
);
expirationLedgerSeq = parsed.contractData().expirationLedgerSeq();
}

const passPhraseHash = hash(Buffer.from(networkPassphrase));
const invocation = entry.rootInvocation();
const hashIDPreimageAuth = new xdr.HashIdPreimageSorobanAuthorization(
{
networkId: Buffer.from(passPhraseHash).subarray(0, 32),
invocation,
nonce: entryNonce,
signatureExpirationLedger: expirationLedgerSeq,
},
);

const preimage =
xdr.HashIdPreimage.envelopeTypeSorobanAuthorization(
hashIDPreimageAuth,
);
const preimageHash = hash(preimage.toXDR("raw"));

// eslint-disable-next-line no-await-in-loop
const signature = (await signTx(
preimageHash.toString("base64"),
signerPubKey,
kit,
)) as any as { data: number[] }; // not a string in this instance

const authEntry = new xdr.SorobanAuthorizationEntry({
credentials: xdr.SorobanCredentials.sorobanCredentialsAddress(
new xdr.SorobanAddressCredentials({
address: new Address(signerPubKey).toScAddress(),
nonce: hashIDPreimageAuth.nonce(),
signatureExpirationLedger:
hashIDPreimageAuth.signatureExpirationLedger(),
signatureArgs: [
nativeToScVal(
{
public_key: StrKey.decodeEd25519PublicKey(signerPubKey),
signature: new Uint8Array(signature.data),
},
{
type: {
public_key: ["symbol", null],
signature: ["symbol", null],
},
} as any,
),
],
}),
),
rootInvocation: invocation,
});
signedAuthEntries.push(authEntry);
} else {
signedAuthEntries.push(entry);
}
for (const entry of authEntries) {
const entryAddress = entry.credentials().address().address().accountId();
const entryNonce = entry.credentials().address().nonce();
const signerKeyPair = Keypair.fromPublicKey(signerPubKey);

if (
entry.credentials().switch() ===
xdr.SorobanCredentialsType.sorobanCredentialsAddress() &&
signerKeyPair.publicKey() ===
Shaptic marked this conversation as resolved.
Show resolved Hide resolved
StrKey.encodeEd25519PublicKey(entryAddress.ed25519())
) {
let expirationLedgerSeq = 0;

const key = xdr.LedgerKey.contractData(
new xdr.LedgerKeyContractData({
contract: new Address(contractID).toScAddress(),
key: xdr.ScVal.scvLedgerKeyContractInstance(),
durability: xdr.ContractDataDurability.persistent(),
bodyType: xdr.ContractEntryBodyType.dataEntry(),
}),
);

// Fetch the current contract ledger seq
// eslint-disable-next-line no-await-in-loop
const entryRes = await server.getLedgerEntries([key]);
if (entryRes.entries && entryRes.entries.length) {
const parsed = xdr.LedgerEntryData.fromXDR(
entryRes.entries[0].xdr,
"base64",
);
expirationLedgerSeq = parsed.contractData().expirationLedgerSeq();
aristidesstaffieri marked this conversation as resolved.
Show resolved Hide resolved
} else {
signedAuthEntries.push(entry);
throw new Error(ERRORS.CANNOT_FETCH_LEDGER_ENTRY);
}

const passPhraseHash = hash(Buffer.from(networkPassphrase));
const invocation = entry.rootInvocation();
const hashIDPreimageAuth = new xdr.HashIdPreimageSorobanAuthorization({
networkId: Buffer.from(passPhraseHash).subarray(0, 32),
invocation,
nonce: entryNonce,
signatureExpirationLedger: expirationLedgerSeq,
});

const preimage =
xdr.HashIdPreimage.envelopeTypeSorobanAuthorization(hashIDPreimageAuth);
const preimageHash = hash(preimage.toXDR());

// eslint-disable-next-line no-await-in-loop
const signature = (await signTx(
preimageHash.toString("base64"),
signerPubKey,
kit,
)) as any as { data: number[] }; // not a string in this instance

const authEntry = new xdr.SorobanAuthorizationEntry({
credentials: xdr.SorobanCredentials.sorobanCredentialsAddress(
new xdr.SorobanAddressCredentials({
address: new Address(signerPubKey).toScAddress(),
nonce: hashIDPreimageAuth.nonce(),
signatureExpirationLedger:
hashIDPreimageAuth.signatureExpirationLedger(),
signatureArgs: [
nativeToScVal(
{
public_key: StrKey.decodeEd25519PublicKey(signerPubKey),
signature: new Uint8Array(signature.data),
},
{
type: {
public_key: ["symbol", null],
signature: ["symbol", null],
},
} as any,
),
],
}),
),
rootInvocation: invocation,
});
signedAuthEntries.push(authEntry);
} else {
signedAuthEntries.push(entry);
}
}

Expand Down