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

Updates soroban-client version, updates use of helpers #13

Merged
merged 7 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@
},
"dependencies": {
"@stellar/design-system": "^1.0.0-beta.12",
"@stellar/freighter-api": "^1.4.0",
"@stellar/freighter-api": "1.7.0",
"bignumber.js": "^9.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.14.1",
"soroban-client": "0.10.1",
"soroban-client": "1.0.0-beta.2",
"stellar-wallets-kit": "github:Creit-Tech/Stellar-Wallets-Kit"
}
}
22 changes: 14 additions & 8 deletions src/components/atomic-swap/exchange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
MemoType,
Operation,
SorobanDataBuilder,
SorobanRpc,
Transaction,
TransactionBuilder,
xdr,
Expand Down Expand Up @@ -160,7 +161,9 @@ export const Exchange = (props: ExchangeProps) => {
props.networkDetails.networkPassphrase,
) as Transaction<Memo<MemoType>, Operation[]>;

const txSim = await server.simulateTransaction(tx);
const txSim = (await server.simulateTransaction(
tx,
)) as SorobanRpc.SimulateTransactionSuccessResponse;
Copy link

Choose a reason for hiding this comment

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

as SorobanRpc.SimulateTransactionSuccessResponse

ballsy 😝

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah yeah lol fair call out I won't cast it 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed in 736cc1d


const preparedTransaction = assembleTransaction(
tx,
Expand All @@ -173,11 +176,9 @@ export const Exchange = (props: ExchangeProps) => {
"base64",
);

const finalTransaction = TransactionBuilder.cloneFrom(
preparedTransaction,
)
const finalTx = preparedTransaction
.setSorobanData(
new SorobanDataBuilder(txSim.transactionData)
new SorobanDataBuilder(txSim.transactionData.build())
.setFootprint(
originalFootprintXDR.readOnly(),
originalFootprintXDR.readWrite(),
Expand All @@ -187,7 +188,7 @@ export const Exchange = (props: ExchangeProps) => {
.build();

const _signedXdr = await signTx(
finalTransaction.toXDR(),
finalTx.toXDR(),
props.pubKey,
props.swkKit,
);
Expand All @@ -199,7 +200,9 @@ export const Exchange = (props: ExchangeProps) => {
server,
);

setTxResultXDR(result);
if (result) {
setTxResultXDR(result.toXDR().toString());
}
setIsSubmitting(false);

setStepCount((stepCount + 1) as StepCount);
Expand Down Expand Up @@ -294,7 +297,10 @@ export const Exchange = (props: ExchangeProps) => {
bc.postMessage({
type: ChannelMessageType.ContractID,
data: {
baseTx: preparedTransaction.toEnvelope().toXDR("base64"),
baseTx: preparedTransaction
.build()
.toEnvelope()
.toXDR("base64"),
contractID,
},
});
Expand Down
17 changes: 10 additions & 7 deletions src/helpers/network.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
StellarWalletsKit,
IStellarWalletsKitSignParams,
// IStellarWalletsKitSignParams,
} from "stellar-wallets-kit";

import { signAuthEntry } from "@stellar/freighter-api";

export interface NetworkDetails {
network: string;
networkUrl: string;
Expand All @@ -17,15 +19,16 @@ export const FUTURENET_DETAILS = {
};

export const signData = async (
blob: string,
entryXdr: string,
publicKey: string,
kit: StellarWalletsKit,
) => {
const { signedXDR } = await kit.sign({
blob,
publicKey,
} as any as IStellarWalletsKitSignParams);
return signedXDR;
// TODO: go back to using kit once auth entry PR lands
console.log(kit);
const signedEntry = await signAuthEntry(entryXdr, {
accountToSign: publicKey,
});
return signedEntry;
};

export const signTx = async (
Expand Down
67 changes: 67 additions & 0 deletions src/helpers/sign-auth-entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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;
}

/** @type {xdr.SorobanAddressCredentials} */
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(sigScVal);
return entry;
}
Loading