@fangorn-network/sdk 2026.3.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +20 -0
- package/README.md +276 -0
- package/lib/builders/a2aCardBuilder.d.ts +28 -0
- package/lib/builders/a2aCardBuilder.js +104 -0
- package/lib/cli/cli.d.ts +2 -0
- package/lib/cli/cli.js +500 -0
- package/lib/cli/index.d.ts +738 -0
- package/lib/cli/index.js +36 -0
- package/lib/cli/registry.d.ts +29 -0
- package/lib/cli/registry.js +53 -0
- package/lib/config.d.ts +761 -0
- package/lib/config.js +46 -0
- package/lib/deployContract.d.ts +23 -0
- package/lib/deployContract.js +78 -0
- package/lib/fangorn.d.ts +59 -0
- package/lib/fangorn.js +145 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +11 -0
- package/lib/interface/datasource-registry/abi.d.ts +97 -0
- package/lib/interface/datasource-registry/abi.js +110 -0
- package/lib/interface/datasource-registry/dataSourceRegistry.d.ts +24 -0
- package/lib/interface/datasource-registry/dataSourceRegistry.js +81 -0
- package/lib/interface/index.d.ts +3 -0
- package/lib/interface/index.js +4 -0
- package/lib/interface/manifest.d.ts +7 -0
- package/lib/interface/manifest.js +10 -0
- package/lib/interface/settlement-tracker/abi.d.ts +26 -0
- package/lib/interface/settlement-tracker/abi.js +88 -0
- package/lib/interface/settlement-tracker/settlementTracker.d.ts +32 -0
- package/lib/interface/settlement-tracker/settlementTracker.js +64 -0
- package/lib/modules/encryption/actions.d.ts +4 -0
- package/lib/modules/encryption/actions.js +18 -0
- package/lib/modules/encryption/aes.d.ts +10 -0
- package/lib/modules/encryption/aes.js +51 -0
- package/lib/modules/encryption/index.d.ts +27 -0
- package/lib/modules/encryption/index.js +3 -0
- package/lib/modules/encryption/lit.d.ts +36 -0
- package/lib/modules/encryption/lit.js +156 -0
- package/lib/modules/encryption/rand.d.ts +5 -0
- package/lib/modules/encryption/rand.js +16 -0
- package/lib/modules/encryption/types.d.ts +55 -0
- package/lib/modules/encryption/types.js +1 -0
- package/lib/modules/gadgets/acc/gadgets.d.ts +7 -0
- package/lib/modules/gadgets/acc/gadgets.js +10 -0
- package/lib/modules/gadgets/acc.d.ts +15 -0
- package/lib/modules/gadgets/acc.js +24 -0
- package/lib/modules/gadgets/builder.d.ts +1 -0
- package/lib/modules/gadgets/builder.js +1 -0
- package/lib/modules/gadgets/index.d.ts +2 -0
- package/lib/modules/gadgets/index.js +1 -0
- package/lib/modules/gadgets/payment.d.ts +23 -0
- package/lib/modules/gadgets/payment.js +88 -0
- package/lib/modules/gadgets/types.d.ts +15 -0
- package/lib/modules/gadgets/types.js +1 -0
- package/lib/providers/storage/index.d.ts +10 -0
- package/lib/providers/storage/index.js +3 -0
- package/lib/providers/storage/pinata/index.d.ts +12 -0
- package/lib/providers/storage/pinata/index.js +26 -0
- package/lib/providers/storage/s3/index.d.ts +1 -0
- package/lib/providers/storage/s3/index.js +1 -0
- package/lib/test/index.d.ts +7 -0
- package/lib/test/index.js +25 -0
- package/lib/test/test-gadget.d.ts +7 -0
- package/lib/test/test-gadget.js +10 -0
- package/lib/test/testbed.d.ts +43 -0
- package/lib/test/testbed.js +174 -0
- package/lib/types/index.d.ts +43 -0
- package/lib/types/index.js +12 -0
- package/lib/utils/index.d.ts +12 -0
- package/lib/utils/index.js +43 -0
- package/package.json +83 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/modules/gadgets/acc.ts
|
|
2
|
+
var AccGadget = class {
|
|
3
|
+
type = "acc";
|
|
4
|
+
constructor(acc, description) {
|
|
5
|
+
this.acc = acc;
|
|
6
|
+
this.description = description;
|
|
7
|
+
}
|
|
8
|
+
toLitAction() {
|
|
9
|
+
return ``;
|
|
10
|
+
}
|
|
11
|
+
async toAccessCondition() {
|
|
12
|
+
return this.acc;
|
|
13
|
+
}
|
|
14
|
+
toDescriptor() {
|
|
15
|
+
return {
|
|
16
|
+
type: this.type,
|
|
17
|
+
description: this.description,
|
|
18
|
+
acc: this.acc
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
export { AccGadget };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Gadget, GadgetDescriptor } from "./types.js";
|
|
2
|
+
import { Address, Hex } from "viem";
|
|
3
|
+
|
|
4
|
+
//#region src/modules/gadgets/payment.d.ts
|
|
5
|
+
interface PaymentGadgetParams {
|
|
6
|
+
usdcPrice: string;
|
|
7
|
+
commitment: Hex;
|
|
8
|
+
chainName: string;
|
|
9
|
+
settlementTrackerContractAddress: Address;
|
|
10
|
+
pinataJwt: String;
|
|
11
|
+
}
|
|
12
|
+
declare class PaymentGadget implements Gadget {
|
|
13
|
+
private params;
|
|
14
|
+
readonly type = "payment";
|
|
15
|
+
private litActionCid;
|
|
16
|
+
constructor(params: PaymentGadgetParams);
|
|
17
|
+
toLitAction(): string;
|
|
18
|
+
toAccessCondition(): Promise<any>;
|
|
19
|
+
toDescriptor(): GadgetDescriptor;
|
|
20
|
+
private toLitActionIpfsHash;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { PaymentGadget, PaymentGadgetParams };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { createAccBuilder } from "@lit-protocol/access-control-conditions";
|
|
2
|
+
|
|
3
|
+
//#region src/modules/gadgets/payment.ts
|
|
4
|
+
var PaymentGadget = class {
|
|
5
|
+
type = "payment";
|
|
6
|
+
litActionCid = null;
|
|
7
|
+
constructor(params) {
|
|
8
|
+
this.params = params;
|
|
9
|
+
}
|
|
10
|
+
toLitAction() {
|
|
11
|
+
return `
|
|
12
|
+
const arbitrumSepolia = "https://sepolia-rollup.arbitrum.io/rpc";
|
|
13
|
+
const baseSepolia = "https://sepolia.base.org";
|
|
14
|
+
|
|
15
|
+
const go = async (supportedNetwork, paywallAddress, commitment, price) => {
|
|
16
|
+
let rpcUrl = baseSepolia;
|
|
17
|
+
if (supportedNetwork == "arbitrumSepolia") rpcUrl = arbitrumSepolia;
|
|
18
|
+
else if (supportedNetwork == "baseSepolia") rpcUrl = baseSepolia;
|
|
19
|
+
else {
|
|
20
|
+
throw new Error(
|
|
21
|
+
'Unsupported network.Choose a supported network in the list ["arbitrumSepolia", "baseSepolia"].'
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const callerAddress = Lit.Auth.authSigAddress;
|
|
26
|
+
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
|
|
27
|
+
|
|
28
|
+
const paywallAbi = [
|
|
29
|
+
"function checkSettlement(bytes32 commitment, address user) external view returns (uint256)",
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
const paywall = new ethers.Contract(paywallAddress, paywallAbi, provider);
|
|
33
|
+
|
|
34
|
+
const paidAmount = await paywall.checkSettlement(commitment, callerAddress);
|
|
35
|
+
|
|
36
|
+
if (paidAmount < price) {
|
|
37
|
+
Lit.Actions.setResponse({ success: false, response: "goodbye" });
|
|
38
|
+
throw new Error("x402: Payment Required. " + paidAmount + " < " + price + " with commitment " + commitment );
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
Lit.Actions.setResponse({ response: true.toString() });
|
|
42
|
+
|
|
43
|
+
return true.toString()
|
|
44
|
+
};
|
|
45
|
+
`;
|
|
46
|
+
}
|
|
47
|
+
async toAccessCondition() {
|
|
48
|
+
return createAccBuilder().requireLitAction(await this.toLitActionIpfsHash(), "go", [
|
|
49
|
+
this.params.chainName,
|
|
50
|
+
this.params.settlementTrackerContractAddress,
|
|
51
|
+
this.params.commitment,
|
|
52
|
+
this.params.usdcPrice
|
|
53
|
+
], "true").build();
|
|
54
|
+
}
|
|
55
|
+
toDescriptor() {
|
|
56
|
+
return {
|
|
57
|
+
type: this.type,
|
|
58
|
+
description: "x402: Payment Required",
|
|
59
|
+
acc: this.toAccessCondition(),
|
|
60
|
+
params: {
|
|
61
|
+
price: this.params.usdcPrice,
|
|
62
|
+
token: "USDC"
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
async toLitActionIpfsHash() {
|
|
67
|
+
const content = this.toLitAction();
|
|
68
|
+
const name = "lit-action-payment-gadget-v1";
|
|
69
|
+
const form = new FormData();
|
|
70
|
+
form.append("file", new Blob([content], { type: "text/plain" }), name);
|
|
71
|
+
form.append("pinataOptions", JSON.stringify({ cidVersion: 0 }));
|
|
72
|
+
form.append("pinataMetadata", JSON.stringify({ name }));
|
|
73
|
+
const res = await fetch("https://api.pinata.cloud/pinning/pinFileToIPFS", {
|
|
74
|
+
method: "POST",
|
|
75
|
+
headers: { Authorization: `Bearer ${this.params.pinataJwt}` },
|
|
76
|
+
body: form
|
|
77
|
+
});
|
|
78
|
+
if (!res.ok) {
|
|
79
|
+
const text = await res.text();
|
|
80
|
+
throw new Error(`Pinata upload failed: ${res.status} - ${text}`);
|
|
81
|
+
}
|
|
82
|
+
this.litActionCid = (await res.json()).IpfsHash;
|
|
83
|
+
return this.litActionCid;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
export { PaymentGadget };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/modules/gadgets/types.d.ts
|
|
2
|
+
interface Gadget {
|
|
3
|
+
type: string;
|
|
4
|
+
toLitAction(): string;
|
|
5
|
+
toAccessCondition(): any;
|
|
6
|
+
toDescriptor(): GadgetDescriptor;
|
|
7
|
+
}
|
|
8
|
+
interface GadgetDescriptor {
|
|
9
|
+
type: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
acc?: any;
|
|
12
|
+
params?: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { Gadget, GadgetDescriptor };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { PinataStorage } from "./pinata/index.js";
|
|
2
|
+
|
|
3
|
+
//#region src/providers/storage/index.d.ts
|
|
4
|
+
interface StorageProvider<T> {
|
|
5
|
+
store(data: T, metadata?: Record<string, unknown>): Promise<string>;
|
|
6
|
+
retrieve(cid: string): Promise<T>;
|
|
7
|
+
delete?(cid: string): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
//#endregion
|
|
10
|
+
export { PinataStorage, StorageProvider as default };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import StorageProvider from "../index.js";
|
|
2
|
+
|
|
3
|
+
//#region src/providers/storage/pinata/index.d.ts
|
|
4
|
+
declare class PinataStorage implements StorageProvider<any> {
|
|
5
|
+
private pinata;
|
|
6
|
+
constructor(pinataJwt: string, pinataGateway: string);
|
|
7
|
+
store(data: any, metadata?: Record<string, unknown>): Promise<string>;
|
|
8
|
+
retrieve(cid: string): Promise<string | JSON | Blob>;
|
|
9
|
+
delete(cid: string): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { PinataStorage };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { PinataSDK } from "pinata";
|
|
2
|
+
|
|
3
|
+
//#region src/providers/storage/pinata/index.ts
|
|
4
|
+
var PinataStorage = class {
|
|
5
|
+
pinata;
|
|
6
|
+
constructor(pinataJwt, pinataGateway) {
|
|
7
|
+
this.pinata = new PinataSDK({
|
|
8
|
+
pinataJwt,
|
|
9
|
+
pinataGateway
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
async store(data, metadata) {
|
|
13
|
+
const content = typeof data === "string" ? data : JSON.stringify(data);
|
|
14
|
+
const file = new File([content], metadata?.name ?? "file", { type: "text/plain" });
|
|
15
|
+
return (await this.pinata.upload.public.file(file, { metadata })).cid;
|
|
16
|
+
}
|
|
17
|
+
async retrieve(cid) {
|
|
18
|
+
return (await this.pinata.gateways.public.get(cid)).data;
|
|
19
|
+
}
|
|
20
|
+
async delete(cid) {
|
|
21
|
+
await this.pinata.files.public.delete([cid]);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { PinataStorage };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region src/test/index.ts
|
|
2
|
+
/**
|
|
3
|
+
* Uploads any data to IPFS via Pinata
|
|
4
|
+
*/
|
|
5
|
+
async function uploadToPinata(name, data, pinataJwt) {
|
|
6
|
+
const jwt = pinataJwt || process.env.PINATA_JWT;
|
|
7
|
+
if (!jwt) throw new Error("PINATA_JWT is required");
|
|
8
|
+
const content = typeof data === "object" && !(data instanceof Blob) ? JSON.stringify(data) : data;
|
|
9
|
+
const contentType = typeof data === "object" ? "application/json" : "text/plain";
|
|
10
|
+
const form = new FormData();
|
|
11
|
+
form.append("file", new Blob([content], { type: contentType }), name);
|
|
12
|
+
const response = await fetch("https://api.pinata.cloud/pinning/pinFileToIPFS", {
|
|
13
|
+
method: "POST",
|
|
14
|
+
headers: { Authorization: `Bearer ${jwt}` },
|
|
15
|
+
body: form
|
|
16
|
+
});
|
|
17
|
+
if (!response.ok) {
|
|
18
|
+
const text = await response.text();
|
|
19
|
+
throw new Error(`Upload failed: ${response.status} - ${text}`);
|
|
20
|
+
}
|
|
21
|
+
return (await response.json()).IpfsHash;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
export { uploadToPinata };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AccGadget } from "../modules/gadgets/acc.js";
|
|
2
|
+
import { createAccBuilder } from "@lit-protocol/access-control-conditions";
|
|
3
|
+
|
|
4
|
+
//#region src/test/test-gadget.ts
|
|
5
|
+
function emptyWallet(chain) {
|
|
6
|
+
return new AccGadget(createAccBuilder().requireEthBalance("0", "=").on(chain).build(), "Caller must have zero ETH balance");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
//#endregion
|
|
10
|
+
export { emptyWallet };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { AppConfig } from "../config.js";
|
|
2
|
+
import { Filedata } from "../types/index.js";
|
|
3
|
+
import { PinataStorage } from "../providers/storage/pinata/index.js";
|
|
4
|
+
import { Fangorn } from "../fangorn.js";
|
|
5
|
+
import { Address, Hex, WalletClient } from "viem";
|
|
6
|
+
import { EvmChain } from "@lit-protocol/access-control-conditions";
|
|
7
|
+
|
|
8
|
+
//#region src/test/testbed.d.ts
|
|
9
|
+
declare class TestBed {
|
|
10
|
+
private delegatorFangorn;
|
|
11
|
+
private delegateeFangorn;
|
|
12
|
+
private delegatorAddress;
|
|
13
|
+
private storage;
|
|
14
|
+
private usdcContractAddress;
|
|
15
|
+
private vaultIds;
|
|
16
|
+
private config;
|
|
17
|
+
private litChain;
|
|
18
|
+
constructor(delegatorAddress: Address, delegatorFangorn: Fangorn, delegateeFangorn: Fangorn, storage: PinataStorage, config: AppConfig, litChain: EvmChain, usdcContractAddress: Address);
|
|
19
|
+
static init(delegatorWalletClient: WalletClient, delegateeWalletClient: WalletClient, jwt: string, gateway: string, dataSourceRegistryContractAddress: Hex, usdcContractAddress: Hex, rpcUrl: string, chain: string, litChain: EvmChain, caip2: number): Promise<TestBed>;
|
|
20
|
+
registerDatasource(name: string): Promise<Hex>;
|
|
21
|
+
/**
|
|
22
|
+
* Upload files with empty wallet gadget
|
|
23
|
+
*/
|
|
24
|
+
fileUploadEmptyWallet(datasourceName: string, filedata: Filedata[]): Promise<string>;
|
|
25
|
+
fileUploadPaymentGadget(datasourceName: string, filedata: Filedata, usdcPrice: string, settlementTrackerContractAddress: Address, jwt: string): Promise<string>;
|
|
26
|
+
tryDecrypt(owner: Address, name: string, tag: string): Promise<Uint8Array>;
|
|
27
|
+
tryDecryptDelegator(owner: Address, name: string, tag: string): Promise<Uint8Array>;
|
|
28
|
+
checkDatasourceRegistryExistence(who: Address, name: string): Promise<boolean>;
|
|
29
|
+
checkDataExistence(who: Address, name: string, tag: string): Promise<void>;
|
|
30
|
+
buildUsdcAuthorization(recipient: Address, amount: string, chainId: number, usdcContractName: string, usdcAddress: Address): Promise<{
|
|
31
|
+
from: `0x${string}`;
|
|
32
|
+
to: `0x${string}`;
|
|
33
|
+
amount: bigint;
|
|
34
|
+
validAfter: bigint;
|
|
35
|
+
validBefore: bigint;
|
|
36
|
+
nonce: `0x${string}`;
|
|
37
|
+
signature: `0x${string}`;
|
|
38
|
+
}>;
|
|
39
|
+
payForFile(owner: Address, name: string, tag: string, amount: string, usdcDomainName: string, settlementTrackerAddress: Address, walletClient: WalletClient, rpcUrl: string): Promise<void>;
|
|
40
|
+
private parseSignature;
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
export { TestBed };
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { Fangorn } from "../fangorn.js";
|
|
2
|
+
import { PinataStorage } from "../providers/storage/pinata/index.js";
|
|
3
|
+
import { computeTagCommitment, fieldToHex } from "../utils/index.js";
|
|
4
|
+
import { LitEncryptionService } from "../modules/encryption/lit.js";
|
|
5
|
+
import { PaymentGadget } from "../modules/gadgets/payment.js";
|
|
6
|
+
import { emptyWallet } from "./test-gadget.js";
|
|
7
|
+
import { SettlementTracker } from "../interface/settlement-tracker/settlementTracker.js";
|
|
8
|
+
import { arbitrumSepolia, baseSepolia } from "viem/chains";
|
|
9
|
+
import { createPublicClient, http, keccak256, parseUnits, toHex } from "viem";
|
|
10
|
+
|
|
11
|
+
//#region src/test/testbed.ts
|
|
12
|
+
var TestBed = class TestBed {
|
|
13
|
+
delegatorFangorn;
|
|
14
|
+
delegateeFangorn;
|
|
15
|
+
delegatorAddress;
|
|
16
|
+
storage;
|
|
17
|
+
usdcContractAddress;
|
|
18
|
+
vaultIds;
|
|
19
|
+
config;
|
|
20
|
+
litChain;
|
|
21
|
+
constructor(delegatorAddress, delegatorFangorn, delegateeFangorn, storage, config, litChain, usdcContractAddress) {
|
|
22
|
+
this.delegatorAddress = delegatorAddress;
|
|
23
|
+
this.delegatorFangorn = delegatorFangorn;
|
|
24
|
+
this.delegateeFangorn = delegateeFangorn;
|
|
25
|
+
this.vaultIds = /* @__PURE__ */ new Map();
|
|
26
|
+
this.config = config;
|
|
27
|
+
this.litChain = litChain;
|
|
28
|
+
this.storage = storage;
|
|
29
|
+
this.usdcContractAddress = usdcContractAddress;
|
|
30
|
+
}
|
|
31
|
+
static async init(delegatorWalletClient, delegateeWalletClient, jwt, gateway, dataSourceRegistryContractAddress, usdcContractAddress, rpcUrl, chain, litChain, caip2) {
|
|
32
|
+
let chainImpl = arbitrumSepolia;
|
|
33
|
+
if (chain === "baseSepolia") chainImpl = baseSepolia;
|
|
34
|
+
const config = {
|
|
35
|
+
dataSourceRegistryContractAddress,
|
|
36
|
+
chainName: chain,
|
|
37
|
+
chain: chainImpl,
|
|
38
|
+
rpcUrl,
|
|
39
|
+
caip2
|
|
40
|
+
};
|
|
41
|
+
const delegatorEncryption = await LitEncryptionService.init(chain);
|
|
42
|
+
const delegateeEncryption = await LitEncryptionService.init(chain);
|
|
43
|
+
const delegatorStorage = new PinataStorage(jwt, gateway);
|
|
44
|
+
const delegateeStorage = new PinataStorage(jwt, gateway);
|
|
45
|
+
const domain = "localhost";
|
|
46
|
+
const delegatorFangorn = await Fangorn.init(delegatorWalletClient, delegatorStorage, delegatorEncryption, domain, config);
|
|
47
|
+
const delegateeFangorn = await Fangorn.init(delegateeWalletClient, delegateeStorage, delegateeEncryption, domain, config);
|
|
48
|
+
return new TestBed(delegatorWalletClient.account.address, delegatorFangorn, delegateeFangorn, delegatorStorage, config, litChain, usdcContractAddress);
|
|
49
|
+
}
|
|
50
|
+
async registerDatasource(name) {
|
|
51
|
+
const existing = this.vaultIds.get(name);
|
|
52
|
+
if (existing) return existing;
|
|
53
|
+
const id = await this.delegatorFangorn.registerDataSource(name, "");
|
|
54
|
+
this.vaultIds.set(name, id);
|
|
55
|
+
return id;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Upload files with empty wallet gadget
|
|
59
|
+
*/
|
|
60
|
+
async fileUploadEmptyWallet(datasourceName, filedata) {
|
|
61
|
+
return await this.delegatorFangorn.upload(datasourceName, filedata, (_file) => emptyWallet(this.litChain));
|
|
62
|
+
}
|
|
63
|
+
async fileUploadPaymentGadget(datasourceName, filedata, usdcPrice, settlementTrackerContractAddress, jwt) {
|
|
64
|
+
return await this.delegatorFangorn.upload(datasourceName, [filedata], async (file) => {
|
|
65
|
+
return new PaymentGadget({
|
|
66
|
+
commitment: fieldToHex(await computeTagCommitment(this.delegatorAddress, datasourceName, file.tag, usdcPrice)),
|
|
67
|
+
chainName: this.config.chainName,
|
|
68
|
+
settlementTrackerContractAddress,
|
|
69
|
+
usdcPrice,
|
|
70
|
+
pinataJwt: jwt
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
async tryDecrypt(owner, name, tag) {
|
|
75
|
+
return await this.delegateeFangorn.decryptFile(owner, name, tag);
|
|
76
|
+
}
|
|
77
|
+
async tryDecryptDelegator(owner, name, tag) {
|
|
78
|
+
return await this.delegatorFangorn.decryptFile(owner, name, tag);
|
|
79
|
+
}
|
|
80
|
+
async checkDatasourceRegistryExistence(who, name) {
|
|
81
|
+
const datasource = await this.delegatorFangorn.getDataSource(who, name);
|
|
82
|
+
return datasource.owner == who.toString() && datasource.name == name;
|
|
83
|
+
}
|
|
84
|
+
async checkDataExistence(who, name, tag) {
|
|
85
|
+
await this.delegatorFangorn.getDataSourceData(who, name, tag);
|
|
86
|
+
}
|
|
87
|
+
async buildUsdcAuthorization(recipient, amount, chainId, usdcContractName, usdcAddress) {
|
|
88
|
+
const walletClient = this.delegateeFangorn["walletClient"];
|
|
89
|
+
const account = walletClient.account;
|
|
90
|
+
const domain = {
|
|
91
|
+
name: usdcContractName,
|
|
92
|
+
version: "2",
|
|
93
|
+
chainId,
|
|
94
|
+
verifyingContract: usdcAddress
|
|
95
|
+
};
|
|
96
|
+
const types = { TransferWithAuthorization: [
|
|
97
|
+
{
|
|
98
|
+
name: "from",
|
|
99
|
+
type: "address"
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
name: "to",
|
|
103
|
+
type: "address"
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
name: "value",
|
|
107
|
+
type: "uint256"
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: "validAfter",
|
|
111
|
+
type: "uint256"
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
name: "validBefore",
|
|
115
|
+
type: "uint256"
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
name: "nonce",
|
|
119
|
+
type: "bytes32"
|
|
120
|
+
}
|
|
121
|
+
] };
|
|
122
|
+
const value = parseUnits(amount, 6);
|
|
123
|
+
const nonce = keccak256(toHex(crypto.getRandomValues(new Uint8Array(32))));
|
|
124
|
+
const signature = await walletClient.signTypedData({
|
|
125
|
+
account,
|
|
126
|
+
domain,
|
|
127
|
+
types,
|
|
128
|
+
primaryType: "TransferWithAuthorization",
|
|
129
|
+
message: {
|
|
130
|
+
from: account.address,
|
|
131
|
+
to: recipient,
|
|
132
|
+
value,
|
|
133
|
+
validAfter: 0n,
|
|
134
|
+
validBefore: 281474976710655n,
|
|
135
|
+
nonce
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
return {
|
|
139
|
+
from: account.address,
|
|
140
|
+
to: recipient,
|
|
141
|
+
amount: value,
|
|
142
|
+
validAfter: 0n,
|
|
143
|
+
validBefore: 281474976710655n,
|
|
144
|
+
nonce,
|
|
145
|
+
signature
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
async payForFile(owner, name, tag, amount, usdcDomainName, settlementTrackerAddress, walletClient, rpcUrl) {
|
|
149
|
+
const auth = await this.buildUsdcAuthorization(this.delegatorAddress, amount, this.config.caip2, usdcDomainName, this.usdcContractAddress);
|
|
150
|
+
const commitmentHex = fieldToHex(await computeTagCommitment(owner, name, tag, amount));
|
|
151
|
+
await new SettlementTracker(settlementTrackerAddress, createPublicClient({ transport: http(rpcUrl) }), walletClient).pay({
|
|
152
|
+
commitment: commitmentHex,
|
|
153
|
+
from: auth.from,
|
|
154
|
+
to: auth.to,
|
|
155
|
+
value: auth.amount,
|
|
156
|
+
validAfter: auth.validAfter,
|
|
157
|
+
validBefore: auth.validBefore,
|
|
158
|
+
nonce: auth.nonce,
|
|
159
|
+
...this.parseSignature(auth.signature)
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
parseSignature(signature) {
|
|
163
|
+
const r = `0x${signature.slice(2, 66)}`;
|
|
164
|
+
const s = `0x${signature.slice(66, 130)}`;
|
|
165
|
+
return {
|
|
166
|
+
v: parseInt(signature.slice(130, 132), 16),
|
|
167
|
+
r,
|
|
168
|
+
s
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
//#endregion
|
|
174
|
+
export { TestBed };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { GadgetDescriptor } from "../modules/gadgets/types.js";
|
|
2
|
+
|
|
3
|
+
//#region src/types/index.d.ts
|
|
4
|
+
interface VaultEntry {
|
|
5
|
+
tag: string;
|
|
6
|
+
cid: string;
|
|
7
|
+
index: number;
|
|
8
|
+
gadgetDescriptor: GadgetDescriptor;
|
|
9
|
+
extension: string;
|
|
10
|
+
fileType: string;
|
|
11
|
+
}
|
|
12
|
+
interface VaultManifest {
|
|
13
|
+
version: number;
|
|
14
|
+
entries: VaultEntry[];
|
|
15
|
+
tree?: string[][];
|
|
16
|
+
}
|
|
17
|
+
interface BuildManifestOptions {
|
|
18
|
+
root: string;
|
|
19
|
+
entries: VaultEntry[];
|
|
20
|
+
tree?: string[][];
|
|
21
|
+
}
|
|
22
|
+
declare const buildManifest: (options: BuildManifestOptions) => VaultManifest;
|
|
23
|
+
interface PendingEntry {
|
|
24
|
+
tag: string;
|
|
25
|
+
cid: string;
|
|
26
|
+
gadgetDescriptor: GadgetDescriptor;
|
|
27
|
+
extension: string;
|
|
28
|
+
fileType: string;
|
|
29
|
+
}
|
|
30
|
+
interface Filedata {
|
|
31
|
+
tag: string;
|
|
32
|
+
data: string;
|
|
33
|
+
extension: string;
|
|
34
|
+
fileType: string;
|
|
35
|
+
}
|
|
36
|
+
interface EncryptedData {
|
|
37
|
+
ciphertext: Uint8Array<ArrayBuffer>;
|
|
38
|
+
iv: Uint8Array<ArrayBuffer>;
|
|
39
|
+
authTag: Uint8Array<ArrayBuffer>;
|
|
40
|
+
salt: Uint8Array<ArrayBuffer>;
|
|
41
|
+
}
|
|
42
|
+
//#endregion
|
|
43
|
+
export { EncryptedData, Filedata, PendingEntry, VaultEntry, VaultManifest, buildManifest };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Address, Hex } from "viem";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/index.d.ts
|
|
4
|
+
declare function stringToBytes32Array(str: string): number[];
|
|
5
|
+
declare function hexToBytes32Array(hex: `0x${string}`): number[];
|
|
6
|
+
declare function addressToBytes32Array(address: Address): number[];
|
|
7
|
+
declare function fieldToHex(field: bigint): `0x${string}`;
|
|
8
|
+
declare function hexToField(hex: string): bigint;
|
|
9
|
+
declare function deriveDatasourceId(name: string, owner: Address): Hex;
|
|
10
|
+
declare function computeTagCommitment(owner: Address, name: string, tag: string, price: string): bigint;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { addressToBytes32Array, computeTagCommitment, deriveDatasourceId, fieldToHex, hexToBytes32Array, hexToField, stringToBytes32Array };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { encodeAbiParameters, encodePacked, keccak256, parseAbiParameters } from "viem";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/index.ts
|
|
4
|
+
function stringToBytes32Array(str) {
|
|
5
|
+
const padded = str.padEnd(32, "\0");
|
|
6
|
+
return Array.from(new TextEncoder().encode(padded)).slice(0, 32);
|
|
7
|
+
}
|
|
8
|
+
function hexToBytes32Array(hex) {
|
|
9
|
+
const padded = hex.slice(2).padStart(64, "0");
|
|
10
|
+
return Array.from(Buffer.from(padded, "hex"));
|
|
11
|
+
}
|
|
12
|
+
function addressToBytes32Array(address) {
|
|
13
|
+
const clean = address.slice(2).toLowerCase();
|
|
14
|
+
const bytes20 = Array.from(Buffer.from(clean, "hex"));
|
|
15
|
+
return [...new Array(12).fill(0), ...bytes20];
|
|
16
|
+
}
|
|
17
|
+
function fieldToHex(field) {
|
|
18
|
+
return `0x${field.toString(16).padStart(64, "0")}`;
|
|
19
|
+
}
|
|
20
|
+
function hexToField(hex) {
|
|
21
|
+
const cleanHex = hex.startsWith("0x") ? hex : `0x${hex}`;
|
|
22
|
+
return BigInt(cleanHex);
|
|
23
|
+
}
|
|
24
|
+
function deriveDatasourceId(name, owner) {
|
|
25
|
+
return keccak256(encodeAbiParameters(parseAbiParameters("string, address"), [name, owner]));
|
|
26
|
+
}
|
|
27
|
+
function computeTagCommitment(owner, name, tag, price) {
|
|
28
|
+
const hash = keccak256(encodePacked([
|
|
29
|
+
"bytes32",
|
|
30
|
+
"string",
|
|
31
|
+
"string",
|
|
32
|
+
"string"
|
|
33
|
+
], [
|
|
34
|
+
deriveDatasourceId(name, owner),
|
|
35
|
+
name,
|
|
36
|
+
tag,
|
|
37
|
+
price
|
|
38
|
+
]));
|
|
39
|
+
return BigInt(hash);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
export { addressToBytes32Array, computeTagCommitment, deriveDatasourceId, fieldToHex, hexToBytes32Array, hexToField, stringToBytes32Array };
|