@coinbase/cdp-sdk 1.14.0 → 1.15.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/CHANGELOG.md +6 -0
- package/README.md +31 -0
- package/_cjs/client/evm/evm.js +43 -0
- package/_cjs/client/evm/evm.js.map +1 -1
- package/_cjs/client/solana/solana.js +43 -0
- package/_cjs/client/solana/solana.js.map +1 -1
- package/_cjs/utils/export.js +84 -0
- package/_cjs/utils/export.js.map +1 -0
- package/_cjs/version.js +1 -1
- package/_esm/client/evm/evm.js +44 -1
- package/_esm/client/evm/evm.js.map +1 -1
- package/_esm/client/solana/solana.js +43 -0
- package/_esm/client/solana/solana.js.map +1 -1
- package/_esm/utils/export.js +75 -0
- package/_esm/utils/export.js.map +1 -0
- package/_esm/version.js +1 -1
- package/_types/client/cdp.d.ts +1 -2
- package/_types/client/cdp.d.ts.map +1 -1
- package/_types/client/evm/evm.d.ts +29 -3
- package/_types/client/evm/evm.d.ts.map +1 -1
- package/_types/client/evm/evm.types.d.ts +13 -1
- package/_types/client/evm/evm.types.d.ts.map +1 -1
- package/_types/client/solana/solana.d.ts +26 -1
- package/_types/client/solana/solana.d.ts.map +1 -1
- package/_types/client/solana/solana.types.d.ts +12 -0
- package/_types/client/solana/solana.types.d.ts.map +1 -1
- package/_types/utils/export.d.ts +30 -0
- package/_types/utils/export.d.ts.map +1 -0
- package/_types/version.d.ts +1 -1
- package/client/cdp.ts +1 -1
- package/client/evm/evm.ts +82 -25
- package/client/evm/evm.types.ts +15 -0
- package/client/solana/solana.ts +61 -0
- package/client/solana/solana.types.ts +15 -2
- package/package.json +1 -1
- package/utils/export.ts +89 -0
- package/version.ts +1 -1
package/utils/export.ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { generateKeyPair, privateDecrypt, constants, createPrivateKey } from "crypto";
|
|
2
|
+
|
|
3
|
+
import { Keypair } from "@solana/web3.js";
|
|
4
|
+
import bs58 from "bs58";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Generates a new RSA key pair with 4096-bit private key.
|
|
8
|
+
* - Private key in PKCS1 DER format
|
|
9
|
+
* - Public key in PKIX/SPKI DER format
|
|
10
|
+
*
|
|
11
|
+
* @returns A promise that resolves to the generated key pair, or rejects with an error.
|
|
12
|
+
*/
|
|
13
|
+
export const generateExportEncryptionKeyPair = async () => {
|
|
14
|
+
return await new Promise<{
|
|
15
|
+
publicKey: string;
|
|
16
|
+
privateKey: string;
|
|
17
|
+
}>((resolve, reject) => {
|
|
18
|
+
generateKeyPair(
|
|
19
|
+
"rsa",
|
|
20
|
+
{
|
|
21
|
+
modulusLength: 4096,
|
|
22
|
+
publicKeyEncoding: {
|
|
23
|
+
type: "spki",
|
|
24
|
+
format: "der",
|
|
25
|
+
},
|
|
26
|
+
privateKeyEncoding: {
|
|
27
|
+
type: "pkcs1",
|
|
28
|
+
format: "der",
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
(err, publicKey, privateKey) => {
|
|
32
|
+
if (err) {
|
|
33
|
+
reject(err);
|
|
34
|
+
}
|
|
35
|
+
resolve({
|
|
36
|
+
publicKey: publicKey.toString("base64"),
|
|
37
|
+
privateKey: privateKey.toString("base64"),
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Decrypts a ciphertext using RSA-OAEP-SHA256.
|
|
46
|
+
* - Parses PKCS1 private key
|
|
47
|
+
* - Uses RSA-OAEP-SHA256 for decryption
|
|
48
|
+
* - Returns hex-encoded result
|
|
49
|
+
*
|
|
50
|
+
* @param b64PrivateKey - The base64-encoded private key in PKCS1 DER format.
|
|
51
|
+
* @param b64Cipher - The base64-encoded ciphertext.
|
|
52
|
+
* @returns The decrypted key hex string, or throws an error if decryption fails.
|
|
53
|
+
*/
|
|
54
|
+
export const decryptWithPrivateKey = (b64PrivateKey: string, b64Cipher: string): string => {
|
|
55
|
+
try {
|
|
56
|
+
// Create a private key object from the PKCS1 DER format
|
|
57
|
+
const privateKey = createPrivateKey({
|
|
58
|
+
key: Buffer.from(b64PrivateKey, "base64"),
|
|
59
|
+
format: "der",
|
|
60
|
+
type: "pkcs1",
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const decryptedBuffer = privateDecrypt(
|
|
64
|
+
{
|
|
65
|
+
key: privateKey,
|
|
66
|
+
padding: constants.RSA_PKCS1_OAEP_PADDING,
|
|
67
|
+
oaepHash: "sha256",
|
|
68
|
+
},
|
|
69
|
+
Buffer.from(b64Cipher, "base64"),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return decryptedBuffer.toString("hex");
|
|
73
|
+
} catch (error) {
|
|
74
|
+
throw new Error(`Decryption failed: ${String(error)}`);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Format a private key to a base58 string for easy import into Solana wallet apps.
|
|
80
|
+
*
|
|
81
|
+
* @param privateKey - The private key as a hex string
|
|
82
|
+
* @returns The formatted private key as a base58 string
|
|
83
|
+
*/
|
|
84
|
+
export const formatSolanaPrivateKey = (privateKey: string): string => {
|
|
85
|
+
const privateKeyBytes = Buffer.from(privateKey, "hex");
|
|
86
|
+
const keypair = Keypair.fromSeed(privateKeyBytes);
|
|
87
|
+
const fullKey = Buffer.concat([keypair.secretKey.subarray(0, 32), keypair.publicKey.toBytes()]);
|
|
88
|
+
return bs58.encode(fullKey);
|
|
89
|
+
};
|
package/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "1.
|
|
1
|
+
export const version = "1.15.0";
|