nara-sdk 1.0.49 → 1.0.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.ts +3 -0
- package/package.json +1 -1
- package/src/sign.ts +69 -0
package/index.ts
CHANGED
|
@@ -17,6 +17,9 @@ export {
|
|
|
17
17
|
DEFAULT_ALT_ADDRESS,
|
|
18
18
|
} from "./src/constants";
|
|
19
19
|
|
|
20
|
+
// Export signing utilities
|
|
21
|
+
export { signParams, signUrl } from "./src/sign";
|
|
22
|
+
|
|
20
23
|
// Export transaction helper
|
|
21
24
|
export { sendTx, setAltAddress, getAltAddress, getRecentPriorityFee } from "./src/tx";
|
|
22
25
|
|
package/package.json
CHANGED
package/src/sign.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ed25519 request signing for Nara API endpoints (e.g. model-hub).
|
|
3
|
+
*
|
|
4
|
+
* All query params (except `sign`) are sorted alphabetically by key,
|
|
5
|
+
* joined as "key=value&key=value", and signed with Ed25519.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Keypair } from "@solana/web3.js";
|
|
9
|
+
import nacl from "tweetnacl";
|
|
10
|
+
import bs58 from "bs58";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Build the message to sign from query parameters.
|
|
14
|
+
* Sorts all params (excluding "sign") alphabetically by key,
|
|
15
|
+
* joins as "key=value&key=value".
|
|
16
|
+
*/
|
|
17
|
+
function buildSignMessage(params: Record<string, string>): string {
|
|
18
|
+
return Object.keys(params)
|
|
19
|
+
.filter((k) => k !== "sign")
|
|
20
|
+
.sort()
|
|
21
|
+
.map((k) => `${k}=${params[k]}`)
|
|
22
|
+
.join("&");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Sign query parameters with a wallet keypair.
|
|
27
|
+
*
|
|
28
|
+
* @param wallet - Keypair used to sign
|
|
29
|
+
* @param params - All query params to include (excluding "sign")
|
|
30
|
+
* @returns The params with "address", "ts", and "sign" added/set
|
|
31
|
+
*/
|
|
32
|
+
export function signParams(
|
|
33
|
+
wallet: Keypair,
|
|
34
|
+
params: Record<string, string> = {}
|
|
35
|
+
): Record<string, string> {
|
|
36
|
+
const allParams: Record<string, string> = {
|
|
37
|
+
...params,
|
|
38
|
+
address: wallet.publicKey.toBase58(),
|
|
39
|
+
ts: Math.floor(Date.now() / 1000).toString(),
|
|
40
|
+
};
|
|
41
|
+
const message = buildSignMessage(allParams);
|
|
42
|
+
const signature = nacl.sign.detached(
|
|
43
|
+
new TextEncoder().encode(message),
|
|
44
|
+
wallet.secretKey
|
|
45
|
+
);
|
|
46
|
+
allParams.sign = bs58.encode(signature);
|
|
47
|
+
return allParams;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Build a signed URL by appending all signed query parameters.
|
|
52
|
+
*
|
|
53
|
+
* @param baseUrl - Base URL (e.g. "https://api.nara.xyz/model-hub-api/user/info")
|
|
54
|
+
* @param wallet - Keypair used to sign
|
|
55
|
+
* @param params - Additional query params (e.g. { tx: "3k2P7n..." })
|
|
56
|
+
* @returns Full URL with all params + address, ts, sign
|
|
57
|
+
*/
|
|
58
|
+
export function signUrl(
|
|
59
|
+
baseUrl: string,
|
|
60
|
+
wallet: Keypair,
|
|
61
|
+
params?: Record<string, string>
|
|
62
|
+
): string {
|
|
63
|
+
const signed = signParams(wallet, params);
|
|
64
|
+
const url = new URL(baseUrl);
|
|
65
|
+
for (const [k, v] of Object.entries(signed)) {
|
|
66
|
+
url.searchParams.set(k, v);
|
|
67
|
+
}
|
|
68
|
+
return url.toString();
|
|
69
|
+
}
|