Skip to content

Commit b4d65b2

Browse files
committed
feat: add ipfsHash helper
1 parent 1e074af commit b4d65b2

File tree

2 files changed

+24
-46
lines changed

2 files changed

+24
-46
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"tslib": "^2.3.0",
8080
"tweetnacl": "^1.0.3",
8181
"tweetnacl-util": "^0.15.1",
82+
"typestub-ipfs-only-hash": "^4.0.0",
8283
"uint8arrays": "^4.0.3"
8384
},
8485
"devDependencies": {

packages/lit-node-client-nodejs/src/lib/lit-node-client-nodejs.ts

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { computeAddress } from '@ethersproject/transactions';
22
import { BigNumber, ethers } from 'ethers';
33
import { joinSignature, sha256 } from 'ethers/lib/utils';
44
import { SiweMessage } from 'siwe';
5+
import Hash from "typestub-ipfs-only-hash";
56

67
import {
78
ILitResource,
@@ -135,8 +136,7 @@ import type {
135136

136137
export class LitNodeClientNodeJs
137138
extends LitCore
138-
implements LitClientSessionManager, ILitNodeClient
139-
{
139+
implements LitClientSessionManager, ILitNodeClient {
140140
defaultAuthCallback?: (authSigParams: AuthCallbackParams) => Promise<AuthSig>;
141141

142142
// ========== Constructor ==========
@@ -654,40 +654,10 @@ export class LitNodeClientNodeJs
654654
};
655655

656656
// ========== Promise Handlers ==========
657-
getIpfsId = async ({
658-
dataToHash,
659-
sessionSigs,
660-
}: {
661-
dataToHash: string;
662-
sessionSigs: SessionSigsMap;
663-
debug?: boolean;
664-
}) => {
665-
const res = await this.executeJs({
666-
ipfsId: LIT_ACTION_IPFS_HASH,
667-
sessionSigs,
668-
jsParams: {
669-
dataToHash,
670-
},
671-
}).catch((e) => {
672-
logError('Error getting IPFS ID', e);
673-
throw e;
674-
});
675-
676-
let data;
677-
678-
if (typeof res.response === 'string') {
679-
try {
680-
data = JSON.parse(res.response).res;
681-
} catch (e) {
682-
data = res.response;
683-
}
684-
}
685-
686-
if (!data.success) {
687-
logError('Error getting IPFS ID', data.data);
688-
}
689-
690-
return data.data;
657+
getIpfsId = async ({ str }: {
658+
str: string
659+
}): Promise<`Qm${string}`> => {
660+
return await Hash.of(Buffer.from(str)) as `Qm${string}`;
691661
};
692662

693663
/**
@@ -716,10 +686,17 @@ export class LitNodeClientNodeJs
716686
});
717687
}
718688

689+
if (!params.code) {
690+
return throwError({
691+
message: 'code is required',
692+
errorKind: LIT_ERROR.INVALID_PARAM_TYPE.kind,
693+
errorCode: LIT_ERROR.INVALID_PARAM_TYPE.name,
694+
});
695+
}
696+
719697
// determine which node to run on
720698
const ipfsId = await this.getIpfsId({
721-
dataToHash: params.code!,
722-
sessionSigs: params.sessionSigs,
699+
str: params.code,
723700
});
724701

725702
// select targetNodeRange number of random index of the bootstrapUrls.length
@@ -1293,8 +1270,8 @@ export class LitNodeClientNodeJs
12931270
// -- optional params
12941271
...(params.authMethods &&
12951272
params.authMethods.length > 0 && {
1296-
authMethods: params.authMethods,
1297-
}),
1273+
authMethods: params.authMethods,
1274+
}),
12981275
};
12991276

13001277
logWithRequestId(requestId, 'reqBody:', reqBody);
@@ -2103,8 +2080,8 @@ export class LitNodeClientNodeJs
21032080
const sessionCapabilityObject = params.sessionCapabilityObject
21042081
? params.sessionCapabilityObject
21052082
: await this.generateSessionCapabilityObjectWithWildcards(
2106-
params.resourceAbilityRequests.map((r) => r.resource)
2107-
);
2083+
params.resourceAbilityRequests.map((r) => r.resource)
2084+
);
21082085
const expiration = params.expiration || LitNodeClientNodeJs.getExpiration();
21092086

21102087
// -- (TRY) to get the wallet signature
@@ -2187,10 +2164,10 @@ export class LitNodeClientNodeJs
21872164

21882165
const capabilities = params.capacityDelegationAuthSig
21892166
? [
2190-
...(params.capabilityAuthSigs ?? []),
2191-
params.capacityDelegationAuthSig,
2192-
authSig,
2193-
]
2167+
...(params.capabilityAuthSigs ?? []),
2168+
params.capacityDelegationAuthSig,
2169+
authSig,
2170+
]
21942171
: [...(params.capabilityAuthSigs ?? []), authSig];
21952172

21962173
const signingTemplate = {

0 commit comments

Comments
 (0)