Skip to content

Commit

Permalink
Merge pull request #402 from LIT-Protocol/feature/lit-2674-js-sdk-mak…
Browse files Browse the repository at this point in the history
…e-getlatestblockhash-async

BREAKING CHANGE: make getLatestBlockhash async
  • Loading branch information
Ansonhkg authored Apr 17, 2024
2 parents 1286138 + 389bcc0 commit 83a070b
Show file tree
Hide file tree
Showing 19 changed files with 70 additions and 52 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"liveServer.settings.port": 5502,
"todo-tree.tree.scanMode": "workspace",
"conventionalCommits.scopes": [
"contracts-sdk"
"contracts-sdk",
"lit-node-client-nodejs",
"core",
],
// "restoreTerminals.terminals": [
// {
Expand Down
2 changes: 1 addition & 1 deletion apps/html/manual_tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
var CheckAndSignAuthMessageWithBlockhash = async() => {
// Currently this will be undefined but later we should replace it with the hardcoded blockhash
const TEST_BLOCKHASH = "0xfe88c94d860f01a17f961bf4bdfb6e0c6cd10d3fda5cc861e805ca1240c58553";
const nonce = litNodeClient.getLatestBlockhash() || TEST_BLOCKHASH;
const nonce = await litNodeClient.getLatestBlockhash() || TEST_BLOCKHASH;

const authSig = await LitJsSdk.checkAndSignAuthMessage({
chain: "ethereum",
Expand Down
2 changes: 1 addition & 1 deletion apps/react/src/app/lit-node-client-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const LitNodeClientPage = () => {
debug: true,
});

const latestBlockhash = client.getLatestBlockhash();
const latestBlockhash = await client.getLatestBlockhash();

const authsig = await authBrowser.checkAndSignAuthMessage({
chain: 'ethereum',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function main() {
console.log(client.hdRootPubkeys);
let subnetKey = '' + client.subnetPubKey;
let rootKeys = client.hdRootPubkeys;
let blockhash = client.getLatestBlockhash();
let blockhash = await client.getLatestBlockhash();
await new Promise((resolve, _reject) => {
setTimeout(resolve, 35_000);
});
Expand Down
32 changes: 32 additions & 0 deletions e2e-nodejs/group-connection/test-latest-blockhash.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import path from 'path';
import { success, fail, testThis } from '../../tools/scripts/utils.mjs';
import LITCONFIG from '../../lit.config.json' assert { type: 'json' };
import { LitNodeClient } from '@lit-protocol/lit-node-client';

const LIT_NETWORK = 'cayenne';

export async function main() {
// ==================== Test Logic ====================
const client = new LitNodeClient({
litNetwork: globalThis.LitCI.network,
debug: globalThis.LitCI.debug,
checkNodeAttestation: globalThis.LitCI.sevAttestation,
});
await client.connect();

const latestBlockhash = await litNodeClient.getLatestBlockhash();

// ==================== Post-Validation ====================
if (!latestBlockhash) {
return fail('latest blockhash not found');
}

if (!latestBlockhash.startsWith('0x')) {
return fail('latest blockhash not in hex format');
}

// ==================== Success ====================
return success(`Latest blockhash found: ${latestBlockhash}`);
}

await testThis({ name: path.basename(import.meta.url), fn: main });
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function main() {
});
await litNodeClient.connect();

let nonce = litNodeClient.getLatestBlockhash();
let nonce = await litNodeClient.getLatestBlockhash();
console.log('Eth blockhash nonce- ', nonce);

if (!nonce) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function main() {
console.log(client.hdRootPubkeys);
let subnetKey = '' + client.subnetPubKey;
let rootKeys = client.hdRootPubkeys;
let blockhash = client.getLatestBlockhash();
let blockhash = await client.getLatestBlockhash();
await new Promise((resolve, _reject) => {
setTimeout(resolve, 35_000);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function main() {
uri: 'https://localhost/login',
version: '1',
chainId: 1,
nonce: litNodeClient.getLatestBlockhash(),
nonce: await litNodeClient.getLatestBlockhash(),
expirationTime: new Date(Date.now() + 60_000 * 60).toISOString(),
});
const messageToSign = siweMessage.prepareMessage();
Expand Down
2 changes: 1 addition & 1 deletion e2e-nodejs/loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if (loadEnv) {

await litNodeClient.connect();

let nonce = litNodeClient.getLatestBlockhash();
let nonce = await litNodeClient.getLatestBlockhash();
console.log('GENERATED NONCE: ', nonce);

const domain = 'localhost';
Expand Down
4 changes: 1 addition & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,5 @@
},
"tags": [
"universal"
],
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
]
}
17 changes: 17 additions & 0 deletions packages/core/src/lib/lit-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,23 @@ export class LitCore {
};

/**
* Return the latest blockhash from the nodes
* @returns { Promise<string> } latest blockhash
*/
getLatestBlockhash = async (): Promise<string> => {
await this.connect();

if (!this.latestBlockhash) {
throw new Error(
`latestBlockhash is not available. Received: "${this.latestBlockhash}"`
);
}

return this.latestBlockhash;
};

/**
*
* Connect to the LIT nodes
*
* @returns { Promise } A promise that resolves when the nodes are connected.
Expand Down
4 changes: 1 addition & 3 deletions packages/lit-auth-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,5 @@
"@simplewebauthn/browser": "^7.2.0",
"@simplewebauthn/typescript-types": "^7.0.0",
"nanoid": "3.3.4"
},
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
}
23 changes: 3 additions & 20 deletions packages/lit-node-client-nodejs/src/lib/lit-node-client-nodejs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from '@lit-protocol/constants';
import { LitCore } from '@lit-protocol/core';
import {
checkSevSnpAttestation,
combineEcdsaShares,
combineSignatureShares,
encrypt,
Expand Down Expand Up @@ -80,6 +81,7 @@ import type {
GetSigningShareForDecryptionRequest,
GetWalletSigProps,
JsonExecutionRequest,
JsonHandshakeResponse,
JsonPkpSignRequest,
LitClientSessionManager,
LitNodeClientConfig,
Expand Down Expand Up @@ -253,7 +255,7 @@ export class LitNodeClientNodeJs
throw new Error('Failed to verify capabilities for resource');
}

const nonce = this.getLatestBlockhash();
const nonce = await this.getLatestBlockhash();

// -- get auth sig
let siweMessage = new siwe.SiweMessage({
Expand Down Expand Up @@ -474,25 +476,6 @@ export class LitNodeClientNodeJs
return LitNodeClientNodeJs.getExpiration();
};

/**
* returns the latest block hash.
* will call refresh if the block hash is expired
* @returns {Promise<string>} latest block hash from `handhsake` with the lit network.
*/
getLatestBlockhash = (): string => {
if (!this.ready) {
logError('Client not connected, remember to call connect');
throwError({
message: 'Client not connected',
errorKind: LIT_ERROR.LIT_NODE_CLIENT_NOT_READY_ERROR.kind,
errorCode: LIT_ERROR.LIT_NODE_CLIENT_NOT_READY_ERROR.code,
});
}

// we are confident in this value being non null so we return
return this.latestBlockhash!;
};

/**
*
* Get the signature from local storage, if not, generates one
Expand Down
4 changes: 1 addition & 3 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@
"publishConfig": {
"access": "public",
"directory": "../../dist/packages/logger"
},
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
}
4 changes: 1 addition & 3 deletions packages/pkp-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,5 @@
},
"tags": [
"universal"
],
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
]
}
4 changes: 1 addition & 3 deletions packages/pkp-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,5 @@
},
"tags": [
"universal"
],
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
]
}
4 changes: 1 addition & 3 deletions packages/pkp-cosmos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,5 @@
},
"tags": [
"universal"
],
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
]
}
4 changes: 1 addition & 3 deletions packages/pkp-sui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,5 @@
},
"tags": [
"universal"
],
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
]
}
4 changes: 1 addition & 3 deletions packages/pkp-walletconnect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,5 @@
},
"tags": [
"universal"
],
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
]
}

0 comments on commit 83a070b

Please sign in to comment.