Skip to content

Commit b34d25b

Browse files
committed
add stamping script
1 parent 6eb2664 commit b34d25b

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import * as path from "path";
2+
import * as dotenv from "dotenv";
3+
4+
// Load environment variables from `.env.local`
5+
dotenv.config({ path: path.resolve(process.cwd(), ".env.local") });
6+
7+
import { TurnkeyClient, type TSignedRequest } from "@turnkey/http";
8+
import { ApiKeyStamper } from "@turnkey/api-key-stamper";
9+
import { forwardRequestToTurnkey } from "../utils";
10+
11+
// Function to generate requests array
12+
const generateStampedRequests = async (
13+
turnkeyClient: TurnkeyClient,
14+
count: number
15+
) => {
16+
// Create an array of numbers from 1 to count
17+
const requests = Array.from({ length: count }, (_, index) => {
18+
const envVar = `SIGN_WITH_${(index % 3) + 1}`;
19+
20+
return turnkeyClient.stampSignRawPayload({
21+
type: "ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2",
22+
timestampMs: String(Date.now()),
23+
organizationId: process.env.ORGANIZATION_ID!,
24+
parameters: {
25+
signWith: process.env[envVar]!,
26+
payload: "hello tkhq",
27+
hashFunction: "HASH_FUNCTION_KECCAK256",
28+
encoding: "PAYLOAD_ENCODING_TEXT_UTF8",
29+
},
30+
});
31+
});
32+
33+
// Execute all requests
34+
const stampedRequests = await Promise.allSettled(requests);
35+
return stampedRequests;
36+
};
37+
38+
const checkUniqueStamps = (stampedRequests: TSignedRequest[]): boolean => {
39+
interface StampMap {
40+
[key: string]: boolean;
41+
}
42+
43+
const uniques: StampMap = {};
44+
45+
for (let i = 0; i < stampedRequests.length; i++) {
46+
const currentStampedRequest = stampedRequests[i];
47+
if (uniques[currentStampedRequest!.stamp.stampHeaderValue]) {
48+
return false;
49+
}
50+
51+
uniques[currentStampedRequest!.stamp.stampHeaderValue] = true;
52+
}
53+
54+
return true;
55+
};
56+
57+
async function main() {
58+
// Initialize a Turnkey client
59+
const turnkeyClient = new TurnkeyClient(
60+
{ baseUrl: process.env.BASE_URL! },
61+
new ApiKeyStamper({
62+
apiPublicKey: process.env.API_PUBLIC_KEY!,
63+
apiPrivateKey: process.env.API_PRIVATE_KEY!,
64+
})
65+
);
66+
67+
const stampedRequests = await generateStampedRequests(turnkeyClient, 100);
68+
69+
const successfulRequests = stampedRequests
70+
.filter(
71+
(result): result is PromiseFulfilledResult<TSignedRequest> =>
72+
result.status === "fulfilled"
73+
)
74+
.map((result) => result.value);
75+
76+
console.log("stamped requests", successfulRequests);
77+
78+
const allUnique = checkUniqueStamps(successfulRequests);
79+
80+
console.log("unique stamps?", allUnique);
81+
82+
// optionally forward to Turnkey
83+
// const response = await forwardRequestToTurnkey(stampedRequest);
84+
// const parsedResponse = await response.json();
85+
// console.log("Successfully signed raw payload:", parsedResponse);
86+
}
87+
88+
main().catch((error) => {
89+
console.error(error);
90+
process.exit(1);
91+
});

0 commit comments

Comments
 (0)