@solidus-network/sdk 0.1.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.
- package/LICENSE +201 -0
- package/README.md +71 -0
- package/dist/chain/auth.d.ts +8 -0
- package/dist/chain/auth.d.ts.map +1 -0
- package/dist/chain/auth.js +78 -0
- package/dist/chain/auth.js.map +1 -0
- package/dist/chain/credentials.d.ts +17 -0
- package/dist/chain/credentials.d.ts.map +1 -0
- package/dist/chain/credentials.js +175 -0
- package/dist/chain/credentials.js.map +1 -0
- package/dist/chain/did.d.ts +16 -0
- package/dist/chain/did.d.ts.map +1 -0
- package/dist/chain/did.js +111 -0
- package/dist/chain/did.js.map +1 -0
- package/dist/chain/index.d.ts +13 -0
- package/dist/chain/index.d.ts.map +1 -0
- package/dist/chain/index.js +19 -0
- package/dist/chain/index.js.map +1 -0
- package/dist/chain/rpc.d.ts +9 -0
- package/dist/chain/rpc.d.ts.map +1 -0
- package/dist/chain/rpc.js +25 -0
- package/dist/chain/rpc.js.map +1 -0
- package/dist/chain/transaction.d.ts +27 -0
- package/dist/chain/transaction.d.ts.map +1 -0
- package/dist/chain/transaction.js +78 -0
- package/dist/chain/transaction.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +52 -0
- package/dist/index.js.map +1 -0
- package/dist/stub/credentials.d.ts +6 -0
- package/dist/stub/credentials.d.ts.map +1 -0
- package/dist/stub/credentials.js +123 -0
- package/dist/stub/credentials.js.map +1 -0
- package/dist/stub/crypto.d.ts +10 -0
- package/dist/stub/crypto.d.ts.map +1 -0
- package/dist/stub/crypto.js +38 -0
- package/dist/stub/crypto.js.map +1 -0
- package/dist/stub/db.d.ts +5 -0
- package/dist/stub/db.d.ts.map +1 -0
- package/dist/stub/db.js +28 -0
- package/dist/stub/db.js.map +1 -0
- package/dist/stub/did.d.ts +5 -0
- package/dist/stub/did.d.ts.map +1 -0
- package/dist/stub/did.js +52 -0
- package/dist/stub/did.js.map +1 -0
- package/dist/stub/migrations/001_stub_schema.sql +51 -0
- package/package.json +34 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { buildTransaction, getAddressFromKey, addressFromPublicKey, hexToBytes, hexToArray, } from './transaction.js';
|
|
2
|
+
import bs58 from 'bs58';
|
|
3
|
+
// ---------- helpers ----------
|
|
4
|
+
/**
|
|
5
|
+
* Build the DID string from network and address.
|
|
6
|
+
* Format: did:solidus:{network}:{base58_address}
|
|
7
|
+
*/
|
|
8
|
+
function buildDidString(network, address) {
|
|
9
|
+
return `did:solidus:${network}:${address}`;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Poll for a transaction receipt, retrying every 500ms for up to 10 seconds.
|
|
13
|
+
*/
|
|
14
|
+
async function pollReceipt(rpc, txHash) {
|
|
15
|
+
const maxAttempts = 20; // 20 * 500ms = 10s
|
|
16
|
+
for (let i = 0; i < maxAttempts; i++) {
|
|
17
|
+
const receipt = await rpc.call('solidus_getReceipt', [txHash]);
|
|
18
|
+
if (receipt)
|
|
19
|
+
return receipt;
|
|
20
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
21
|
+
}
|
|
22
|
+
throw new Error(`Transaction ${txHash} not confirmed after 10 seconds`);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Convert an RPC DID document to the W3C DIDDocument format expected
|
|
26
|
+
* by the SolidusSDK interface.
|
|
27
|
+
*/
|
|
28
|
+
function mapToW3CDIDDocument(doc) {
|
|
29
|
+
const verificationMethod = doc.verification_method.map((vm) => ({
|
|
30
|
+
id: vm.id,
|
|
31
|
+
type: 'Ed25519VerificationKey2020',
|
|
32
|
+
controller: vm.controller,
|
|
33
|
+
publicKeyMultibase: 'z' + bs58.encode(hexToBytes(vm.publicKeyHex)),
|
|
34
|
+
}));
|
|
35
|
+
return {
|
|
36
|
+
'@context': [
|
|
37
|
+
'https://www.w3.org/ns/did/v1',
|
|
38
|
+
'https://w3id.org/security/suites/ed25519-2020/v1',
|
|
39
|
+
],
|
|
40
|
+
id: doc.id,
|
|
41
|
+
controller: doc.controller,
|
|
42
|
+
verificationMethod,
|
|
43
|
+
authentication: doc.authentication,
|
|
44
|
+
assertionMethod: doc.authentication, // mirror authentication
|
|
45
|
+
created: new Date(doc.created_ms).toISOString(),
|
|
46
|
+
updated: new Date(doc.updated_ms).toISOString(),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
// ---------- factory ----------
|
|
50
|
+
export function createChainDid(rpc, config) {
|
|
51
|
+
return {
|
|
52
|
+
async create(publicKey) {
|
|
53
|
+
// 1. Derive sender address from the signing key
|
|
54
|
+
const senderAddress = config.signerAddress ?? (await getAddressFromKey(config.signerPrivateKey));
|
|
55
|
+
// 2. Get current nonce
|
|
56
|
+
const nonce = await rpc.call('solidus_getNonce', [senderAddress]);
|
|
57
|
+
// 3. Build DidCreate payload (Rust serde tagged enum format)
|
|
58
|
+
const payload = {
|
|
59
|
+
DidCreate: {
|
|
60
|
+
public_key: hexToArray(publicKey),
|
|
61
|
+
service_endpoints: [],
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
// 4. Build and sign transaction
|
|
65
|
+
const tx = await buildTransaction(config.signerPrivateKey, nonce, payload);
|
|
66
|
+
// 5. Submit via RPC
|
|
67
|
+
const txHash = await rpc.call('solidus_sendTransaction', [JSON.stringify(tx)]);
|
|
68
|
+
// 6. Poll for receipt
|
|
69
|
+
const receipt = await pollReceipt(rpc, txHash);
|
|
70
|
+
if (!receipt.status.startsWith('success')) {
|
|
71
|
+
throw new Error(`DidCreate failed: ${receipt.status}`);
|
|
72
|
+
}
|
|
73
|
+
// 7. Build DID string from the public key that was registered
|
|
74
|
+
// The chain creates the DID using the public key's address
|
|
75
|
+
const pubKeyBytes = hexToBytes(publicKey);
|
|
76
|
+
const didAddress = addressFromPublicKey(pubKeyBytes);
|
|
77
|
+
const did = buildDidString(config.network, didAddress);
|
|
78
|
+
// 8. Resolve the created DID to get timestamps
|
|
79
|
+
const doc = await rpc.call('solidus_didResolve', [did]);
|
|
80
|
+
const now = new Date().toISOString();
|
|
81
|
+
return {
|
|
82
|
+
id: did,
|
|
83
|
+
controller: did,
|
|
84
|
+
created: doc ? new Date(doc.created_ms).toISOString() : now,
|
|
85
|
+
updated: doc ? new Date(doc.updated_ms).toISOString() : now,
|
|
86
|
+
network: config.network,
|
|
87
|
+
};
|
|
88
|
+
},
|
|
89
|
+
async resolve(did) {
|
|
90
|
+
const doc = await rpc.call('solidus_didResolve', [did]);
|
|
91
|
+
if (!doc)
|
|
92
|
+
return null;
|
|
93
|
+
if (!doc.active)
|
|
94
|
+
return null;
|
|
95
|
+
return mapToW3CDIDDocument(doc);
|
|
96
|
+
},
|
|
97
|
+
async deactivate(did, signerKey) {
|
|
98
|
+
// Derive address from the provided signing key
|
|
99
|
+
const address = await getAddressFromKey(signerKey);
|
|
100
|
+
const nonce = await rpc.call('solidus_getNonce', [address]);
|
|
101
|
+
const payload = { DidDeactivate: { did } };
|
|
102
|
+
const tx = await buildTransaction(signerKey, nonce, payload);
|
|
103
|
+
const txHash = await rpc.call('solidus_sendTransaction', [JSON.stringify(tx)]);
|
|
104
|
+
const receipt = await pollReceipt(rpc, txHash);
|
|
105
|
+
if (!receipt.status.startsWith('success')) {
|
|
106
|
+
throw new Error(`DidDeactivate failed: ${receipt.status}`);
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=did.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"did.js","sourceRoot":"","sources":["../../src/chain/did.ts"],"names":[],"mappings":"AAUA,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,oBAAoB,EACpB,UAAU,EACV,UAAU,GAEX,MAAM,kBAAkB,CAAA;AAEzB,OAAO,IAAI,MAAM,MAAM,CAAA;AA+BvB,gCAAgC;AAEhC;;;GAGG;AACH,SAAS,cAAc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kBAAkB,EAAE,CAAC,OAAO,CAAC,CAAC,CAAA;YAEnE,MAAM,OAAO,GAAG,EAAE,aAAa,EAAE,EAAE,GAAG,EAAE,EAAE,CAAA;YAC1C,MAAM,EAAE,GAAG,MAAM,gBAAgB,CAAC,SAAS,EAAE,KAAK,EAAE,OAAO,CAAC,CAAA;YAE5D,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,IAAI,CAAS,yBAAyB,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;YACtF,MAAM,OAAO,GAAG,MAAM,WAAW,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;YAC9C,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;gBAC1C,MAAM,IAAI,KAAK,CAAC,yBAAyB,OAAO,CAAC,MAAM,EAAE,CAAC,CAAA;YAC5D,CAAC;QACH,CAAC;KACF,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SolidusSDK } from '../index.js';
|
|
2
|
+
export interface ChainConfig {
|
|
3
|
+
/** JSON-RPC endpoint URL of the Solidus chain node. */
|
|
4
|
+
rpcUrl: string;
|
|
5
|
+
/** Hex-encoded 32-byte Ed25519 private key used for signing transactions. */
|
|
6
|
+
signerPrivateKey: string;
|
|
7
|
+
/** Network identifier (used in DID strings). */
|
|
8
|
+
network: 'testnet' | 'mainnet';
|
|
9
|
+
/** Base58 sender address. Computed from signerPrivateKey if not provided. */
|
|
10
|
+
signerAddress?: string;
|
|
11
|
+
}
|
|
12
|
+
export declare function createChainClient(config: ChainConfig): SolidusSDK;
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/chain/index.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAE7C,MAAM,WAAW,WAAW;IAC1B,uDAAuD;IACvD,MAAM,EAAE,MAAM,CAAA;IACd,6EAA6E;IAC7E,gBAAgB,EAAE,MAAM,CAAA;IACxB,gDAAgD;IAChD,OAAO,EAAE,SAAS,GAAG,SAAS,CAAA;IAC9B,6EAA6E;IAC7E,aAAa,CAAC,EAAE,MAAM,CAAA;CACvB;AAED,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,WAAW,GAAG,UAAU,CAOjE"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chain-backed SolidusSDK client factory.
|
|
3
|
+
*
|
|
4
|
+
* Creates an SDK instance that communicates with the Solidus blockchain
|
|
5
|
+
* via JSON-RPC, replacing the PostgreSQL-backed stub for testnet/mainnet.
|
|
6
|
+
*/
|
|
7
|
+
import { RpcClient } from './rpc.js';
|
|
8
|
+
import { createChainDid } from './did.js';
|
|
9
|
+
import { createChainCredentials } from './credentials.js';
|
|
10
|
+
import { createChainAuth } from './auth.js';
|
|
11
|
+
export function createChainClient(config) {
|
|
12
|
+
const rpc = new RpcClient(config.rpcUrl);
|
|
13
|
+
return {
|
|
14
|
+
did: createChainDid(rpc, config),
|
|
15
|
+
credentials: createChainCredentials(rpc, config),
|
|
16
|
+
auth: createChainAuth(rpc, config),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/chain/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AACpC,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAA;AACzC,OAAO,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAA;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA;AAe3C,MAAM,UAAU,iBAAiB,CAAC,MAAmB;IACnD,MAAM,GAAG,GAAG,IAAI,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;IACxC,OAAO;QACL,GAAG,EAAE,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC;QAChC,WAAW,EAAE,sBAAsB,CAAC,GAAG,EAAE,MAAM,CAAC;QAChD,IAAI,EAAE,eAAe,CAAC,GAAG,EAAE,MAAM,CAAC;KACnC,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON-RPC 2.0 HTTP client for communicating with the Solidus chain node.
|
|
3
|
+
*/
|
|
4
|
+
export declare class RpcClient {
|
|
5
|
+
private rpcUrl;
|
|
6
|
+
constructor(rpcUrl: string);
|
|
7
|
+
call<T>(method: string, params: unknown[]): Promise<T>;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=rpc.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rpc.d.ts","sourceRoot":"","sources":["../../src/chain/rpc.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,SAAS;IACR,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,MAAM;IAE5B,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,CAAC,CAAC;CAiB7D"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON-RPC 2.0 HTTP client for communicating with the Solidus chain node.
|
|
3
|
+
*/
|
|
4
|
+
export class RpcClient {
|
|
5
|
+
rpcUrl;
|
|
6
|
+
constructor(rpcUrl) {
|
|
7
|
+
this.rpcUrl = rpcUrl;
|
|
8
|
+
}
|
|
9
|
+
async call(method, params) {
|
|
10
|
+
const res = await fetch(this.rpcUrl, {
|
|
11
|
+
method: 'POST',
|
|
12
|
+
headers: { 'Content-Type': 'application/json' },
|
|
13
|
+
body: JSON.stringify({ jsonrpc: '2.0', method, params, id: Date.now() }),
|
|
14
|
+
});
|
|
15
|
+
if (!res.ok) {
|
|
16
|
+
throw new Error(`RPC HTTP error: ${res.status} ${res.statusText}`);
|
|
17
|
+
}
|
|
18
|
+
const json = (await res.json());
|
|
19
|
+
if (json.error) {
|
|
20
|
+
throw new Error(`RPC error ${json.error.code}: ${json.error.message}`);
|
|
21
|
+
}
|
|
22
|
+
return json.result;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=rpc.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rpc.js","sourceRoot":"","sources":["../../src/chain/rpc.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,OAAO,SAAS;IACA;IAApB,YAAoB,MAAc;QAAd,WAAM,GAAN,MAAM,CAAQ;IAAG,CAAC;IAEtC,KAAK,CAAC,IAAI,CAAI,MAAc,EAAE,MAAiB;QAC7C,MAAM,GAAG,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE;YACnC,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;YAC/C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC;SACzE,CAAC,CAAA;QAEF,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CAAC,mBAAmB,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,UAAU,EAAE,CAAC,CAAA;QACpE,CAAC;QAED,MAAM,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAA8D,CAAA;QAC5F,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CAAC,aAAa,IAAI,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAA;QACxE,CAAC;QACD,OAAO,IAAI,CAAC,MAAW,CAAA;IACzB,CAAC;CACF"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface ChainTransaction {
|
|
2
|
+
sender_pubkey: number[];
|
|
3
|
+
nonce: number;
|
|
4
|
+
payload: unknown;
|
|
5
|
+
signature: number[];
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Build and sign a transaction for the Solidus chain.
|
|
9
|
+
*/
|
|
10
|
+
export declare function buildTransaction(privateKeyHex: string, nonce: number, payload: unknown): Promise<ChainTransaction>;
|
|
11
|
+
/**
|
|
12
|
+
* Derive the Solidus address from a private key.
|
|
13
|
+
* Algorithm: BLAKE3(publicKey) -> take first 20 bytes -> base58 encode.
|
|
14
|
+
* Matches Rust Address::from_public_key.
|
|
15
|
+
*/
|
|
16
|
+
export declare function getAddressFromKey(privateKeyHex: string): Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* Derive the Solidus address from a raw public key (Uint8Array).
|
|
19
|
+
*/
|
|
20
|
+
export declare function addressFromPublicKey(publicKey: Uint8Array): string;
|
|
21
|
+
export declare function hexToBytes(hex: string): Uint8Array;
|
|
22
|
+
export declare function bytesToHex(bytes: Uint8Array): string;
|
|
23
|
+
/**
|
|
24
|
+
* Convert a hex-encoded public key to a number[] array (for JSON payloads).
|
|
25
|
+
*/
|
|
26
|
+
export declare function hexToArray(hex: string): number[];
|
|
27
|
+
//# sourceMappingURL=transaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction.d.ts","sourceRoot":"","sources":["../../src/chain/transaction.ts"],"names":[],"mappings":"AAmBA,MAAM,WAAW,gBAAgB;IAC/B,aAAa,EAAE,MAAM,EAAE,CAAA;IACvB,KAAK,EAAE,MAAM,CAAA;IACb,OAAO,EAAE,OAAO,CAAA;IAChB,SAAS,EAAE,MAAM,EAAE,CAAA;CACpB;AAED;;GAEG;AACH,wBAAsB,gBAAgB,CACpC,aAAa,EAAE,MAAM,EACrB,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,OAAO,GACf,OAAO,CAAC,gBAAgB,CAAC,CAuB3B;AAED;;;;GAIG;AACH,wBAAsB,iBAAiB,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAI9E;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,UAAU,GAAG,MAAM,CAIlE;AAID,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAMlD;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAIpD;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CAEhD"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transaction building and Ed25519 signing for the Solidus chain.
|
|
3
|
+
*
|
|
4
|
+
* Matches the Rust chain's expected format:
|
|
5
|
+
* - sender_pubkey: [u8; 32] (JSON array of numbers)
|
|
6
|
+
* - nonce: u64
|
|
7
|
+
* - payload: TxPayload (serde tagged enum, e.g. {"DidCreate": {...}})
|
|
8
|
+
* - signature: [u8; 64] (JSON array of numbers)
|
|
9
|
+
*
|
|
10
|
+
* signing_bytes = BLAKE3(sender_pubkey || nonce_le_bytes || serde_json(payload))
|
|
11
|
+
*/
|
|
12
|
+
import * as ed from '@noble/ed25519';
|
|
13
|
+
import { sha512 } from '@noble/hashes/sha512';
|
|
14
|
+
import { blake3 } from '@noble/hashes/blake3';
|
|
15
|
+
import bs58 from 'bs58';
|
|
16
|
+
// Configure ed25519 v2 to use sha512 for sync operations
|
|
17
|
+
ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m));
|
|
18
|
+
/**
|
|
19
|
+
* Build and sign a transaction for the Solidus chain.
|
|
20
|
+
*/
|
|
21
|
+
export async function buildTransaction(privateKeyHex, nonce, payload) {
|
|
22
|
+
const privateKey = hexToBytes(privateKeyHex);
|
|
23
|
+
const publicKey = await ed.getPublicKeyAsync(privateKey);
|
|
24
|
+
// Build signing bytes: BLAKE3(pubkey || nonce_le || json(payload))
|
|
25
|
+
const nonceBytes = new Uint8Array(8);
|
|
26
|
+
new DataView(nonceBytes.buffer).setBigUint64(0, BigInt(nonce), true); // little-endian
|
|
27
|
+
const payloadBytes = new TextEncoder().encode(JSON.stringify(payload));
|
|
28
|
+
const combined = new Uint8Array(publicKey.length + nonceBytes.length + payloadBytes.length);
|
|
29
|
+
combined.set(publicKey, 0);
|
|
30
|
+
combined.set(nonceBytes, publicKey.length);
|
|
31
|
+
combined.set(payloadBytes, publicKey.length + nonceBytes.length);
|
|
32
|
+
const signingHash = blake3(combined);
|
|
33
|
+
const signature = await ed.signAsync(signingHash, privateKey);
|
|
34
|
+
return {
|
|
35
|
+
sender_pubkey: Array.from(publicKey),
|
|
36
|
+
nonce,
|
|
37
|
+
payload,
|
|
38
|
+
signature: Array.from(signature),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Derive the Solidus address from a private key.
|
|
43
|
+
* Algorithm: BLAKE3(publicKey) -> take first 20 bytes -> base58 encode.
|
|
44
|
+
* Matches Rust Address::from_public_key.
|
|
45
|
+
*/
|
|
46
|
+
export async function getAddressFromKey(privateKeyHex) {
|
|
47
|
+
const privateKey = hexToBytes(privateKeyHex);
|
|
48
|
+
const publicKey = await ed.getPublicKeyAsync(privateKey);
|
|
49
|
+
return addressFromPublicKey(publicKey);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Derive the Solidus address from a raw public key (Uint8Array).
|
|
53
|
+
*/
|
|
54
|
+
export function addressFromPublicKey(publicKey) {
|
|
55
|
+
const hash = blake3(publicKey);
|
|
56
|
+
const addressBytes = hash.slice(0, 20);
|
|
57
|
+
return bs58.encode(addressBytes);
|
|
58
|
+
}
|
|
59
|
+
// ---------- hex utilities ----------
|
|
60
|
+
export function hexToBytes(hex) {
|
|
61
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
62
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
63
|
+
bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
64
|
+
}
|
|
65
|
+
return bytes;
|
|
66
|
+
}
|
|
67
|
+
export function bytesToHex(bytes) {
|
|
68
|
+
return Array.from(bytes)
|
|
69
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
70
|
+
.join('');
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Convert a hex-encoded public key to a number[] array (for JSON payloads).
|
|
74
|
+
*/
|
|
75
|
+
export function hexToArray(hex) {
|
|
76
|
+
return Array.from(hexToBytes(hex));
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=transaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction.js","sourceRoot":"","sources":["../../src/chain/transaction.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,KAAK,EAAE,MAAM,gBAAgB,CAAA;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAC7C,OAAO,IAAI,MAAM,MAAM,CAAA;AAEvB,yDAAyD;AACzD,EAAE,CAAC,GAAG,CAAC,UAAU,GAAG,CAAC,GAAG,CAAe,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;AAS5E;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,aAAqB,EACrB,KAAa,EACb,OAAgB;IAEhB,MAAM,UAAU,GAAG,UAAU,CAAC,aAAa,CAAC,CAAA;IAC5C,MAAM,SAAS,GAAG,MAAM,EAAE,CAAC,iBAAiB,CAAC,UAA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CAAC,CAAA;AACxC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAAC,SAAqB;IACxD,MAAM,IAAI,GAAG,MAAM,CAAC,SAAS,CAAC,CAAA;IAC9B,MAAM,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;IACtC,OAAO,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,CAAA;AAClC,CAAC;AAED,sCAAsC;AAEtC,MAAM,UAAU,UAAU,CAAC,GAAW;IACpC,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;IAC5C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QACvC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;IACtD,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,KAAiB;IAC1C,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;SACrB,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;SAC3C,IAAI,CAAC,EAAE,CAAC,CAAA;AACb,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,UAAU,CAAC,GAAW;IACpC,OAAO,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAA;AACpC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { DID, DIDDocument, VerifiableCredential, IssueCredentialParams, VerificationResult, AuthResult } from '@solidus-network/types';
|
|
2
|
+
export type { DID, DIDDocument, VerifiableCredential, IssueCredentialParams, VerificationResult, AuthResult, };
|
|
3
|
+
export { runMigrations, closeConnection } from './stub/db.js';
|
|
4
|
+
export { generateKeypair, decodePublicKey } from './stub/crypto.js';
|
|
5
|
+
export type { ChainConfig } from './chain/index.js';
|
|
6
|
+
export interface SolidusSDK {
|
|
7
|
+
did: {
|
|
8
|
+
create(publicKey: string): Promise<DID>;
|
|
9
|
+
resolve(did: string): Promise<DIDDocument | null>;
|
|
10
|
+
deactivate(did: string, signerKey: string): Promise<void>;
|
|
11
|
+
};
|
|
12
|
+
credentials: {
|
|
13
|
+
issue(params: IssueCredentialParams): Promise<VerifiableCredential>;
|
|
14
|
+
verify(vcId: string): Promise<VerificationResult>;
|
|
15
|
+
revoke(credentialId: string, issuerKey: string): Promise<void>;
|
|
16
|
+
query(subjectDid: string): Promise<VerifiableCredential[]>;
|
|
17
|
+
};
|
|
18
|
+
auth: {
|
|
19
|
+
createChallenge(domain: string): Promise<string>;
|
|
20
|
+
verifyPresentation(vp: string, challenge: string, domain: string): Promise<AuthResult>;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export interface SolidusConfig {
|
|
24
|
+
mode: 'stub' | 'testnet' | 'mainnet';
|
|
25
|
+
/** JSON-RPC URL for testnet/mainnet modes (default: http://127.0.0.1:9944) */
|
|
26
|
+
rpcUrl?: string;
|
|
27
|
+
/** Hex-encoded Ed25519 private key for testnet/mainnet modes */
|
|
28
|
+
signerPrivateKey?: string;
|
|
29
|
+
}
|
|
30
|
+
export declare function createSdk(config: SolidusConfig): SolidusSDK;
|
|
31
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,GAAG,EAAE,WAAW,EAAE,oBAAoB,EACtC,qBAAqB,EAAE,kBAAkB,EAAE,UAA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AAI,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,CAAA;IACpC,8EAA8E;IAC9E,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,gEAAgE;IAChE,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAC1B;AAkCD,wBAAgB,SAAS,CAAC,MAAM,EAAE,aAAa,GAAG,UAAU,CAiB3D"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { didCreate, didResolve, didDeactivate } from './stub/did.js';
|
|
3
|
+
import { credentialsIssue, credentialsVerify, credentialsRevoke, credentialsQuery } from './stub/credentials.js';
|
|
4
|
+
import { getSql } from './stub/db.js';
|
|
5
|
+
import { createChainClient } from './chain/index.js';
|
|
6
|
+
export { runMigrations, closeConnection } from './stub/db.js';
|
|
7
|
+
export { generateKeypair, decodePublicKey } from './stub/crypto.js';
|
|
8
|
+
function createStubSdk() {
|
|
9
|
+
return {
|
|
10
|
+
did: {
|
|
11
|
+
create: didCreate,
|
|
12
|
+
resolve: didResolve,
|
|
13
|
+
deactivate: didDeactivate,
|
|
14
|
+
},
|
|
15
|
+
credentials: {
|
|
16
|
+
issue: credentialsIssue,
|
|
17
|
+
verify: credentialsVerify,
|
|
18
|
+
revoke: credentialsRevoke,
|
|
19
|
+
query: credentialsQuery,
|
|
20
|
+
},
|
|
21
|
+
auth: {
|
|
22
|
+
async createChallenge(domain) {
|
|
23
|
+
const sql = getSql();
|
|
24
|
+
const challenge = randomUUID();
|
|
25
|
+
await sql `INSERT INTO stub_challenges (challenge, domain) VALUES (${challenge}, ${domain})`;
|
|
26
|
+
return challenge;
|
|
27
|
+
},
|
|
28
|
+
async verifyPresentation(_vp, _challenge, _domain) {
|
|
29
|
+
// Real VP verification ships with Identity (Phase 2)
|
|
30
|
+
return { valid: true, claims: {} };
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export function createSdk(config) {
|
|
36
|
+
const mode = config.mode ?? (process.env['SOLIDUS_SDK_MODE'] ?? 'stub');
|
|
37
|
+
switch (mode) {
|
|
38
|
+
case 'testnet':
|
|
39
|
+
case 'mainnet': {
|
|
40
|
+
const rpcUrl = config.rpcUrl ?? process.env['SOLIDUS_RPC_URL'] ?? 'http://127.0.0.1:9944';
|
|
41
|
+
const signerKey = config.signerPrivateKey ?? process.env['SOLIDUS_SIGNER_KEY'] ?? '';
|
|
42
|
+
if (!signerKey) {
|
|
43
|
+
throw new Error('signerPrivateKey or SOLIDUS_SIGNER_KEY env var required for chain mode');
|
|
44
|
+
}
|
|
45
|
+
return createChainClient({ rpcUrl, signerPrivateKey: signerKey, network: mode });
|
|
46
|
+
}
|
|
47
|
+
case 'stub':
|
|
48
|
+
default:
|
|
49
|
+
return createStubSdk();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAKxC,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AACpE,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAChH,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,EAAE,iBAAiB,EAAoB,MAAM,kBAAkB,CAAA;AAMtE,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAC7D,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AA6BnE,SAAS,aAAa;IACpB,OAAO;QACL,GAAG,EAAE;YACH,MAAM,EAAE,SAAS;YACjB,OAAO,EAAE,UAAU;YACnB,UAAU,EAAE,a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AA;QAClF,CAAC;QACD,KAAK,MAAM,CAAC;QACZ;YACE,OAAO,aAAa,EAAE,CAAA;IAC1B,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { VerifiableCredential, IssueCredentialParams, VerificationResult } from '@solidus-network/types';
|
|
2
|
+
export declare function credentialsIssue(params: IssueCredentialParams): Promise<VerifiableCredential>;
|
|
3
|
+
export declare function credentialsVerify(vcId: string): Promise<VerificationResult>;
|
|
4
|
+
export declare function credentialsRevoke(credentialId: string, _issuerKey: string): Promise<void>;
|
|
5
|
+
export declare function credentialsQuery(subjectDid: string): Promise<VerifiableCredential[]>;
|
|
6
|
+
//# sourceMappingURL=credentials.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credentials.d.ts","sourceRoot":"","sources":["../../src/stub/credentials.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AAqDrG,wBAAsB,gBAAgB,CAAC,MAAM,EAAE,qBAAqB,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAsCnG;AAED,wBAAsB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAuCjF;AAED,wBAAsB,iBAAiB,CAAC,YAAY,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAU/F;AAED,wBAAsB,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,oBAAoB,EAAE,CAAC,CAQ1F"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { getSql } from './db.js';
|
|
3
|
+
import { sign, verify as cryptoVerify } from './crypto.js';
|
|
4
|
+
function rowToVC(row) {
|
|
5
|
+
return {
|
|
6
|
+
'@context': ['https://www.w3.org/2018/credentials/v1'],
|
|
7
|
+
id: row.credential_id,
|
|
8
|
+
type: row.type,
|
|
9
|
+
issuer: row.issuer_did,
|
|
10
|
+
issuanceDate: row.issued_at.toISOString(),
|
|
11
|
+
...(row.expires_at && { expirationDate: row.expires_at.toISOString() }),
|
|
12
|
+
credentialSubject: { id: row.subject_did, ...row.claims },
|
|
13
|
+
proof: {
|
|
14
|
+
type: 'Ed25519Signature2020',
|
|
15
|
+
created: row.issued_at.toISOString(),
|
|
16
|
+
verificationMethod: `${row.issuer_did}#key-1`,
|
|
17
|
+
proofPurpose: 'assertionMethod',
|
|
18
|
+
proofValue: row.proof_value,
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function buildPayload(params) {
|
|
23
|
+
return new TextEncoder().encode(JSON.stringify({
|
|
24
|
+
id: params.credentialId,
|
|
25
|
+
subject: params.subjectDid,
|
|
26
|
+
issuer: params.issuerDid,
|
|
27
|
+
claims: params.claims,
|
|
28
|
+
issuedAt: params.issuedAt,
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
export async function credentialsIssue(params) {
|
|
32
|
+
const sql = getSql();
|
|
33
|
+
const credentialId = `vc:solidus:stub:${randomUUID()}`;
|
|
34
|
+
const now = new Date();
|
|
35
|
+
const expiresAt = params.expiresInDays
|
|
36
|
+
? new Date(now.getTime() + params.expiresInDays * 86_400_000)
|
|
37
|
+
: null;
|
|
38
|
+
const payload = buildPayload({
|
|
39
|
+
credentialId,
|
|
40
|
+
subjectDid: params.subjectDid,
|
|
41
|
+
issuerDid: params.issuerDid,
|
|
42
|
+
claims: params.claims,
|
|
43
|
+
issuedAt: now.toISOString(),
|
|
44
|
+
});
|
|
45
|
+
const proofValue = await sign(payload, params.issuerPrivateKey);
|
|
46
|
+
await sql `
|
|
47
|
+
INSERT INTO stub_credentials
|
|
48
|
+
(credential_id, subject_did, issuer_did, type, claims, proof_value, issued_at, expires_at)
|
|
49
|
+
VALUES (
|
|
50
|
+
${credentialId}, ${params.subjectDid}, ${params.issuerDid},
|
|
51
|
+
${sql.array(params.type)}, ${sql.json(params.claims)},
|
|
52
|
+
${proofValue}, ${now}, ${expiresAt}
|
|
53
|
+
)
|
|
54
|
+
`;
|
|
55
|
+
return rowToVC({
|
|
56
|
+
credential_id: credentialId,
|
|
57
|
+
subject_did: params.subjectDid,
|
|
58
|
+
issuer_did: params.issuerDid,
|
|
59
|
+
type: params.type,
|
|
60
|
+
claims: params.claims,
|
|
61
|
+
proof_value: proofValue,
|
|
62
|
+
issued_at: now,
|
|
63
|
+
expires_at: expiresAt,
|
|
64
|
+
revoked: false,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
export async function credentialsVerify(vcId) {
|
|
68
|
+
const sql = getSql();
|
|
69
|
+
const rows = await sql `
|
|
70
|
+
SELECT * FROM stub_credentials WHERE credential_id = ${vcId}
|
|
71
|
+
`;
|
|
72
|
+
if (rows.length === 0) {
|
|
73
|
+
return {
|
|
74
|
+
valid: false,
|
|
75
|
+
error: 'Credential not found',
|
|
76
|
+
checks: { signature: false, expiry: false, revocation: false },
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const row = rows[0];
|
|
80
|
+
const notRevoked = !row.revoked;
|
|
81
|
+
const notExpired = !row.expires_at || row.expires_at > new Date();
|
|
82
|
+
const issuerRows = await sql `
|
|
83
|
+
SELECT public_key FROM stub_dids WHERE did = ${row.issuer_did}
|
|
84
|
+
`;
|
|
85
|
+
let sigValid = false;
|
|
86
|
+
if (issuerRows.length > 0) {
|
|
87
|
+
const payload = buildPayload({
|
|
88
|
+
credentialId: row.credential_id,
|
|
89
|
+
subjectDid: row.subject_did,
|
|
90
|
+
issuerDid: row.issuer_did,
|
|
91
|
+
claims: row.claims,
|
|
92
|
+
issuedAt: row.issued_at.toISOString(),
|
|
93
|
+
});
|
|
94
|
+
sigValid = await cryptoVerify(payload, row.proof_value, issuerRows[0].public_key);
|
|
95
|
+
}
|
|
96
|
+
const valid = sigValid && notRevoked && notExpired;
|
|
97
|
+
return {
|
|
98
|
+
valid,
|
|
99
|
+
credentialId: vcId,
|
|
100
|
+
checks: { signature: sigValid, expiry: notExpired, revocation: notRevoked },
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
export async function credentialsRevoke(credentialId, _issuerKey) {
|
|
104
|
+
const sql = getSql();
|
|
105
|
+
await sql `
|
|
106
|
+
UPDATE stub_credentials SET revoked = true, revoked_at = now()
|
|
107
|
+
WHERE credential_id = ${credentialId}
|
|
108
|
+
`;
|
|
109
|
+
await sql `
|
|
110
|
+
INSERT INTO stub_revocations (credential_id) VALUES (${credentialId})
|
|
111
|
+
ON CONFLICT DO NOTHING
|
|
112
|
+
`;
|
|
113
|
+
}
|
|
114
|
+
export async function credentialsQuery(subjectDid) {
|
|
115
|
+
const sql = getSql();
|
|
116
|
+
const rows = await sql `
|
|
117
|
+
SELECT * FROM stub_credentials
|
|
118
|
+
WHERE subject_did = ${subjectDid} AND revoked = false
|
|
119
|
+
ORDER BY issued_at DESC
|
|
120
|
+
`;
|
|
121
|
+
return rows.map(rowToVC);
|
|
122
|
+
}
|
|
123
|
+
//# sourceMappingURL=credentials.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credentials.js","sourceRoot":"","sources":["../../src/stub/credentials.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAGxC,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAChC,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,YAAY,EAAE,MAAM,aAAa,CAAA;AAc1D,SAAS,OAAO,CAAC,GAAkB;IACjC,OAAO;QACL,UAAU,EAAE,CAAC,wCAAwC,CAAC;QACtD,EAAE,EAAE,GAAG,CAAC,aAAa;QACrB,IAAI,EAAE,GAAG,CAAC,IAAI;QACd,MAAM,EAAE,GAAG,CAAC,UAAU;QACtB,YAAY,EAAE,GAAG,CAAC,SAAS,CAAC,WAAW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AM,GAAG,GAAG,MAAM,EAAE,CAAA;IACpB,MAAM,IAAI,GAAG,MAAM,GAAG,CAAiB;;0BAEf,UAAU;;GAEjC,CAAA;IACD,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;AAC1B,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare function generateKeypair(): Promise<{
|
|
2
|
+
privateKey: string;
|
|
3
|
+
publicKey: string;
|
|
4
|
+
}>;
|
|
5
|
+
export declare function sign(message: Uint8Array, privateKeyHex: string): Promise<string>;
|
|
6
|
+
export declare function verify(message: Uint8Array, signatureBase64url: string, publicKeyHex: string): Promise<boolean>;
|
|
7
|
+
/** Encode public key as multibase base58btc (W3C Ed25519VerificationKey2020) */
|
|
8
|
+
export declare function encodePublicKey(publicKeyHex: string): string;
|
|
9
|
+
export declare function decodePublicKey(multibase: string): string;
|
|
10
|
+
//# sourceMappingURL=crypto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../src/stub/crypto.ts"],"names":[],"mappings":"AAOA,wBAAsB,eAAe,IAAI,OAAO,CAAC;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC,CAO1F;AAED,wBAAsB,IAAI,CAAC,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAItF;AAED,wBAAsB,MAAM,CAC1B,OAAO,EAAE,UAAU,EACnB,kBAAkB,EAAE,MAAM,EAC1B,YAAY,EAAE,MAAM,GACnB,OAAO,CAAC,OAAO,CAAC,CAQlB;AAED,gFAAgF;AAChF,wBAAgB,eAAe,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAG5D;AAED,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAGzD"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import * as ed from '@noble/ed25519';
|
|
2
|
+
import { sha512 } from '@noble/hashes/sha512';
|
|
3
|
+
import bs58 from 'bs58';
|
|
4
|
+
// @noble/ed25519 v2 requires sha512 to be configured for sync operations
|
|
5
|
+
ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m));
|
|
6
|
+
export async function generateKeypair() {
|
|
7
|
+
const privateKeyBytes = ed.utils.randomPrivateKey();
|
|
8
|
+
const publicKeyBytes = await ed.getPublicKeyAsync(privateKeyBytes);
|
|
9
|
+
return {
|
|
10
|
+
privateKey: Buffer.from(privateKeyBytes).toString('hex'),
|
|
11
|
+
publicKey: Buffer.from(publicKeyBytes).toString('hex'),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export async function sign(message, privateKeyHex) {
|
|
15
|
+
const privateKeyBytes = Buffer.from(privateKeyHex, 'hex');
|
|
16
|
+
const sig = await ed.signAsync(message, privateKeyBytes);
|
|
17
|
+
return Buffer.from(sig).toString('base64url');
|
|
18
|
+
}
|
|
19
|
+
export async function verify(message, signatureBase64url, publicKeyHex) {
|
|
20
|
+
try {
|
|
21
|
+
const sig = Buffer.from(signatureBase64url, 'base64url');
|
|
22
|
+
const pub = Buffer.from(publicKeyHex, 'hex');
|
|
23
|
+
return await ed.verifyAsync(sig, message, pub);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** Encode public key as multibase base58btc (W3C Ed25519VerificationKey2020) */
|
|
30
|
+
export function encodePublicKey(publicKeyHex) {
|
|
31
|
+
const bytes = Buffer.from(publicKeyHex, 'hex');
|
|
32
|
+
return 'z' + bs58.encode(bytes);
|
|
33
|
+
}
|
|
34
|
+
export function decodePublicKey(multibase) {
|
|
35
|
+
const bytes = bs58.decode(multibase.slice(1));
|
|
36
|
+
return Buffer.from(bytes).toString('hex');
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=crypto.js.map
|
|
@@ -0,0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