Skip to content

Commit edefa24

Browse files
committed
payment, all networks tested
1 parent d176afe commit edefa24

File tree

2 files changed

+76
-46
lines changed

2 files changed

+76
-46
lines changed

lit.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,19 @@ import {
1010
import { Sequencer } from "./lib/sequencer";
1111
import { parseEther } from "ethers/lib/utils";
1212
import { CapacityToken } from "lit";
13-
import { LIT_NETWORK_VALUES, LIT_NETWORKS } from "@lit-protocol/constants";
13+
import { LIT_NETWORK_VALUES } from "@lit-protocol/constants";
1414
import { LitNodeClientNodeJs } from "@lit-protocol/lit-node-client-nodejs";
1515
import { PKPEthersWallet } from "@lit-protocol/pkp-ethers";
1616
import { LitActionResource, LitPKPResource } from "@lit-protocol/auth-helpers";
1717
import { LIT_ABILITY } from "@lit-protocol/constants";
1818
import { LIT_NETWORKS_KEYS } from "@lit-protocol/types";
19-
import { estimateGasWithBalanceOverride, removeTxnSignature, txnToBytesToSign } from "./utils/eth";
2019

2120
import {
2221
datil,
2322
datilDev,
2423
datilTest
2524
} from "@lit-protocol/contracts";
2625

27-
// Cast the network to LIT_NETWORK_VALUES type at the config level
28-
config.network = config.network as unknown as LIT_NETWORK_VALUES;
29-
3026
function getContractFromJsSdk(
3127
network: LIT_NETWORK_VALUES,
3228
contractName: string,
@@ -135,16 +131,6 @@ function getAccessControlConditionsContract() {
135131
}
136132
}
137133

138-
function getPkpNftContractAbiPath() {
139-
if (config.useSoloNet) {
140-
return "./contracts/serrano/SoloNetPKP.json";
141-
}
142-
switch (config.network) {
143-
case "datil-dev":
144-
return "./contracts/serrano/PKPNFT.json";
145-
}
146-
}
147-
148134
function getPkpHelperContract(network: LIT_NETWORK_VALUES): ethers.Contract {
149135
switch (network) {
150136
case "datil-dev":
@@ -782,7 +768,7 @@ export async function initializeLitClient() {
782768
}
783769

784770
export async function getPkpSessionSigs(litNodeClient: any, pkpPublicKey: string, authMethod: any) {
785-
return litNodeClient.getPkpSessionSigs({
771+
let sessionSigsParams: any = {
786772
pkpPublicKey: pkpPublicKey,
787773
chain: "ethereum",
788774
authMethods: [authMethod],
@@ -797,7 +783,36 @@ export async function getPkpSessionSigs(litNodeClient: any, pkpPublicKey: string
797783
ability: LIT_ABILITY.PKPSigning,
798784
},
799785
],
800-
});
786+
};
787+
788+
// Get capacity delegation auth sig for datil networks before session sigs
789+
if (process.env.NETWORK === "datil-test" || process.env.NETWORK === "datil") {
790+
const signer = getSigner();
791+
792+
const capacityTokens = await queryCapacityCredits(signer);
793+
const capacityToken = capacityTokens.find((token) => !token.isExpired);
794+
let capacityTokenId;
795+
796+
if (!capacityToken) {
797+
const mintResult = await mintCapacityCredits({ signer });
798+
if (!mintResult || !mintResult.capacityTokenId) {
799+
throw new Error("Failed to mint capacity credits");
800+
}
801+
capacityTokenId = mintResult.capacityTokenId;
802+
} else {
803+
capacityTokenId = capacityToken.tokenId;
804+
}
805+
806+
const result = await litNodeClient.createCapacityDelegationAuthSig({
807+
dAppOwnerWallet: signer,
808+
capacityTokenId: capacityTokenId.toString(),
809+
delegateeAddresses: [ethers.utils.computeAddress(pkpPublicKey)],
810+
uses: "1",
811+
});
812+
sessionSigsParams.capabilityAuthSigs = [result.capacityDelegationAuthSig];
813+
}
814+
815+
return await litNodeClient.getPkpSessionSigs(sessionSigsParams);
801816
}
802817

803818
export async function signWithPkp(litNodeClient: any, pkpPublicKey: string, sessionSigs: any, toSign: Uint8Array) {

tests/routes/auth/pkpsign.test.ts

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,37 @@ import { LitNodeClientNodeJs } from "@lit-protocol/lit-node-client-nodejs";
88
import { LitRelay, EthWalletProvider } from "@lit-protocol/lit-auth-client";
99
import { LIT_NETWORKS_KEYS } from "@lit-protocol/types";
1010

11-
describe("pkpsign Integration Tests", () => {
11+
type NetworkType = 'datil-dev' | 'datil-test' | 'datil';
12+
13+
const REGISTRY_ADDRESSES = {
14+
'datil-dev': '0x2707eabb60D262024F8738455811a338B0ECd3EC',
15+
'datil-test': '0x525bF2bEb622D7C05E979a8b3fFcDBBEF944450E',
16+
'datil': '0xBDEd44A02b64416C831A0D82a630488A854ab4b1',
17+
} as const;
18+
19+
// Example ABI for PKP contract interactions
20+
const PKP_PERMISSIONS_ABI = [
21+
'function addDelegatees(uint256 pkpTokenId, address[] calldata delegatees) external',
22+
'function getDelegatees(uint256 pkpTokenId) external view returns (address[] memory)'
23+
];
24+
25+
const networks: NetworkType[] = ['datil-dev', 'datil-test', 'datil'];
26+
27+
describe.each(networks)('pkpsign Integration Tests on %s', (network) => {
1228
let app: express.Application;
1329
let litNodeClient: LitNodeClientNodeJs;
1430
let provider: ethers.providers.JsonRpcProvider;
1531
let pkp: any;
1632
let authMethod: any;
1733
let pkpTokenId: ethers.BigNumber;
1834

19-
20-
// Example ABI for PKP contract interactions
21-
const PKP_PERMISSIONS_ABI = [
22-
'function addDelegatees(uint256 pkpTokenId, address[] calldata delegatees) external',
23-
'function getDelegatees(uint256 pkpTokenId) external view returns (address[] memory)'
24-
];
25-
2635
beforeAll(async () => {
36+
// Set network for this test suite
37+
process.env.NETWORK = network;
38+
2739
// connect to lit so we can sign messages
2840
litNodeClient = new LitNodeClientNodeJs({
29-
litNetwork: process.env.NETWORK as LIT_NETWORKS_KEYS,
41+
litNetwork: network,
3042
debug: false,
3143
});
3244
await litNodeClient.connect();
@@ -38,7 +50,7 @@ describe("pkpsign Integration Tests", () => {
3850
const authWallet = getSigner();
3951

4052
const litRelay = new LitRelay({
41-
relayUrl: LitRelay.getRelayUrl(process.env.NETWORK as LIT_NETWORKS_KEYS),
53+
relayUrl: LitRelay.getRelayUrl(network),
4254
relayApiKey: "test-api-key",
4355
});
4456

@@ -58,6 +70,7 @@ describe("pkpsign Integration Tests", () => {
5870
pkpPublicKey: pkp.pkpPublicKey,
5971
pkpEthAddress: pkp.pkpEthAddress,
6072
tokenId: pkp.tokenId,
73+
network: network,
6174
fullResponse: pkp // Log the full response to see what we get
6275
});
6376

@@ -74,15 +87,9 @@ describe("pkpsign Integration Tests", () => {
7487
console.log("Token ID conversion:", {
7588
original: tokenId,
7689
hex: tokenIdHex,
77-
decimal: pkpTokenId.toString()
90+
decimal: pkpTokenId.toString(),
91+
network: network
7892
});
79-
80-
// Create contract instance to verify permissions
81-
const pkpPermissionsContract = new ethers.Contract(
82-
"0x2707eabb60D262024F8738455811a338B0ECd3EC",
83-
PKP_PERMISSIONS_ABI,
84-
provider
85-
);
8693
}, 60000); // Increase timeout to 60 seconds
8794

8895
beforeEach(() => {
@@ -96,7 +103,7 @@ describe("pkpsign Integration Tests", () => {
96103
litNodeClient.disconnect();
97104
});
98105

99-
it("should successfully sign a message using PKP", async () => {
106+
it(`should successfully sign a message using PKP on ${network}`, async () => {
100107
const messageToSign = "Hello, World!";
101108

102109
const response = await request(app)
@@ -121,10 +128,12 @@ describe("pkpsign Integration Tests", () => {
121128
expect(recoveredAddress.toLowerCase()).toBe(pkp.pkpEthAddress!.toLowerCase());
122129
});
123130

124-
it("should successfully sign a contract interaction using PKP", async () => {
131+
it(`should successfully sign a contract interaction using PKP on ${network}`, async () => {
132+
// Get the correct contract address for the current network
133+
const contractAddress = REGISTRY_ADDRESSES[network];
134+
125135
// Create a contract interface for testing
126136
const iface = new ethers.utils.Interface(PKP_PERMISSIONS_ABI);
127-
const contractAddress = "0x2707eabb60D262024F8738455811a338B0ECd3EC";
128137

129138
// Generate a random Ethereum address as delegatee
130139
const delegatee = ethers.Wallet.createRandom().address;
@@ -143,6 +152,7 @@ describe("pkpsign Integration Tests", () => {
143152
pkpAddress: pkp.pkpEthAddress,
144153
delegatee,
145154
contractAddress,
155+
network,
146156
encodedData: data,
147157
});
148158

@@ -152,7 +162,7 @@ describe("pkpsign Integration Tests", () => {
152162
value: "0x0",
153163
gasPrice: await provider.getGasPrice(),
154164
nonce: await provider.getTransactionCount(pkp.pkpEthAddress),
155-
gasLimit: ethers.BigNumber.from(179970), // Updated to match estimated gas
165+
gasLimit: ethers.BigNumber.from(179970),
156166
chainId: (await provider.getNetwork()).chainId,
157167
};
158168

@@ -170,7 +180,8 @@ describe("pkpsign Integration Tests", () => {
170180
data: error.data,
171181
pkpAddress: pkp.pkpEthAddress,
172182
tokenId: pkpTokenId.toString(),
173-
delegatee
183+
delegatee,
184+
network
174185
});
175186
}
176187

@@ -179,7 +190,8 @@ describe("pkpsign Integration Tests", () => {
179190
...transaction,
180191
pkpPublicKey: pkp.pkpPublicKey,
181192
pkpEthAddress: pkp.pkpEthAddress,
182-
tokenId: pkpTokenId.toString()
193+
tokenId: pkpTokenId.toString(),
194+
network
183195
});
184196

185197
try {
@@ -195,7 +207,8 @@ describe("pkpsign Integration Tests", () => {
195207
console.log("Response from pkp-sign:", {
196208
status: response.status,
197209
body: response.body,
198-
txHash: response.body.requestId // This is the transaction hash
210+
txHash: response.body.requestId,
211+
network
199212
});
200213

201214
// Log transaction hash separately for easy copying
@@ -216,7 +229,8 @@ describe("pkpsign Integration Tests", () => {
216229
const delegatees = await contract.getDelegatees(pkpTokenId);
217230
console.log("Delegatees after transaction:", {
218231
delegatees: delegatees.map((d: string) => d.toLowerCase()),
219-
expectedDelegatee: delegatee.toLowerCase()
232+
expectedDelegatee: delegatee.toLowerCase(),
233+
network
220234
});
221235

222236
// Check if our delegatee is in the list
@@ -227,13 +241,14 @@ describe("pkpsign Integration Tests", () => {
227241
console.log("Error from pkp-sign request:", {
228242
error: error.message,
229243
response: error.response?.body,
230-
errorDetails: error.response?.body?.error
244+
errorDetails: error.response?.body?.error,
245+
network
231246
});
232247
throw error;
233248
}
234249
}, 60000);
235250

236-
it("should reject direct ETH transfers", async () => {
251+
it(`should reject direct ETH transfers on ${network}`, async () => {
237252
// Create a simple ETH transfer transaction
238253
const transaction = {
239254
to: ethers.Wallet.createRandom().address,
@@ -259,7 +274,7 @@ describe("pkpsign Integration Tests", () => {
259274
expect(response.body.error).toContain("Direct ETH transfers are not allowed");
260275
});
261276

262-
it("should fail with missing parameters", async () => {
277+
it(`should fail with missing parameters on ${network}`, async () => {
263278
const response = await request(app)
264279
.post("/pkp-sign")
265280
.send({

0 commit comments

Comments
 (0)