Skip to content

Commit b46e97a

Browse files
Merge pull request #373 from autonomys/feat/improve-user-session
Feat: Improve user session
2 parents 057a595 + a80bde5 commit b46e97a

File tree

10 files changed

+114
-22
lines changed

10 files changed

+114
-22
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RPC_URL="https://auto-evm.taurus.autonomys.xyz/ws"
2+
PRIVATE_KEY=""
3+
4+
BLOCKSCOUT_API="https://blockscout.taurus.autonomys.xyz/api"
5+
CONTRACT_ADDRESS=""

packages/utility/user-session/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"typescript": "^5.4.5"
3232
},
3333
"dependencies": {
34+
"@autonomys/asynchronous": "^1.4.17",
3435
"@autonomys/auto-dag-data": "^1.4.17",
3536
"@autonomys/auto-drive": "^1.4.17",
3637
"ethers": "6.13.5"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.28;
3+
4+
import { Script } from "forge-std/Script.sol";
5+
import { UserSession } from "../src/UserSession.sol";
6+
import { console } from "forge-std/console.sol";
7+
8+
contract DeployScript is Script {
9+
function setUp() public {}
10+
11+
function run() public returns (UserSession) {
12+
// Log deployment info
13+
console.log("Deploying UserSession contract");
14+
console.log("Deployer address:", msg.sender);
15+
console.log("Chain ID:", block.chainid);
16+
17+
// Begin sending transactions
18+
vm.startBroadcast();
19+
20+
// Deploy UserSession contract
21+
UserSession userSession = new UserSession();
22+
console.log("UserSession deployed to:", address(userSession));
23+
24+
// Stop sending transactions
25+
vm.stopBroadcast();
26+
27+
return userSession;
28+
}
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
# Load environment variables from .env file
4+
if [ -f .env ]; then
5+
source .env
6+
else
7+
echo "Error: .env file not found in the contracts directory"
8+
exit 1
9+
fi
10+
11+
# Check if required environment variables are set
12+
if [ -z "$RPC_URL" ] || [ -z "$PRIVATE_KEY" ]; then
13+
echo "Error: RPC_URL and PRIVATE_KEY must be set in .env file"
14+
exit 1
15+
fi
16+
17+
forge script scripts/UserSession.s.sol:DeployScript \
18+
--rpc-url $RPC_URL \
19+
--private-key $PRIVATE_KEY \
20+
--evm-version london \
21+
--via-ir \
22+
--broadcast
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
# Load environment variables from .env file
4+
if [ -f .env ]; then
5+
source .env
6+
else
7+
echo "Error: .env file not found in the contracts directory"
8+
exit 1
9+
fi
10+
11+
# Check if required environment variables are set
12+
if [ -z "$CONTRACT_ADDRESS" ] || [ -z "$BLOCKSCOUT_API" ]; then
13+
echo "Error: CONTRACT_ADDRESS and BLOCKSCOUT_API must be set in .env file"
14+
exit 1
15+
fi
16+
17+
forge verify-contract \
18+
--verifier blockscout \
19+
--verifier-url $BLOCKSCOUT_API \
20+
--evm-version london --chain 490000 --compiler-version 0.8.28 \
21+
--watch \
22+
$CONTRACT_ADDRESS \
23+
src/UserSession.sol:UserSession

packages/utility/user-session/src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type ContractOptions = {
55
address: string
66
privateKey: string
77
rpcUrl: string
8+
waitReceipt?: boolean
89
}
910

1011
export type DataOptions = {
@@ -36,6 +37,7 @@ export type SaveDataParams<T> = {
3637
fileName?: string
3738
password?: string
3839
showLogs?: boolean
40+
waitReceipt?: boolean
3941
}
4042

4143
export type SaveDataResult = {

packages/utility/user-session/src/utils/data.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import { asyncIterableToBuffer } from '@autonomys/asynchronous'
23
import { GetDataParams, GetDataResult, SaveDataParams, SaveDataResult } from '../types'
34
import { userIdHash, userSessionCIDFromHash, userSessionCIDHash } from './hash'
45

@@ -13,7 +14,10 @@ const logger = (
1314
export const get = async <T>(params: GetDataParams): Promise<GetDataResult<T>> => {
1415
const { autoDriveApi, contract, userId } = params
1516
try {
16-
const userSession = await contract.getUserSession(userIdHash(userId))
17+
logger(params, 'userId:', userId)
18+
const hash = userIdHash(userId)
19+
logger(params, 'hash:', hash)
20+
const userSession = await contract.getUserSession(hash)
1721
logger(params, 'userSession:', userSession)
1822

1923
if (!userSession || userSession === '0x') {
@@ -26,20 +30,8 @@ export const get = async <T>(params: GetDataParams): Promise<GetDataResult<T>> =
2630

2731
logger(params, `Downloading file: ${cid}`)
2832
const stream = await autoDriveApi.downloadFile(cid, params.password)
29-
30-
const chunks: Uint8Array[] = []
31-
for await (const chunk of stream) {
32-
chunks.push(chunk)
33-
}
34-
35-
const allChunks = new Uint8Array(chunks.reduce((acc, chunk) => acc + chunk.length, 0))
36-
let position = 0
37-
for (const chunk of chunks) {
38-
allChunks.set(chunk, position)
39-
position += chunk.length
40-
}
41-
42-
const jsonString = new TextDecoder().decode(allChunks)
33+
const buffer = await asyncIterableToBuffer(stream)
34+
const jsonString = new TextDecoder().decode(buffer)
4335
const data = JSON.parse(jsonString)
4436
logger(params, 'data-decoded:', data)
4537

@@ -54,17 +46,28 @@ export const get = async <T>(params: GetDataParams): Promise<GetDataResult<T>> =
5446
}
5547

5648
export const save = async <T>(params: SaveDataParams<T>): Promise<SaveDataResult> => {
57-
const { autoDriveApi, contract, userId, data } = params
49+
const { autoDriveApi, contract, userId, data, waitReceipt } = params
5850

5951
const options = params.password ? { password: params.password } : undefined
6052
const cid = await autoDriveApi.uploadObjectAsJSON(data, params.fileName, options)
6153
logger(params, 'CID:', cid)
6254

63-
const tx = await contract.setUserSession(userIdHash(userId), userSessionCIDHash(cid))
55+
logger(params, 'userId:', userId)
56+
const hash = userIdHash(userId)
57+
logger(params, 'hash:', hash)
58+
59+
const cidHash = userSessionCIDHash(cid)
60+
logger(params, 'cidHash:', cidHash)
61+
62+
const tx = await contract.setUserSession(hash, cidHash)
6463
logger(params, 'userSession:', tx)
6564

66-
const txHash = await tx.wait()
67-
logger(params, 'txHash:', txHash)
65+
let txHash = tx.hash
66+
if (waitReceipt) {
67+
const txReceipt = await tx.wait()
68+
logger(params, 'txReceipt:', txReceipt)
69+
txHash = txReceipt.transactionHash
70+
}
6871

6972
return {
7073
cid,

packages/utility/user-session/src/utils/hash.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import { hexlify, keccak256, toUtf8Bytes, zeroPadValue } from 'ethers'
88

99
export const userIdHash = (userId: string) => keccak256(toUtf8Bytes(userId))
1010

11-
export const userSessionCIDHash = (CID: string) =>
12-
zeroPadValue(hexlify(blake3HashFromCid(stringToCid(CID))), 32)
11+
export const userSessionCIDHash = (CID: string) => {
12+
const blake3Hash = blake3HashFromCid(stringToCid(CID))
13+
return zeroPadValue(hexlify(blake3Hash), 32)
14+
}
1315

14-
export const userSessionCIDFromHash = (hash: Buffer) => cidToString(cidFromBlakeHash(hash))
16+
export const userSessionCIDFromHash = (hash: string) => {
17+
const hashBuffer = Buffer.from(hash.slice(2), 'hex')
18+
return cidToString(cidFromBlakeHash(hashBuffer))
19+
}

packages/utility/user-session/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const UserSession = <T>(options: UsersSessionOptions) => {
2525
fileName: options.fileName,
2626
password: options.password,
2727
showLogs: options.showLogs,
28+
waitReceipt: options.waitReceipt,
2829
})
2930

3031
return {

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ __metadata:
215215
version: 0.0.0-use.local
216216
resolution: "@autonomys/user-session@workspace:packages/utility/user-session"
217217
dependencies:
218+
"@autonomys/asynchronous": "npm:^1.4.17"
218219
"@autonomys/auto-dag-data": "npm:^1.4.17"
219220
"@autonomys/auto-drive": "npm:^1.4.17"
220221
eslint: "npm:^9.23.0"

0 commit comments

Comments
 (0)