Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
manually release private key
  • Loading branch information
glitch003 committed Jul 1, 2024
commit 0791ae5b14bda537688d9d0e112c288ccd6574e8
22 changes: 12 additions & 10 deletions local-tests/setup/tinny-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,6 @@ export class TinnyEnvironment {
this.processEnvs.KEY_IN_USE[index] = true; // Mark the key as in use
// console.log('[𐬺🧪 Tinny Environment𐬺] 🔑 Selected key at index', index); // Log a message indicating that we have selected a key

// Set a timer to automatically release the key after 10 seconds
setTimeout(() => {
this.releasePrivateKey(index);
// console.log(
// '[𐬺🧪 Tinny Environment𐬺] 🔓 Automatically released key at index',
// index,
// `after ${this.processEnvs.TIME_TO_RELEASE_KEY / 10000} seconds`
// );
}, this.processEnvs.TIME_TO_RELEASE_KEY);

return { privateKey: this.processEnvs.PRIVATE_KEYS[index], index }; // Return the key and its index
} else {
// console.log('[𐬺🧪 Tinny Environment𐬺] No available keys. Waiting...'); // Log a message indicating that we are waiting
Expand All @@ -182,6 +172,18 @@ export class TinnyEnvironment {
}
}

/**
* Marks a private key as available again after use.
* @param {number} index - The index of the key to mark as available.
*/
releasePrivateKeyFromUser(user: TinnyPerson) {
const index = this.processEnvs.PRIVATE_KEYS.indexOf(user.privateKey);
this.processEnvs.KEY_IN_USE[index] = false;
// console.log(
// `[𐬺🧪 Tinny Environment𐬺] 🪽 Released key at index ${index}. Thank you for your service!`
// );
}

/**
* Marks a private key as available again after use.
* @param {number} index - The index of the key to mark as available.
Expand Down
2 changes: 2 additions & 0 deletions local-tests/tests/test-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const testExample = async (devEnv: TinnyEnvironment) => {

console.log('aliceExecuteJsRes:', aliceExecuteJsRes);

devEnv.releasePrivateKeyFromUser(alice);

// console.log('aliceEoaSessionSigs: ', aliceEoaSessionSigs);

// const alicePkpSessionSigs = await getPkpSessionSigs(devEnv, alice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ export const testDelegatingCapacityCreditsNFTToAnotherPkpToExecuteJs = async (

console.log('✅ res:', res);

devEnv.releasePrivateKeyFromUser(alice);
devEnv.releasePrivateKeyFromUser(bob);

// -- Expected output:
// {
// claims: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export const testDelegatingCapacityCreditsNFTToAnotherWalletToExecuteJs =
},
});

devEnv.releasePrivateKeyFromUser(alice);
devEnv.releasePrivateKeyFromUser(bob);

// Expected output:
// {
// claims: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export const testDelegatingCapacityCreditsNFTToAnotherWalletToPkpSign = async (
pubKey: bob.pkp.publicKey,
});

devEnv.releasePrivateKeyFromUser(alice);
devEnv.releasePrivateKeyFromUser(bob);

// -- Expected output:
// {
// r: "25e04b2abdf220b1374b19228bc292bab71a3224a635726a46d4cbe3a62bb636",
Expand Down
1 change: 1 addition & 0 deletions local-tests/tests/testExecuteJsBroadcastAndCollect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const testExecuteJsBroadcastAndCollect = async (
})();`,
jsParams: {},
});
devEnv.releasePrivateKeyFromUser(alice);

const response = res.response;
if (!response) {
Expand Down
2 changes: 2 additions & 0 deletions local-tests/tests/testExecuteJsDecryptAndCombine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export const testExecutJsDecryptAndCombine = async (
},
});

devEnv.releasePrivateKeyFromUser(alice);

if (res.response !== 'Hello world') {
throw new Error('content does not match what was expected');
}
Expand Down
3 changes: 3 additions & 0 deletions local-tests/tests/testExecuteJsSignAndCombineEcdsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export const testExecuteJsSignAndCombineEcdsa = async (
},
});

devEnv.releasePrivateKeyFromUser(alice);
devEnv.releasePrivateKeyFromUser(bob);

/**
Response format
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@ export const testPkpEthersWithEoaSessionSigsToEthSign = async (
console.log('✅ recoveredAddr:', recoveredAddr);
} catch (e) {
throw new Error('❌ Error: ' + e.message);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,7 @@ export const testPkpEthersWithEoaSessionSigsToEthSignTransaction = async (
} else {
throw new Error(`❌ Error: ${e.toString()}`);
}
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,7 @@ export const testPkpEthersWithEoaSessionSigsToEthSignTypedData = async (
}
} catch (e) {
throw new Error(`❌ ${e.toString()}`);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,7 @@ export const testPkpEthersWithEoaSessionSigsToEthSignTypedDataUtil = async (
}
} catch (e) {
throw new Error(`❌ ${e.toString()}`);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,7 @@ export const testPkpEthersWithEoaSessionSigsToEthSignTypedDataV1 = async (
console.log('recoveredAddr: ', recoveredAddr);
} catch (e) {
throw new Error(`❌ ${e.toString()}`);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,7 @@ export const testPkpEthersWithEoaSessionSigsToEthSignTypedDataV3 = async (
}
} catch (e) {
throw new Error(`❌ ${e.toString()}`);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,7 @@ export const testPkpEthersWithEoaSessionSigsToEthSignTypedDataV4 = async (
}
} catch (e) {
throw new Error(`❌ ${e.toString()}`);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@ export const testPkpEthersWithEoaSessionSigsToPersonalSign = async (
console.log('✅ personal_sign recoveredAddr:', recoveredAddr);
} catch (e) {
throw new Error('❌ Error: ' + e.message);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ export const testPkpEthersWithEoaSessionSigsToSendTx = async (
} else {
throw new Error(`❌ Error: ${e.toString()}`);
}
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ export const testPkpEthersWithEoaSessionSigsToSignMessage = async (
console.log('✅ signature:', signature);
} catch (e) {
throw new Error('❌ Error: ' + e.message);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,7 @@ export const testPkpEthersWithEoaSessionSigsToSignWithAuthContext = async (
console.log('✅ signature:', signature);
} catch (e) {
throw new Error('❌ Error: ' + e.message);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@ export const testPkpEthersWithLitActionSessionSigsToEthSign = async (
console.log('✅ recoveredAddr:', recoveredAddr);
} catch (e) {
throw new Error('❌ Error: ' + e.message);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,7 @@ export const testPkpEthersWithLitActionSessionSigsToEthSignTransaction = async (
} else {
throw new Error(`❌ Error: ${e.toString()}`);
}
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,7 @@ export const testPkpEthersWithLitActionSessionSigsToEthSignTypedData = async (
}
} catch (e) {
throw new Error(`❌ ${e.toString()}`);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,7 @@ export const testPkpEthersWithLitActionSessionSigsToEthSignTypedDataUtil =
}
} catch (e) {
throw new Error(`❌ ${e.toString()}`);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,7 @@ export const testPkpEthersWithLitActionSessionSigsToEthSignTypedDataV1 = async (
console.log('recoveredAddr: ', recoveredAddr);
} catch (e) {
throw new Error(`❌ ${e.toString()}`);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,7 @@ export const testPkpEthersWithLitActionSessionSigsToEthSignTypedDataV3 = async (
}
} catch (e) {
throw new Error(`❌ ${e.toString()}`);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,7 @@ export const testPkpEthersWithLitActionSessionSigsToEthSignTypedDataV4 = async (
}
} catch (e) {
throw new Error(`❌ ${e.toString()}`);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@ export const testPkpEthersWithLitActionSessionSigsToPersonalSign = async (
console.log('✅ personal_sign recoveredAddr:', recoveredAddr);
} catch (e) {
throw new Error('❌ Error: ' + e.message);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ export const testPkpEthersWithLitActionSessionSigsToSendTx = async (
} else {
throw new Error(`❌ Error: ${e.toString()}`);
}
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ export const testPkpEthersWithLitActionSessionSigsToSignMessage = async (
console.log('✅ signature:', signature);
} catch (e) {
throw new Error('❌ Error: ' + e.message);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@ export const testPkpEthersWithPkpSessionSigsToEthSign = async (
console.log('✅ recoveredAddr:', recoveredAddr);
} catch (e) {
throw new Error('❌ Error: ' + e.message);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,7 @@ export const testPkpEthersWithPkpSessionSigsToEthSignTransaction = async (
} else {
throw new Error(`❌ Error: ${e.toString()}`);
}
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,7 @@ export const testPkpEthersWithPkpSessionSigsToEthSignTypedData = async (
}
} catch (e) {
throw new Error(`❌ ${e.toString()}`);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,7 @@ export const testPkpEthersWithPkpSessionSigsToEthSignTypedDataUtil = async (
}
} catch (e) {
throw new Error(`❌ ${e.toString()}`);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,7 @@ export const testPkpEthersWithPkpSessionSigsToEthSignTypedDataV1 = async (
console.log('recoveredAddr: ', recoveredAddr);
} catch (e) {
throw new Error(`❌ ${e.toString()}`);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,7 @@ export const testPkpEthersWithPkpSessionSigsToEthSignTypedDataV3 = async (
}
} catch (e) {
throw new Error(`❌ ${e.toString()}`);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,7 @@ export const testPkpEthersWithPkpSessionSigsToEthSignTypedDataV4 = async (
}
} catch (e) {
throw new Error(`❌ ${e.toString()}`);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@ export const testPkpEthersWithPkpSessionSigsToPersonalSign = async (
console.log('✅ personal_sign recoveredAddr:', recoveredAddr);
} catch (e) {
throw new Error('❌ Error: ' + e.message);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@ export const testPkpEthersWithPkpSessionSigsToSendTx = async (
} else {
throw new Error(`❌ Error: ${e.toString()}`);
}
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ export const testPkpEthersWithPkpSessionSigsToSignMessage = async (
console.log('✅ signature:', signature);
} catch (e) {
throw new Error('❌ Error: ' + e.message);
} finally {
devEnv.releasePrivateKeyFromUser(alice);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ export const testUseCapacityDelegationAuthSigWithUnspecifiedCapacityTokenIdToExe
},
});

devEnv.releasePrivateKeyFromUser(alice);
devEnv.releasePrivateKeyFromUser(bob);

// Expected output:
// {
// claims: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export const testUseCapacityDelegationAuthSigWithUnspecifiedCapacityTokenIdToPkp
pubKey: bob.pkp.publicKey,
});

devEnv.releasePrivateKeyFromUser(alice);
devEnv.releasePrivateKeyFromUser(bob);

// -- Expected output:
// {
// r: "25e04b2abdf220b1374b19228bc292bab71a3224a635726a46d4cbe3a62bb636",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ export const testUseCapacityDelegationAuthSigWithUnspecifiedDelegateesToExecuteJ
},
});

devEnv.releasePrivateKeyFromUser(alice);
devEnv.releasePrivateKeyFromUser(bob);

// Expected output:
// {
// claims: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export const testUseCapacityDelegationAuthSigWithUnspecifiedDelegateesToPkpSign
sessionSigs: bobsSessionSigs,
});

devEnv.releasePrivateKeyFromUser(alice);
devEnv.releasePrivateKeyFromUser(bob);

// -- Expected output:
// {
// r: "36bd0039b4e4d1dae488a63437318790df86b8023ac4ffa842c8983245b7f629",
Expand Down
Loading