Skip to content

Instantly share code, notes, and snippets.

@aristidesstaffieri
Created July 20, 2023 19:01
Show Gist options
  • Save aristidesstaffieri/4534db641c9cfcff9733b474d1ae9121 to your computer and use it in GitHub Desktop.
Save aristidesstaffieri/4534db641c9cfcff9733b474d1ae9121 to your computer and use it in GitHub Desktop.
Soroban auth - Restore entries and bump expiration
const SorobanClient = require("soroban-client")
const LEDGER_COUNT_YEAR = 6311000 // a little under a year
const RPC_URL = "https://rpc-futurenet.stellar.org:443"
const NETWORK_PASSPHRASE = "Test SDF Future Network ; October 2022"
const FEE = "10000000"
const signer = SorobanClient.Keypair.fromSecret("S...")
// envelope from token deployment which has expired entries
const DEPLOY_XDR = "..." // xdr from an instance of a deployment to the expired entry
async function restoreExpiredEntries(type) {
// assume restore first
if (type !== 'restore' && type !== 'bump') {
type = 'restore'
}
const server = new SorobanClient.Server(RPC_URL, {
allowHttp: RPC_URL.startsWith("http://"),
});
const source = await server.getAccount(signer.publicKey());
const builder = new SorobanClient.TransactionBuilder(source, {
fee: FEE,
networkPassphrase: NETWORK_PASSPHRASE,
});
// simulate deployment, grab soroban data and attach footprint to bump op tx
const deployTx = SorobanClient.TransactionBuilder.fromXDR(DEPLOY_XDR, NETWORK_PASSPHRASE)
const { transactionData: deployTxXdr } = await server.simulateTransaction(deployTx);
const op = type === 'restore'
? SorobanClient.Operation.restoreFootprint()
: SorobanClient.Operation.bumpFootprintExpiration({ ledgersToExpire: LEDGER_COUNT_YEAR })
const tx = builder.addOperation(op).setTimeout(100);
const sorobanTxData = SorobanClient.xdr.SorobanTransactionData.fromXDR(
deployTxXdr,
"base64"
);
// put readWrite from deploy sim into readWrite of restore footprint, this is how it knows which entries
const extXDR = new SorobanClient.xdr.ExtensionPoint(0).toXDR("base64")
const restoreNewSorobanData = new SorobanClient.xdr.SorobanTransactionData({
ext: SorobanClient.xdr.ExtensionPoint.fromXDR(extXDR, "base64"),
refundableFee: new SorobanClient.xdr.Int64(1),
resources: new SorobanClient.xdr.SorobanResources({
footprint: new SorobanClient.xdr.LedgerFootprint({
readWrite: sorobanTxData.resources().footprint().readWrite(),
readOnly: [],
}),
instructions: 1,
readBytes: 1,
writeBytes: 1,
extendedMetaDataSizeBytes: 1,
})
})
tx.setSorobanData(restoreNewSorobanData)
const preparedTransaction = await server.prepareTransaction(
tx.build(),
NETWORK_PASSPHRASE,
);
preparedTransaction.sign(signer)
const sendResponse = await server.sendTransaction(preparedTransaction);
if (sendResponse.status === "PENDING") {
let txResponse = await server.getTransaction(sendResponse.hash);
// Poll this until the status is not "NOT_FOUND"
while (txResponse.status === "NOT_FOUND") {
// See if the transaction is complete
txResponse = await server.getTransaction(sendResponse.hash);
// Wait a second
await new Promise((resolve) => setTimeout(resolve, 1000));
}
console.dir(txResponse, { depth: null })
} else {
console.dir(sendResponse, { depth: null })
}
}
async function main() {
await restoreExpiredEntries()
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment