Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The guide for this code example can be found [here](https://developer.litprotocol.com/v3/sdk/access-control/other-chains/stellar-access-control)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tasks": {
"bundle": "deno run --allow-read --allow-write --allow-env --allow-net --allow-run esbuild.js",
"test": "deno run --allow-net dist/litAction_simulate.js"
}
}
58 changes: 58 additions & 0 deletions lit-actions-conditions-reading-state-from-stellar/lit/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
globalThis.process = {
env: {},
};
15 changes: 15 additions & 0 deletions lit-actions-conditions-reading-state-from-stellar/lit/esbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as esbuild from "https://deno.land/x/esbuild@v0.20.1/mod.js";
import { denoPlugins } from "jsr:@luca/esbuild-deno-loader@^0.10.3";

esbuild.build({
plugins: [...denoPlugins()],
entryPoints: ["src/litAction_simulate.js"],
outdir: "dist/",
bundle: true,
platform: "browser",
format: "esm",
target: "esnext",
minify: false,
inject: ["./esbuild-shims.js"],
});
await esbuild.stop();
20 changes: 20 additions & 0 deletions lit-actions-conditions-reading-state-from-stellar/lit/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "la-acc-for-unsupported-chains",
"version": "0.1.0",
"description": "An example of using Lit Actions to provide access control using unsupported chains",
"license": "MIT",
"type": "module",
"scripts": {
"build": "deno task bundle"
},
"dependencies": {
"@kitsonk/xhr": "npm:@jsr/kitsonk__xhr@^0.4.3",
"@lit-protocol/lit-node-client-nodejs": "^5.0.0",
"@stellar/stellar-sdk": "^11.3.0",
"ethers": "^6.12.0",
"siwe": "^2.3.2"
},
"devDependencies": {
"esbuild": "^0.20.2"
}
}
97 changes: 97 additions & 0 deletions lit-actions-conditions-reading-state-from-stellar/lit/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import * as LitJsSdk from "@lit-protocol/lit-node-client-nodejs";
import { ethers } from "ethers";
import siwe from "siwe";

(async () => {
const client = new LitJsSdk.LitNodeClientNodeJs({
litNetwork: "habanero",
});
await client.connect();

const authSig = await getAuthSig(client);

const accessControlConditions = [
{
contractAddress: "ipfs://QmcyrxqaLSDjYZpxJUQ3521fUfnVr86bSvLHRZHiaPhMyY",
standardContractType: "LitAction",
chain: "ethereum",
method: "go",
parameters: ["42"],
returnValueTest: {
comparator: "=",
value: "true",
},
},
];

const { ciphertext, dataToEncryptHash } = await LitJsSdk.encryptString(
{
accessControlConditions,
authSig,
chain: "ethereum",
dataToEncrypt: "the answer to life, the universe, and everything is 42",
},
client
);

const decryptedString = await LitJsSdk.decryptToString(
{
accessControlConditions,
ciphertext,
dataToEncryptHash,
authSig,
chain: "ethereum",
},
client
);
console.log("decryptedString", decryptedString);
})();

async function getAuthSig(client) {
const wallet = getWallet();
const address = ethers.getAddress(await wallet.getAddress());
const messageToSign = (
await getSiweMessage(client, address)
).prepareMessage();
const signature = await wallet.signMessage(messageToSign);

return {
sig: signature,
derivedVia: "web3.eth.personal.sign",
signedMessage: messageToSign,
address,
};
}

function getPrivateKey() {
if (process.env.PRIVATE_KEY === undefined)
throw new Error("Please provide the env: PRIVATE_KEY");
return process.env.PRIVATE_KEY;
}

function getWallet() {
return new ethers.Wallet(getPrivateKey());
}

async function getSiweMessage(client, address) {
const domain = "localhost";
const origin = "https://localhost/login";
const statement =
"This is a test statement. You can put anything you want here.";

// Expiration time in ISO 8601 format. This is 7 days in the future
const expirationTime = new Date(
Date.now() + 1000 * 60 * 60 * 24 * 7
).toISOString();

return new siwe.SiweMessage({
domain,
address,
statement,
uri: origin,
version: "1",
chainId: 1,
nonce: await client.getLatestBlockhash(),
expirationTime,
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import "jsr:/@kitsonk/xhr";
import * as StellarSdk from "https://cdnjs.cloudflare.com/ajax/libs/stellar-sdk/11.3.0/stellar-sdk.js";

const go = async (number) => {
try {
const sourceKeypair = StellarSdk.Keypair.fromSecret(
"SCQN3XGRO65BHNSWLSHYIR4B65AHLDUQ7YLHGIWQ4677AZFRS77TCZRB"
);

const server = new StellarSdk.SorobanRpc.Server(
"https://soroban-testnet.stellar.org:443"
);

const contractAddress =
"CCIRVLI5WAHVPOU5FXHWPKVTMBCADQFXGJS4ACSUBKT55GCOPTGN5KPQ";
const contract = new StellarSdk.Contract(contractAddress);

const sourceAccount = await server.getAccount(sourceKeypair.publicKey());
let builtTransaction = new StellarSdk.TransactionBuilder(sourceAccount, {
fee: "100",
networkPassphrase: StellarSdk.Networks.TESTNET,
})
.addOperation(
contract.call(
"is_magic_number",
StellarSdk.nativeToScVal(parseInt(number), { type: "u32" })
)
)
.setTimeout(90)
.build();

let preparedTransaction = await server.prepareTransaction(builtTransaction);
preparedTransaction.sign(sourceKeypair);

let simulatedResponse = await server.simulateTransaction(
preparedTransaction
);

const parsedReturnVal = StellarSdk.scValToNative(
simulatedResponse.result.retval
);

console.log("Result", parsedReturnVal);
return parsedReturnVal;
} catch (e) {
console.log(e);
Lit.Actions.setResponse({ response: JSON.stringify(e) });
}
return false;
};

go();
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import "jsr:/@kitsonk/xhr";
import "https://cdnjs.cloudflare.com/ajax/libs/stellar-sdk/11.3.0/stellar-sdk.js";

const go = async () => {
try {
const sourceKeypair = StellarSdk.Keypair.fromSecret(
"SCQN3XGRO65BHNSWLSHYIR4B65AHLDUQ7YLHGIWQ4677AZFRS77TCZRB"
);

const server = new StellarSdk.SorobanRpc.Server(
"https://soroban-testnet.stellar.org:443"
);

const contractAddress =
"CCIRVLI5WAHVPOU5FXHWPKVTMBCADQFXGJS4ACSUBKT55GCOPTGN5KPQ";
const contract = new StellarSdk.Contract(contractAddress);

const sourceAccount = await server.getAccount(sourceKeypair.publicKey());
let builtTransaction = new StellarSdk.TransactionBuilder(sourceAccount, {
fee: "100",
networkPassphrase: StellarSdk.Networks.TESTNET,
})
.addOperation(contract.call("always_true"))
.setTimeout(90)
.build();

let preparedTransaction = await server.prepareTransaction(builtTransaction);
preparedTransaction.sign(sourceKeypair);

let sendResponse = await server.sendTransaction(preparedTransaction);
if (sendResponse.status === "PENDING") {
let getResponse = await server.getTransaction(sendResponse.hash);
while (getResponse.status === "NOT_FOUND") {
getResponse = await server.getTransaction(sendResponse.hash);
await new Promise((resolve) => setTimeout(resolve, 1000));
}

if (getResponse.status === "SUCCESS") {
if (!getResponse.resultMetaXdr) {
throw "Empty resultMetaXDR in getTransaction response";
}
let transactionMeta = getResponse.resultMetaXdr;
let returnValue = transactionMeta.v3().sorobanMeta().returnValue();

console.log(`Transaction result: ${returnValue.value()}`);
return returnValue.value();
} else {
throw `Transaction failed: ${getResponse.resultXdr}`;
}
} else {
throw sendResponse.errorResultXdr;
}
} catch (e) {
console.log(e);
Lit.Actions.setResponse({ response: JSON.stringify(e) });
}
return false;
};

go();
Loading