@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,7 @@
|
|
|
1
|
+
import { VaultEntry, VaultManifest } from "../types/index.js";
|
|
2
|
+
|
|
3
|
+
//#region src/interface/manifest.d.ts
|
|
4
|
+
declare function findEntry(manifest: VaultManifest, tag: string): VaultEntry | undefined;
|
|
5
|
+
declare function findEntryByCid(manifest: VaultManifest, cid: string): VaultEntry | undefined;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { findEntry, findEntryByCid };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
//#region src/interface/manifest.ts
|
|
2
|
+
function findEntry(manifest, tag) {
|
|
3
|
+
return manifest.entries.find((e) => e.tag === tag);
|
|
4
|
+
}
|
|
5
|
+
function findEntryByCid(manifest, cid) {
|
|
6
|
+
return manifest.entries.find((e) => e.cid === cid);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
//#endregion
|
|
10
|
+
export { findEntry, findEntryByCid };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/interface/settlement-tracker/abi.d.ts
|
|
2
|
+
declare const SETTLEMENT_TRACKER_ABI: ({
|
|
3
|
+
name: string;
|
|
4
|
+
type: string;
|
|
5
|
+
stateMutability: string;
|
|
6
|
+
inputs: {
|
|
7
|
+
name: string;
|
|
8
|
+
type: string;
|
|
9
|
+
}[];
|
|
10
|
+
outputs: {
|
|
11
|
+
name: string;
|
|
12
|
+
type: string;
|
|
13
|
+
}[];
|
|
14
|
+
} | {
|
|
15
|
+
name: string;
|
|
16
|
+
type: string;
|
|
17
|
+
inputs: {
|
|
18
|
+
name: string;
|
|
19
|
+
type: string;
|
|
20
|
+
indexed: boolean;
|
|
21
|
+
}[];
|
|
22
|
+
stateMutability?: undefined;
|
|
23
|
+
outputs?: undefined;
|
|
24
|
+
})[];
|
|
25
|
+
//#endregion
|
|
26
|
+
export { SETTLEMENT_TRACKER_ABI };
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
//#region src/interface/settlement-tracker/abi.ts
|
|
2
|
+
const SETTLEMENT_TRACKER_ABI = [
|
|
3
|
+
{
|
|
4
|
+
name: "pay",
|
|
5
|
+
type: "function",
|
|
6
|
+
stateMutability: "payable",
|
|
7
|
+
inputs: [
|
|
8
|
+
{
|
|
9
|
+
name: "commitment",
|
|
10
|
+
type: "bytes32"
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
name: "from",
|
|
14
|
+
type: "address"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
name: "to",
|
|
18
|
+
type: "address"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: "value",
|
|
22
|
+
type: "uint256"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: "validAfter",
|
|
26
|
+
type: "uint256"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: "validBefore",
|
|
30
|
+
type: "uint256"
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: "nonce",
|
|
34
|
+
type: "bytes32"
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: "v",
|
|
38
|
+
type: "uint8"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: "r",
|
|
42
|
+
type: "bytes32"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: "s",
|
|
46
|
+
type: "bytes32"
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
outputs: []
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
name: "checkSettlement",
|
|
53
|
+
type: "function",
|
|
54
|
+
stateMutability: "view",
|
|
55
|
+
inputs: [{
|
|
56
|
+
name: "commitment",
|
|
57
|
+
type: "bytes32"
|
|
58
|
+
}, {
|
|
59
|
+
name: "user",
|
|
60
|
+
type: "address"
|
|
61
|
+
}],
|
|
62
|
+
outputs: [{
|
|
63
|
+
name: "amount",
|
|
64
|
+
type: "uint256"
|
|
65
|
+
}]
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
name: "SettlementRecordedd",
|
|
69
|
+
type: "event",
|
|
70
|
+
inputs: [{
|
|
71
|
+
name: "hash",
|
|
72
|
+
type: "bytes32",
|
|
73
|
+
indexed: true
|
|
74
|
+
}, {
|
|
75
|
+
name: "amount",
|
|
76
|
+
type: "uint256",
|
|
77
|
+
indexed: false
|
|
78
|
+
}]
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
name: "TransferFailed",
|
|
82
|
+
type: "error",
|
|
83
|
+
inputs: []
|
|
84
|
+
}
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
88
|
+
export { SETTLEMENT_TRACKER_ABI };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import * as viem0 from "viem";
|
|
2
|
+
import { Address, Hash, Hex, PublicClient, WalletClient } from "viem";
|
|
3
|
+
|
|
4
|
+
//#region src/interface/settlement-tracker/settlementTracker.d.ts
|
|
5
|
+
declare class SettlementTracker {
|
|
6
|
+
private publicClient;
|
|
7
|
+
private walletClient;
|
|
8
|
+
private contractAddress;
|
|
9
|
+
constructor(contractAddress: Address, publicClient: PublicClient, walletClient: WalletClient);
|
|
10
|
+
private getWriteConfig;
|
|
11
|
+
getContractAddress(): `0x${string}`;
|
|
12
|
+
checkSettlement(commitment: Hex, user: Address): Promise<boolean>;
|
|
13
|
+
/**
|
|
14
|
+
* Settles the payment on-chain using the EIP-3009 authorization
|
|
15
|
+
* This is usually called by the Facilitator.
|
|
16
|
+
*/
|
|
17
|
+
pay(args: {
|
|
18
|
+
commitment: Hex;
|
|
19
|
+
from: Address;
|
|
20
|
+
to: Address;
|
|
21
|
+
value: bigint;
|
|
22
|
+
validAfter: bigint;
|
|
23
|
+
validBefore: bigint;
|
|
24
|
+
nonce: Hex;
|
|
25
|
+
v: number;
|
|
26
|
+
r: Hex;
|
|
27
|
+
s: Hex;
|
|
28
|
+
}): Promise<Hash>;
|
|
29
|
+
waitForTransaction(hash: Hash): Promise<viem0.TransactionReceipt>;
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
export { SettlementTracker };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { SETTLEMENT_TRACKER_ABI } from "./abi.js";
|
|
2
|
+
|
|
3
|
+
//#region src/interface/settlement-tracker/settlementTracker.ts
|
|
4
|
+
var SettlementTracker = class {
|
|
5
|
+
publicClient;
|
|
6
|
+
walletClient;
|
|
7
|
+
contractAddress;
|
|
8
|
+
constructor(contractAddress, publicClient, walletClient) {
|
|
9
|
+
this.publicClient = publicClient;
|
|
10
|
+
this.contractAddress = contractAddress;
|
|
11
|
+
this.walletClient = walletClient;
|
|
12
|
+
}
|
|
13
|
+
getWriteConfig() {
|
|
14
|
+
if (!this.walletClient.chain) throw new Error("Chain required");
|
|
15
|
+
if (!this.walletClient.account) throw new Error("Account required");
|
|
16
|
+
return {
|
|
17
|
+
chain: this.walletClient.chain,
|
|
18
|
+
account: this.walletClient.account
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
getContractAddress() {
|
|
22
|
+
return this.contractAddress;
|
|
23
|
+
}
|
|
24
|
+
async checkSettlement(commitment, user) {
|
|
25
|
+
return await this.publicClient.readContract({
|
|
26
|
+
address: this.contractAddress,
|
|
27
|
+
abi: SETTLEMENT_TRACKER_ABI,
|
|
28
|
+
functionName: "checkSettlement",
|
|
29
|
+
args: [commitment, user]
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Settles the payment on-chain using the EIP-3009 authorization
|
|
34
|
+
* This is usually called by the Facilitator.
|
|
35
|
+
*/
|
|
36
|
+
async pay(args) {
|
|
37
|
+
const { chain, account } = this.getWriteConfig();
|
|
38
|
+
return this.walletClient.writeContract({
|
|
39
|
+
address: this.contractAddress,
|
|
40
|
+
abi: SETTLEMENT_TRACKER_ABI,
|
|
41
|
+
functionName: "pay",
|
|
42
|
+
args: [
|
|
43
|
+
args.commitment,
|
|
44
|
+
args.from,
|
|
45
|
+
args.to,
|
|
46
|
+
args.value,
|
|
47
|
+
args.validAfter,
|
|
48
|
+
args.validBefore,
|
|
49
|
+
args.nonce,
|
|
50
|
+
args.v,
|
|
51
|
+
args.r,
|
|
52
|
+
args.s
|
|
53
|
+
],
|
|
54
|
+
chain,
|
|
55
|
+
account
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
async waitForTransaction(hash) {
|
|
59
|
+
return this.publicClient.waitForTransactionReceipt({ hash });
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
export { SettlementTracker };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//#region src/modules/encryption/actions.ts
|
|
2
|
+
const createDecryptAction = (chainName) => `(async () => {
|
|
3
|
+
try {
|
|
4
|
+
const decryptedContent = await Lit.Actions.decryptAndCombine({
|
|
5
|
+
accessControlConditions: jsParams.accessControlConditions,
|
|
6
|
+
ciphertext: jsParams.ciphertext,
|
|
7
|
+
dataToEncryptHash: jsParams.dataToEncryptHash,
|
|
8
|
+
authSig: jsParams.authSig,
|
|
9
|
+
chain: "${chainName}",
|
|
10
|
+
});
|
|
11
|
+
Lit.Actions.setResponse({ response: decryptedContent, success: true });
|
|
12
|
+
} catch (error) {
|
|
13
|
+
Lit.Actions.setResponse({ response: error.message, success: false });
|
|
14
|
+
}
|
|
15
|
+
})();`;
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { createDecryptAction };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { EncryptedData } from "../../types/index.js";
|
|
2
|
+
|
|
3
|
+
//#region src/modules/encryption/aes.d.ts
|
|
4
|
+
declare function encryptData(data: string | Uint8Array): Promise<{
|
|
5
|
+
encryptedData: EncryptedData;
|
|
6
|
+
keyMaterial: Uint8Array<ArrayBuffer>;
|
|
7
|
+
}>;
|
|
8
|
+
declare function decryptData(encryptedData: EncryptedData, keyMaterial: Uint8Array<ArrayBuffer>): Promise<Uint8Array>;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { decryptData, encryptData };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { getRandomValues, getSubtleCrypto } from "./rand.js";
|
|
2
|
+
|
|
3
|
+
//#region src/modules/encryption/aes.ts
|
|
4
|
+
async function encryptData(data) {
|
|
5
|
+
const subtle = getSubtleCrypto();
|
|
6
|
+
const salt = getRandomValues(new Uint8Array(16));
|
|
7
|
+
const iv = getRandomValues(new Uint8Array(12));
|
|
8
|
+
const keyMaterial = getRandomValues(new Uint8Array(32));
|
|
9
|
+
const key = await subtle.importKey("raw", keyMaterial, { name: "AES-GCM" }, false, ["encrypt"]);
|
|
10
|
+
const encodedData = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
11
|
+
const encryptedContent = await subtle.encrypt({
|
|
12
|
+
name: "AES-GCM",
|
|
13
|
+
iv,
|
|
14
|
+
tagLength: 128
|
|
15
|
+
}, key, encodedData);
|
|
16
|
+
const ciphertext = encryptedContent.slice(0, encryptedContent.byteLength - 16);
|
|
17
|
+
const authTag = encryptedContent.slice(encryptedContent.byteLength - 16);
|
|
18
|
+
return {
|
|
19
|
+
encryptedData: {
|
|
20
|
+
ciphertext: new Uint8Array(ciphertext),
|
|
21
|
+
iv,
|
|
22
|
+
authTag: new Uint8Array(authTag),
|
|
23
|
+
salt
|
|
24
|
+
},
|
|
25
|
+
keyMaterial
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
async function decryptData(encryptedData, keyMaterial) {
|
|
29
|
+
const subtle = getSubtleCrypto();
|
|
30
|
+
const ciphertext = toUint8Array(encryptedData.ciphertext);
|
|
31
|
+
const iv = toUint8Array(encryptedData.iv);
|
|
32
|
+
const authTag = toUint8Array(encryptedData.authTag);
|
|
33
|
+
const key = await subtle.importKey("raw", keyMaterial, { name: "AES-GCM" }, false, ["decrypt"]);
|
|
34
|
+
const dataWithAuthTag = new Uint8Array(ciphertext.length + authTag.length);
|
|
35
|
+
dataWithAuthTag.set(ciphertext, 0);
|
|
36
|
+
dataWithAuthTag.set(authTag, ciphertext.length);
|
|
37
|
+
const decryptedContent = await subtle.decrypt({
|
|
38
|
+
name: "AES-GCM",
|
|
39
|
+
iv,
|
|
40
|
+
tagLength: 128
|
|
41
|
+
}, key, dataWithAuthTag);
|
|
42
|
+
return new Uint8Array(decryptedContent);
|
|
43
|
+
}
|
|
44
|
+
function toUint8Array(data) {
|
|
45
|
+
if (data instanceof Uint8Array) return data;
|
|
46
|
+
if (Array.isArray(data)) return new Uint8Array(data);
|
|
47
|
+
return new Uint8Array(Object.values(data));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
export { decryptData, encryptData };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Gadget } from "../gadgets/types.js";
|
|
2
|
+
import { Filedata } from "../../types/index.js";
|
|
3
|
+
import { DecryptedPayload, EncryptedPayload } from "./types.js";
|
|
4
|
+
import { LitEncryptionService, LitEncryptionServiceConfig } from "./lit.js";
|
|
5
|
+
import { WalletClient } from "viem";
|
|
6
|
+
|
|
7
|
+
//#region src/modules/encryption/index.d.ts
|
|
8
|
+
interface EncryptionService {
|
|
9
|
+
encrypt(file: Filedata, gadget: Gadget): Promise<EncryptedPayload>;
|
|
10
|
+
decrypt(payload: EncryptedPayload, authContext: AuthContext): Promise<DecryptedPayload>;
|
|
11
|
+
createAuthContext(walletClient: WalletClient, domain: string): Promise<AuthContext>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Auth context needed for Lit decryption
|
|
15
|
+
*/
|
|
16
|
+
interface AuthContext {
|
|
17
|
+
authSig: AuthSig;
|
|
18
|
+
sessionContext?: unknown;
|
|
19
|
+
}
|
|
20
|
+
interface AuthSig {
|
|
21
|
+
sig: string;
|
|
22
|
+
derivedVia: string;
|
|
23
|
+
signedMessage: string;
|
|
24
|
+
address: string;
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { AuthContext, AuthSig, EncryptionService, LitEncryptionService, LitEncryptionServiceConfig };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Gadget } from "../gadgets/types.js";
|
|
2
|
+
import { Filedata } from "../../types/index.js";
|
|
3
|
+
import { AuthContextWrapper, DecryptedPayload, EncryptedPayload } from "./types.js";
|
|
4
|
+
import { EncryptionService } from "./index.js";
|
|
5
|
+
import { WalletClient } from "viem";
|
|
6
|
+
import { LitClient } from "@lit-protocol/lit-client";
|
|
7
|
+
|
|
8
|
+
//#region src/modules/encryption/lit.d.ts
|
|
9
|
+
interface LitEncryptionServiceConfig {
|
|
10
|
+
chainName: string;
|
|
11
|
+
}
|
|
12
|
+
declare class LitEncryptionService implements EncryptionService {
|
|
13
|
+
private litClient;
|
|
14
|
+
private chainName;
|
|
15
|
+
constructor(litClient: LitClient, chainName: string);
|
|
16
|
+
static init(chain: string): Promise<LitEncryptionService>;
|
|
17
|
+
/**
|
|
18
|
+
* Encrypt filedata under the given gadget
|
|
19
|
+
* @param file The filedata to encrypt
|
|
20
|
+
* @param gadget The gadget to use
|
|
21
|
+
* @returns The ciphertext bundle
|
|
22
|
+
*/
|
|
23
|
+
encrypt(file: Filedata, gadget: Gadget): Promise<EncryptedPayload>;
|
|
24
|
+
/**
|
|
25
|
+
* Attempt to decrypt some encrypted data
|
|
26
|
+
* @param payload The encrytped bundle to recover
|
|
27
|
+
* @param authContext The authorization context
|
|
28
|
+
* @returns The decrytped output (on success), else empty
|
|
29
|
+
*/
|
|
30
|
+
decrypt(payload: EncryptedPayload, authContext: AuthContextWrapper): Promise<DecryptedPayload>;
|
|
31
|
+
createAuthContext(walletClient: WalletClient, domain: string): Promise<AuthContextWrapper>;
|
|
32
|
+
private createAuthSig;
|
|
33
|
+
private parseKeyResponse;
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
export { LitEncryptionService, LitEncryptionServiceConfig };
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { decryptData, encryptData } from "./aes.js";
|
|
2
|
+
import { LitAccessControlConditionResource, LitActionResource, LitPKPResource, createSiweMessage } from "@lit-protocol/auth-helpers";
|
|
3
|
+
import { createLitClient } from "@lit-protocol/lit-client";
|
|
4
|
+
import { createAuthManager, storagePlugins } from "@lit-protocol/auth";
|
|
5
|
+
import { nagaDev } from "@lit-protocol/networks";
|
|
6
|
+
|
|
7
|
+
//#region src/modules/encryption/lit.ts
|
|
8
|
+
const createDecryptLitActionCode = (chainName) => `(async () => {
|
|
9
|
+
try {
|
|
10
|
+
const decryptedContent = await Lit.Actions.decryptAndCombine({
|
|
11
|
+
accessControlConditions: jsParams.accessControlConditions,
|
|
12
|
+
ciphertext: jsParams.ciphertext,
|
|
13
|
+
dataToEncryptHash: jsParams.dataToEncryptHash,
|
|
14
|
+
authSig: jsParams.authSig,
|
|
15
|
+
chain: "${chainName}",
|
|
16
|
+
});
|
|
17
|
+
Lit.Actions.setResponse({
|
|
18
|
+
response: decryptedContent,
|
|
19
|
+
success: true,
|
|
20
|
+
});
|
|
21
|
+
} catch (error) {
|
|
22
|
+
Lit.Actions.setResponse({
|
|
23
|
+
response: error.message,
|
|
24
|
+
success: false,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
})();`;
|
|
28
|
+
var LitEncryptionService = class LitEncryptionService {
|
|
29
|
+
constructor(litClient, chainName) {
|
|
30
|
+
this.litClient = litClient;
|
|
31
|
+
this.chainName = chainName;
|
|
32
|
+
}
|
|
33
|
+
static async init(chain) {
|
|
34
|
+
return new LitEncryptionService(await createLitClient({ network: nagaDev }), chain);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Encrypt filedata under the given gadget
|
|
38
|
+
* @param file The filedata to encrypt
|
|
39
|
+
* @param gadget The gadget to use
|
|
40
|
+
* @returns The ciphertext bundle
|
|
41
|
+
*/
|
|
42
|
+
async encrypt(file, gadget) {
|
|
43
|
+
const { encryptedData, keyMaterial } = await encryptData(file.data);
|
|
44
|
+
const acc = await gadget.toAccessCondition();
|
|
45
|
+
const litEncryptedKey = await this.litClient.encrypt({
|
|
46
|
+
dataToEncrypt: keyMaterial.toString(),
|
|
47
|
+
unifiedAccessControlConditions: acc,
|
|
48
|
+
chain: this.chainName
|
|
49
|
+
});
|
|
50
|
+
return {
|
|
51
|
+
data: encryptedData,
|
|
52
|
+
key: {
|
|
53
|
+
ciphertext: litEncryptedKey.ciphertext,
|
|
54
|
+
dataToEncryptHash: litEncryptedKey.dataToEncryptHash
|
|
55
|
+
},
|
|
56
|
+
acc,
|
|
57
|
+
litAction: gadget.toLitAction()
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Attempt to decrypt some encrypted data
|
|
62
|
+
* @param payload The encrytped bundle to recover
|
|
63
|
+
* @param authContext The authorization context
|
|
64
|
+
* @returns The decrytped output (on success), else empty
|
|
65
|
+
*/
|
|
66
|
+
async decrypt(payload, authContext) {
|
|
67
|
+
const result = await this.litClient.executeJs({
|
|
68
|
+
code: createDecryptLitActionCode(authContext.chainName),
|
|
69
|
+
authContext: authContext.sessionContext,
|
|
70
|
+
jsParams: {
|
|
71
|
+
accessControlConditions: payload.acc,
|
|
72
|
+
ciphertext: payload.key.ciphertext,
|
|
73
|
+
dataToEncryptHash: payload.key.dataToEncryptHash,
|
|
74
|
+
authSig: authContext.authSig
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
const key = this.parseKeyResponse(result.response);
|
|
78
|
+
return { data: await decryptData(payload.data, key) };
|
|
79
|
+
}
|
|
80
|
+
async createAuthContext(walletClient, domain) {
|
|
81
|
+
const isWindowUndefined = typeof window === "undefined";
|
|
82
|
+
const account = isWindowUndefined ? walletClient.account : walletClient;
|
|
83
|
+
const sessionContext = await (isWindowUndefined ? createAuthManager({ storage: storagePlugins.localStorageNode({
|
|
84
|
+
appName: "fangorn",
|
|
85
|
+
networkName: "naga-dev",
|
|
86
|
+
storagePath: "./lit-auth-storage"
|
|
87
|
+
}) }) : createAuthManager({ storage: storagePlugins.localStorage({
|
|
88
|
+
appName: "fangorn",
|
|
89
|
+
networkName: "naga-dev"
|
|
90
|
+
}) })).createEoaAuthContext({
|
|
91
|
+
litClient: this.litClient,
|
|
92
|
+
config: { account },
|
|
93
|
+
authConfig: {
|
|
94
|
+
domain,
|
|
95
|
+
statement: "Recover key.",
|
|
96
|
+
expiration: new Date(Date.now() + 1e3 * 60 * 60 * 24).toISOString(),
|
|
97
|
+
resources: [
|
|
98
|
+
["access-control-condition-decryption", "*"],
|
|
99
|
+
["lit-action-execution", "*"],
|
|
100
|
+
["pkp-signing", "*"]
|
|
101
|
+
]
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
return {
|
|
105
|
+
authSig: await this.createAuthSig(walletClient, domain),
|
|
106
|
+
sessionContext,
|
|
107
|
+
chainName: walletClient.chain.name
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
async createAuthSig(walletClient, domain) {
|
|
111
|
+
const account = walletClient.account;
|
|
112
|
+
const resources = [
|
|
113
|
+
{
|
|
114
|
+
resource: new LitAccessControlConditionResource("*"),
|
|
115
|
+
ability: "access-control-condition-decryption"
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
resource: new LitActionResource("*"),
|
|
119
|
+
ability: "lit-action-execution"
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
resource: new LitPKPResource("*"),
|
|
123
|
+
ability: "pkp-signing"
|
|
124
|
+
}
|
|
125
|
+
];
|
|
126
|
+
const siweMessage = await createSiweMessage({
|
|
127
|
+
walletAddress: account.address,
|
|
128
|
+
domain,
|
|
129
|
+
statement: "Decrypt data",
|
|
130
|
+
uri: `https://${domain}`,
|
|
131
|
+
version: "1",
|
|
132
|
+
chainId: 1,
|
|
133
|
+
expiration: new Date(Date.now() + 1e3 * 60 * 60 * 24).toISOString(),
|
|
134
|
+
resources,
|
|
135
|
+
nonce: Date.now().toString()
|
|
136
|
+
});
|
|
137
|
+
return {
|
|
138
|
+
sig: await walletClient.signMessage({
|
|
139
|
+
message: siweMessage,
|
|
140
|
+
account
|
|
141
|
+
}),
|
|
142
|
+
derivedVia: "web3.eth.personal.sign",
|
|
143
|
+
signedMessage: siweMessage,
|
|
144
|
+
address: account.address
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
parseKeyResponse(response) {
|
|
148
|
+
return Uint8Array.from(response.replace(/^[^\d]+/, "").split(","), (entry) => {
|
|
149
|
+
const val = parseInt(entry.trim(), 10);
|
|
150
|
+
return isNaN(val) ? 0 : val;
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
//#endregion
|
|
156
|
+
export { LitEncryptionService };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/modules/encryption/rand.ts
|
|
2
|
+
function getCrypto() {
|
|
3
|
+
if (typeof globalThis.crypto !== "undefined") return globalThis.crypto;
|
|
4
|
+
throw new Error("No crypto available - are you in Node < 19?");
|
|
5
|
+
}
|
|
6
|
+
function getSubtleCrypto() {
|
|
7
|
+
if (typeof window !== "undefined" && window.crypto) return window.crypto.subtle;
|
|
8
|
+
else return getCrypto().subtle;
|
|
9
|
+
}
|
|
10
|
+
function getRandomValues(array) {
|
|
11
|
+
if (typeof window !== "undefined" && window.crypto) return window.crypto.getRandomValues(array);
|
|
12
|
+
else return getCrypto().getRandomValues(array);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
//#endregion
|
|
16
|
+
export { getRandomValues, getSubtleCrypto };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region src/modules/encryption/types.d.ts
|
|
2
|
+
interface AesEncryptedData {
|
|
3
|
+
ciphertext: Uint8Array<ArrayBuffer>;
|
|
4
|
+
iv: Uint8Array<ArrayBuffer>;
|
|
5
|
+
authTag: Uint8Array<ArrayBuffer>;
|
|
6
|
+
salt: Uint8Array<ArrayBuffer>;
|
|
7
|
+
}
|
|
8
|
+
interface LitEncryptedData {
|
|
9
|
+
ciphertext: string;
|
|
10
|
+
dataToEncryptHash: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Complete encrypted payload - stored to IPFS
|
|
14
|
+
*/
|
|
15
|
+
interface EncryptedPayload {
|
|
16
|
+
data: AesEncryptedData;
|
|
17
|
+
key: LitEncryptedData;
|
|
18
|
+
acc: AccessControlCondition;
|
|
19
|
+
litAction: string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Lit's access control condition structure
|
|
23
|
+
* Using a loose type since Lit's ACC shape is complex and varies by condition type
|
|
24
|
+
*/
|
|
25
|
+
type AccessControlCondition = Record<string, unknown>;
|
|
26
|
+
/**
|
|
27
|
+
* Auth signature for Lit
|
|
28
|
+
*/
|
|
29
|
+
interface AuthSig {
|
|
30
|
+
sig: string;
|
|
31
|
+
derivedVia: string;
|
|
32
|
+
signedMessage: string;
|
|
33
|
+
address: string;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Context needed to authenticate with Lit for decryption
|
|
37
|
+
*/
|
|
38
|
+
interface AuthContextWrapper {
|
|
39
|
+
authSig: AuthSig;
|
|
40
|
+
sessionContext: any;
|
|
41
|
+
chainName: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Result of successful decryption
|
|
45
|
+
*/
|
|
46
|
+
interface DecryptedPayload {
|
|
47
|
+
data: Uint8Array;
|
|
48
|
+
metadata?: {
|
|
49
|
+
tag: string;
|
|
50
|
+
extension?: string;
|
|
51
|
+
fileType?: string;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
export { AccessControlCondition, AesEncryptedData, AuthContextWrapper, AuthSig, DecryptedPayload, EncryptedPayload, LitEncryptedData };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AccGadget } from "../acc.js";
|
|
2
|
+
import { createAccBuilder } from "@lit-protocol/access-control-conditions";
|
|
3
|
+
|
|
4
|
+
//#region src/modules/gadgets/acc/gadgets.ts
|
|
5
|
+
function identity(chain, who) {
|
|
6
|
+
return new AccGadget(createAccBuilder().requireWalletOwnership(who).on(chain).build(), "Caller must match the specified wallet.");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
//#endregion
|
|
10
|
+
export { identity };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Gadget, GadgetDescriptor } from "./types.js";
|
|
2
|
+
import { AccessControlConditions } from "@lit-protocol/access-control-conditions";
|
|
3
|
+
|
|
4
|
+
//#region src/modules/gadgets/acc.d.ts
|
|
5
|
+
declare class AccGadget implements Gadget {
|
|
6
|
+
private acc;
|
|
7
|
+
private description?;
|
|
8
|
+
readonly type = "acc";
|
|
9
|
+
constructor(acc: AccessControlConditions, description?: string);
|
|
10
|
+
toLitAction(): string;
|
|
11
|
+
toAccessCondition(): Promise<AccessControlConditions>;
|
|
12
|
+
toDescriptor(): GadgetDescriptor;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { AccGadget };
|