Skip to content

Commit a219f01

Browse files
committed
Add pkp signing
1 parent 798b5fa commit a219f01

File tree

6 files changed

+137
-145
lines changed

6 files changed

+137
-145
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
ETHEREUM_PRIVATE_KEY=
21
STELLAR_SECRET=
3-
STELLAR_ACCOUNT_SEQUENCE_NUMBER=0
2+
STELLAR_ACCOUNT_SEQUENCE_NUMBER=0
3+
ETHEREUM_PRIVATE_KEY=
4+
LIT_ACTION_IPFS_CID=
5+
LIT_PKP_PUBLIC_KEY=
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# How to Run
2+
3+
1. `yarn`
4+
2. `cp .env.example .env` and fill out the ENVs
5+
3. `yarn start`

lit-access-control-conditions-stellar/nodejs/esbuild.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { denoPlugins } from "jsr:@luca/esbuild-deno-loader@^0.10.3";
33

44
esbuild.build({
55
plugins: [...denoPlugins()],
6-
entryPoints: ["src/litAction.ts"],
6+
entryPoints: ["src/litAction.js"],
77
outdir: "dist/",
88
bundle: true,
99
platform: "browser",

lit-access-control-conditions-stellar/nodejs/src/index.ts

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,42 @@ const getEnv = (name: string): string => {
1919
return env;
2020
};
2121

22-
const ETHEREUM_PRIVATE_KEY = getEnv("ETHEREUM_PRIVATE_KEY");
2322
const STELLAR_SECRET = getEnv("STELLAR_SECRET");
24-
// const LIT_PKP_PUBLIC_KEY = getEnv("LIT_PKP_PUBLIC_KEY");
2523
const STELLAR_ACCOUNT_SEQUENCE_NUMBER = getEnv(
2624
"STELLAR_ACCOUNT_SEQUENCE_NUMBER"
2725
);
26+
const ETHEREUM_PRIVATE_KEY = getEnv("ETHEREUM_PRIVATE_KEY");
27+
const LIT_ACTION_IPFS_CID = getEnv("LIT_ACTION_IPFS_CID");
28+
const LIT_PKP_PUBLIC_KEY = getEnv("LIT_PKP_PUBLIC_KEY");
2829

2930
let litNodeClient: LitNodeClientNodeJs | undefined = undefined;
3031

3132
try {
33+
const stellarKeyPair = StellarBase.Keypair.fromSecret(STELLAR_SECRET);
34+
const stellarAccount = new StellarBase.Account(
35+
stellarKeyPair.publicKey(),
36+
STELLAR_ACCOUNT_SEQUENCE_NUMBER
37+
);
38+
39+
const stellarAuthTx = new StellarBase.TransactionBuilder(stellarAccount, {
40+
fee: StellarBase.BASE_FEE,
41+
networkPassphrase: StellarBase.Networks.TESTNET,
42+
})
43+
.setTimeout(60 * 60 * 24) // 24 hours
44+
.build();
45+
stellarAuthTx.sign(stellarKeyPair);
46+
47+
litNodeClient = new LitNodeClientNodeJs({
48+
litNetwork: LitNetwork.Cayenne,
49+
});
50+
await litNodeClient.connect();
51+
3252
const ethersWallet = new Ethers.Wallet(
3353
ETHEREUM_PRIVATE_KEY,
3454
new Ethers.providers.JsonRpcProvider(
3555
"https://chain-rpc.litprotocol.com/http"
3656
)
3757
);
38-
litNodeClient = new LitNodeClientNodeJs({
39-
litNetwork: LitNetwork.Cayenne,
40-
});
41-
await litNodeClient.connect();
4258
const sessionSigs = await litNodeClient.getSessionSigs({
4359
chain: "ethereum",
4460
expiration: new Date(Date.now() + 1000 * 60 * 60 * 24).toISOString(), // 24 hours
@@ -74,37 +90,19 @@ try {
7490
});
7591
},
7692
});
77-
const stellarKeyPair = StellarBase.Keypair.fromSecret(STELLAR_SECRET);
78-
const stellarAccount = new StellarBase.Account(
79-
stellarKeyPair.publicKey(),
80-
STELLAR_ACCOUNT_SEQUENCE_NUMBER
81-
);
8293

83-
const stellarAuthTx = new StellarBase.TransactionBuilder(stellarAccount, {
84-
fee: StellarBase.BASE_FEE,
85-
networkPassphrase: StellarBase.Networks.TESTNET,
86-
})
87-
.setTimeout(60 * 60 * 24) // 24 hours
88-
.build();
89-
stellarAuthTx.sign(stellarKeyPair);
90-
const authTxSignature = stellarAuthTx.signatures[0];
91-
console.log({
92-
stellarPublicKey: stellarKeyPair.publicKey(),
93-
stellarAuthTxHash: stellarAuthTx.hash(),
94-
stellarAuthTxSignature: authTxSignature.signature(),
95-
});
96-
const litActionSignatures = await litNodeClient.executeJs({
94+
const litPkpSignature = await litNodeClient.executeJs({
9795
sessionSigs,
98-
ipfsId: "QmbyhacLykPWYP7fDa5cdhJgSFnQuKnpK57NTqqcD3dd3R",
96+
ipfsId: LIT_ACTION_IPFS_CID,
9997
jsParams: {
10098
stellarPublicKey: stellarKeyPair.publicKey(),
10199
stellarAuthTxHash: stellarAuthTx.hash(),
102-
stellarAuthTxSignature: authTxSignature.signature(),
103-
sourcePubkey: stellarKeyPair.publicKey(),
104-
sourceSequence: "0",
100+
stellarAuthTxSignature: stellarAuthTx.signatures[0].signature(),
101+
stellarAccountSequenceNumber: STELLAR_ACCOUNT_SEQUENCE_NUMBER,
102+
litPkpPublicKey: LIT_PKP_PUBLIC_KEY,
105103
},
106104
});
107-
console.log("litActionSignatures: ", litActionSignatures);
105+
console.log("litPkpSignature: ", litPkpSignature);
108106
} catch (error) {
109107
console.error(error);
110108
} finally {
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import "jsr:/@kitsonk/xhr";
2+
import * as StellarSdk from "https://cdnjs.cloudflare.com/ajax/libs/stellar-sdk/12.0.0-rc.2/stellar-sdk.js";
3+
4+
const ALLOW_LIST_CONTRACT_ADDRESS =
5+
"CAZEGBTBD7ZAL62YHHLS3W5EC2ZNVV2T32YGCQ6WRYAUF7O3EICW7UFF";
6+
const STELLAR_TESTNET_RPC_URL = "https://soroban-testnet.stellar.org";
7+
8+
class BufferShim extends Uint8Array {
9+
toJSON() {
10+
// Return an object similar to Node.js Buffer's .toJSON output
11+
return {
12+
type: "Buffer",
13+
data: Array.from(this),
14+
};
15+
}
16+
}
17+
18+
(async () => {
19+
const stellarKeyPair = StellarSdk.Keypair.fromPublicKey(stellarPublicKey);
20+
const signatureVerified = stellarKeyPair.verify(
21+
new BufferShim(stellarAuthTxHash),
22+
new BufferShim(stellarAuthTxSignature)
23+
);
24+
if (!signatureVerified) {
25+
LitActions.setResponse({
26+
response:
27+
"provided signature does not verify with provided stellarPublicKey and stellarAuthTxHash",
28+
});
29+
return;
30+
}
31+
32+
const stellarSenderAccount = new StellarSdk.Account(
33+
stellarPublicKey,
34+
stellarAccountSequenceNumber
35+
);
36+
const builtTransaction = new StellarSdk.TransactionBuilder(
37+
stellarSenderAccount,
38+
{
39+
fee: "100",
40+
networkPassphrase: StellarSdk.Networks.TESTNET,
41+
}
42+
)
43+
.addOperation(
44+
new StellarSdk.Contract(ALLOW_LIST_CONTRACT_ADDRESS).call(
45+
"is_allowed",
46+
StellarSdk.nativeToScVal(stellarPublicKey, {
47+
type: "address",
48+
})
49+
)
50+
)
51+
.setTimeout(90)
52+
.build();
53+
54+
const requestBody = {
55+
jsonrpc: "2.0",
56+
id: 42,
57+
method: "simulateTransaction",
58+
params: {
59+
transaction: builtTransaction.toXDR(),
60+
resourceConfig: {
61+
instructionLeeway: 3000000,
62+
},
63+
},
64+
};
65+
const result = await fetch(STELLAR_TESTNET_RPC_URL, {
66+
method: "POST",
67+
headers: {
68+
"Content-Type": "application/json",
69+
},
70+
body: JSON.stringify(requestBody),
71+
});
72+
73+
const xdrObject = StellarSdk.xdr.ScVal.fromXDR(
74+
(await result.json()).result.results[0].xdr,
75+
"base64"
76+
);
77+
const isAllowed = xdrObject.value();
78+
if (!isAllowed) {
79+
LitActions.setResponse({
80+
response: "provided Stellar address is not authorized",
81+
});
82+
return;
83+
}
84+
85+
// sigShare is a special variable that's automatically returned by the Lit Action
86+
const sigShare = await LitActions.signEcdsa({
87+
toSign: ethers.utils.arrayify(
88+
ethers.utils.keccak256(
89+
new TextEncoder().encode(`${stellarPublicKey} is authorized`)
90+
)
91+
),
92+
publicKey: litPkpPublicKey,
93+
sigName: "authorizationSignature",
94+
});
95+
96+
LitActions.setResponse({
97+
response: "provided Stellar address is authorized",
98+
});
99+
})();

lit-access-control-conditions-stellar/nodejs/src/litAction.ts

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)