npm-package-near-simple-key 1.0.0

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.
Files changed (3) hide show
  1. package/README.md +56 -0
  2. package/index.js +59 -0
  3. package/package.json +29 -0
package/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # npm-package-near-simple-key
2
+
3
+ A lightweight utility for working with NEAR Protocol key pairs and access keys.
4
+
5
+ ## Installation
6
+
7
+ npm install npm-package-near-simple-key
8
+
9
+ ## API & Usage
10
+
11
+ ### `generateKeyPair()`
12
+ Generate a new random ED25519 key pair.
13
+
14
+ import { generateKeyPair } from 'npm-package-near-simple-key';
15
+
16
+ const { publicKey, secretKey } = await generateKeyPair();
17
+ console.log(publicKey); // "ed25519:ABC..."
18
+ console.log(secretKey); // "ed25519:XYZ..."
19
+
20
+ ### `getPublicKey(secretKey)`
21
+ Derive the public key from an existing secret key.
22
+
23
+ import { getPublicKey } from 'npm-package-near-simple-key';
24
+
25
+ const publicKey = await getPublicKey('ed25519:XYZ...');
26
+ console.log(publicKey); // "ed25519:ABC..."
27
+
28
+ ### `getAccessKeys(accountId)`
29
+ Fetch all access keys for a NEAR account from mainnet.
30
+
31
+ import { getAccessKeys } from 'npm-package-near-simple-key';
32
+
33
+ const keys = await getAccessKeys('alice.near');
34
+ keys.forEach(({ public_key, access_key }) => {
35
+ console.log(public_key, access_key.nonce, access_key.permission);
36
+ });
37
+
38
+ ### `hasAccessKey(accountId, publicKey)`
39
+ Check whether a specific public key exists on an account.
40
+
41
+ import { hasAccessKey } from 'npm-package-near-simple-key';
42
+
43
+ const exists = await hasAccessKey('alice.near', 'ed25519:ABC...');
44
+ console.log(exists); // true or false
45
+
46
+ ### `signMessage(secretKey, message)`
47
+ Sign a message with a secret key.
48
+
49
+ import { signMessage } from 'npm-package-near-simple-key';
50
+
51
+ const signature = await signMessage('ed25519:XYZ...', 'hello near');
52
+ console.log(signature);
53
+
54
+ ## License
55
+
56
+ MIT
package/index.js ADDED
@@ -0,0 +1,59 @@
1
+ import { KeyPair, utils } from 'near-api-js';
2
+
3
+ /** Generate a new random NEAR key pair and return publicKey + secretKey */
4
+ export async function generateKeyPair(): Promise<{ publicKey: string; secretKey: string }> {
5
+ const keyPair = KeyPair.fromRandom('ed25519');
6
+ return {
7
+ publicKey: keyPair.getPublicKey().toString(),
8
+ secretKey: keyPair.toString(),
9
+ };
10
+ }
11
+
12
+ /** Derive the public key string from a secret key string */
13
+ export async function getPublicKey(secretKey: string): Promise<string> {
14
+ const keyPair = KeyPair.fromString(secretKey);
15
+ return keyPair.getPublicKey().toString();
16
+ }
17
+
18
+ /** Fetch all access keys for a given NEAR account ID from mainnet RPC */
19
+ export async function getAccessKeys(
20
+ accountId: string
21
+ ): Promise<Array<{ public_key: string; access_key: { nonce: number; permission: unknown } }>> {
22
+ const response = await fetch('https://rpc.mainnet.near.org', {
23
+ method: 'POST',
24
+ headers: { 'Content-Type': 'application/json' },
25
+ body: JSON.stringify({
26
+ jsonrpc: '2.0',
27
+ id: 'near-simple-key',
28
+ method: 'query',
29
+ params: { request_type: 'view_access_key_list', finality: 'final', account_id: accountId },
30
+ }),
31
+ });
32
+ const json = (await response.json()) as { result?: { keys: never[] }; error?: { message: string } };
33
+ if (json.error) throw new Error(json.error.message);
34
+ return json.result!.keys;
35
+ }
36
+
37
+ /** Check whether a specific public key exists on a NEAR account */
38
+ export async function hasAccessKey(accountId: string, publicKey: string): Promise<boolean> {
39
+ const response = await fetch('https://rpc.mainnet.near.org', {
40
+ method: 'POST',
41
+ headers: { 'Content-Type': 'application/json' },
42
+ body: JSON.stringify({
43
+ jsonrpc: '2.0',
44
+ id: 'near-simple-key',
45
+ method: 'query',
46
+ params: { request_type: 'view_access_key', finality: 'final', account_id: accountId, public_key: publicKey },
47
+ }),
48
+ });
49
+ const json = (await response.json()) as { result?: unknown; error?: unknown };
50
+ return !json.error && !!json.result;
51
+ }
52
+
53
+ /** Sign an arbitrary UTF-8 message with a secret key; returns base58-encoded signature */
54
+ export async function signMessage(message: string, secretKey: string): Promise<string> {
55
+ const keyPair = KeyPair.fromString(secretKey);
56
+ const msgBytes = new TextEncoder().encode(message);
57
+ const { signature } = keyPair.sign(msgBytes);
58
+ return utils.serialize.base_encode(signature);
59
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "npm-package-near-simple-key",
3
+ "version": "1.0.0",
4
+ "description": "npm Package - near-simple-key",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "jest --passWithNoTests"
8
+ },
9
+ "keywords": [
10
+ "near",
11
+ "blockchain",
12
+ "web3"
13
+ ],
14
+ "license": "MIT",
15
+ "dependencies": {
16
+ "near-api-js": "^4.0.0"
17
+ },
18
+ "devDependencies": {
19
+ "typescript": "^5.0.0",
20
+ "@types/node": "^20.0.0",
21
+ "jest": "^29.0.0",
22
+ "@types/jest": "^29.0.0",
23
+ "ts-jest": "^29.0.0"
24
+ },
25
+ "files": [
26
+ "dist/**/*",
27
+ "README.md"
28
+ ]
29
+ }