Skip to content

Commit 53fa214

Browse files
committed
work out the api endpoints
1 parent c314f57 commit 53fa214

File tree

11 files changed

+259
-50
lines changed

11 files changed

+259
-50
lines changed

.env.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PUBLIC_STELLAR_NETWORK=testnet
55
PUBLIC_STELLAR_ACCOUNT=stroopy
66

77
# These variables are useful for PasskeyKit and Launchtube
8-
PUBLIC_FACTORY_CONTRACT_ADDRESS=CCD7M4VVKELWL2RO4XJOZOGBDF3ESFIKG2EAU4ETVNAKMRRKE6YIQU5E
8+
PUBLIC_FACTORY_CONTRACT_ADDRESS=CD5QFGMCJ7CAL77GPTJ5MYABV6N3T4D7677S27ZOMC5B76BK4OBHJKKQ
99
PUBLIC_NATIVE_CONTRACT_ADDRESS=CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC
1010
PUBLIC_LAUNCHTUBE_URL="https://testnet.launchtube.xyz"
1111
PUBLIC_MERCURY_URL="https://api.mercurydata.app"

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@
4949
},
5050
"type": "module",
5151
"dependencies": {
52-
"@stellar/stellar-sdk": "13.0.0-rc.1",
52+
"@stellar/stellar-sdk": "13.0.0-beta.1",
5353
"hello_world": "file:./packages/hello_world",
54-
"passkey-kit": "~0.9.4",
54+
"passkey-kit": "^0.10.1",
5555
"svelte-persisted-store": "^0.12.0"
5656
},
5757
"pnpm": {

pnpm-lock.yaml

+40-40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/passkeyClient.ts

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { Server } from '@stellar/stellar-sdk/rpc';
2+
import { PasskeyKit, SACClient } from 'passkey-kit';
3+
4+
import {
5+
PUBLIC_STELLAR_RPC_URL,
6+
PUBLIC_STELLAR_NETWORK_PASSPHRASE,
7+
PUBLIC_FACTORY_CONTRACT_ADDRESS,
8+
PUBLIC_NATIVE_CONTRACT_ADDRESS,
9+
} from '$env/static/public';
10+
11+
/**
12+
* A configured Stellar RPC server instance used to interact with the network
13+
*/
14+
export const rpc = new Server(PUBLIC_STELLAR_RPC_URL);
15+
16+
/**
17+
* The account object is an instance of the PasskeyKit class. This is the
18+
* primary means of communicating with our user's smart wallet from the client.
19+
*/
20+
export const account = new PasskeyKit({
21+
rpcUrl: PUBLIC_STELLAR_RPC_URL,
22+
networkPassphrase: PUBLIC_STELLAR_NETWORK_PASSPHRASE,
23+
factoryContractId: PUBLIC_FACTORY_CONTRACT_ADDRESS,
24+
});
25+
26+
/**
27+
* A client allowing us to easily create SAC clients for any asset on the
28+
* network.
29+
*/
30+
const sac = new SACClient({
31+
rpcUrl: PUBLIC_STELLAR_RPC_URL,
32+
networkPassphrase: PUBLIC_STELLAR_NETWORK_PASSPHRASE,
33+
});
34+
35+
/**
36+
* A SAC client for the native XLM asset.
37+
*/
38+
export const native = sac.getSACClient(PUBLIC_NATIVE_CONTRACT_ADDRESS);
39+
40+
/**
41+
* A wrapper function so it's easier for our client-side code to access the
42+
* `/api/send` endpoint we have created.
43+
*
44+
* @param xdr - The base64-encoded, signed transaction. This transaction
45+
* **must** contain a Soroban operation
46+
* @returns JSON object containing the RPC's response
47+
*/
48+
export async function send(xdr: string) {
49+
return fetch('/api/send', {
50+
method: 'POST',
51+
body: JSON.stringify({
52+
xdr,
53+
}),
54+
}).then(async (res) => {
55+
if (res.ok) return res.json();
56+
else throw await res.text();
57+
});
58+
}
59+
60+
/**
61+
* A wrapper function so it's easier for our client-side code to access the
62+
* `/api/contract/[signer]` endpoint we have created.
63+
*
64+
* @param signer - The passkey ID we want to find an associated smart wallet for
65+
* @returns The contract address to which the specified signer has been added
66+
*/
67+
export async function getContractId(signer: string) {
68+
return fetch(`/api/contract/${signer}`).then(async (res) => {
69+
if (res.ok) return res.text();
70+
else throw await res.text();
71+
});
72+
}
73+
74+
/**
75+
* A wrapper function so it's easier for our client-side code to access the
76+
* `/api/fund/[address]` endpoint we have created.
77+
*
78+
* @param address - The contract address to fund on the Testnet
79+
*/
80+
export async function fundContract(address: string) {
81+
return fetch(`/api/fund/${address}`).then(async (res) => {
82+
if (res.ok) return res.json();
83+
else throw await res.text();
84+
});
85+
}

src/lib/server/passkeyServer.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { PasskeyServer } from 'passkey-kit';
2+
3+
import {
4+
PUBLIC_LAUNCHTUBE_URL,
5+
PUBLIC_MERCURY_URL,
6+
PUBLIC_STELLAR_RPC_URL,
7+
} from '$env/static/public';
8+
import { PRIVATE_LAUNCHTUBE_JWT, PRIVATE_MERCURY_TOKEN } from '$env/static/private';
9+
10+
export const server = new PasskeyServer({
11+
rpcUrl: PUBLIC_STELLAR_RPC_URL,
12+
launchtubeUrl: PUBLIC_LAUNCHTUBE_URL,
13+
launchtubeJwt: PRIVATE_LAUNCHTUBE_JWT,
14+
mercuryProjectName: 'smart-wallets-next-dima',
15+
mercuryUrl: PUBLIC_MERCURY_URL,
16+
mercuryKey: PRIVATE_MERCURY_TOKEN,
17+
});

src/routes/+page.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const load: PageLoad = async () => {
55
const { result } = await hello_world.hello({
66
to: 'SvelteKit Passkeys',
77
});
8-
console.log('result', result);
8+
99
return {
1010
greeting: result,
1111
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { server } from '$lib/server/passkeyServer';
2+
3+
import type { RequestHandler } from './$types';
4+
5+
/**
6+
* Perform a reverse-lookup for a smart wallet contract address, given a known
7+
* passkey ID, via a PasskeyServer instance.
8+
*
9+
* @remarks
10+
*
11+
* This API endpoint uses the Mercury indexer, and an accompanying Zephyr
12+
* program that will ingest and store add, remove, etc. events taking place on a
13+
* smart wallet contract.
14+
*
15+
* @param signer - The public key from the Passkey which should be searched for
16+
* @returns The contract address to which the specified signer has been added
17+
*/
18+
export const GET: RequestHandler = async ({ params }) => {
19+
const contractAddress = await server.getContractId({ keyId: params.signer });
20+
return new Response(contractAddress);
21+
};

0 commit comments

Comments
 (0)