11/*
22
3- ConvexAccount .ts class to manage private/public key for signing hashed text on the Convex Network.
3+ KeyPair .ts class to manage private/public key for signing hashed text on the Convex Network.
44
55
66*/
@@ -11,20 +11,29 @@ import pem from 'pem-file'
1111
1212import { toPublicKeyChecksum } from './Utils'
1313
14- export class ConvexAccount {
14+ export class KeyPair {
1515 readonly privateKey : KeyObject // private key object
1616 readonly publicKey : KeyObject // public key object
17- readonly address : BigInt // address for this account
18- readonly name : string // name of the registered account
1917 readonly publicKeyAPI : string // address as hex string without leading '0x'
2018 readonly publicKeyChecksum : string // address as hex string with checksum upper an lower case hex letters
2119
20+ constructor ( publicKey : KeyObject , privateKey : KeyObject ) {
21+ this . publicKey = publicKey
22+ this . privateKey = privateKey
23+ const exportPublicKey = publicKey . export ( {
24+ type : 'spki' ,
25+ format : 'pem' ,
26+ } )
27+ this . publicKeyAPI = pem . decode ( exportPublicKey ) . toString ( 'hex' ) . substring ( 24 )
28+ this . publicKeyChecksum = toPublicKeyChecksum ( this . publicKeyAPI )
29+ }
30+
2231 /**
23- * Creates a new account
32+ * Creates a new keypair
2433 *
25- * @returns a new ConvexAccount Object
34+ * @returns a new KeyPair Object
2635 */
27- public static create ( ) : ConvexAccount {
36+ public static create ( ) : KeyPair {
2837 // create a temporary password for generating a random private/public keys
2938 const password = randomBytes ( 64 ) . toString ( 'hex' )
3039 const { publicKey, privateKey } = generateKeyPairSync ( 'ed25519' , {
@@ -39,29 +48,26 @@ export class ConvexAccount {
3948 passphrase : password ,
4049 } ,
4150 } )
42- return ConvexAccount . importFromString ( privateKey , password , null , null , publicKey )
51+ return KeyPair . importFromString ( privateKey , password , publicKey )
4352 }
4453
4554 /**
46- * Imports an account from a PKCS8 fromated text string. You need to pass the correct password, to decrypt
55+ * Imports a keypair from a PKCS8 fromated text string. You need to pass the correct password, to decrypt
4756 * the private key stored in the text string.
4857 *
4958 * @param text PKCS8 fromated text with the private key encrypted.
5059 * @param password Password to decrypt the private key.
51- * @param address Optional address used for this account.
5260 * @param publicKeyText Optional public key encoded in PEM format, if non provided, the public key
5361 * can be obtained from the private key.
5462 *
55- * @returns an account object with the private and public key pairs.
63+ * @returns an KeyPair object with the private and public key pairs.
5664 *
5765 */
5866 public static importFromString (
5967 text : string ,
6068 password : string ,
61- address ?: BigInt ,
62- name ?: string ,
6369 publicKeyText ?: string
64- ) : ConvexAccount {
70+ ) : KeyPair {
6571 const privateKey = createPrivateKey ( {
6672 key : text ,
6773 type : 'pkcs8' ,
@@ -75,60 +81,38 @@ export class ConvexAccount {
7581 publicKey = createPublicKey ( privateKey )
7682 }
7783
78- return new ConvexAccount ( publicKey , privateKey , address , name )
84+ return new KeyPair ( publicKey , privateKey )
7985 }
8086
8187 /**
8288 * Imports a private key file. The key file is in the format as PKCS8 text. The private key is encrypted.
8389 *
8490 * @param filename Filename containing the encrypted private key.
8591 * @param password Password to decrypt the private key.
86- * @param address Optional address used for this account.
8792 *
88- * @returns An ConvexAccount object with the private and public keys.
93+ * @returns An KeyPair object with the private and public keys.
8994 *
9095 */
9196 public static async importFromFile (
9297 filename : string ,
9398 password : string ,
94- address ?: BigInt ,
95- name ?: string
96- ) : Promise < ConvexAccount > {
99+ ) : Promise < KeyPair > {
97100 if ( fs . existsSync ( filename ) ) {
98101 const data = await fs . promises . readFile ( filename )
99- return ConvexAccount . importFromString ( data . toString ( ) , password , address , name )
102+ return KeyPair . importFromString ( data . toString ( ) , password )
100103 }
101104 return null
102105 }
103106
104- public static async importFromAccount ( account : ConvexAccount , address ?: BigInt , name ?: string ) : Promise < ConvexAccount > {
105- const password = randomBytes ( 64 ) . toString ( 'hex' )
106- const keyText = account . exportToText ( password )
107- return ConvexAccount . importFromString ( keyText , password , address , name )
108- }
109-
110- constructor ( publicKey : KeyObject , privateKey : KeyObject , address ?: BigInt , name ?: string ) {
111- this . publicKey = publicKey
112- this . privateKey = privateKey
113- const exportPublicKey = publicKey . export ( {
114- type : 'spki' ,
115- format : 'pem' ,
116- } )
117- this . publicKeyAPI = pem . decode ( exportPublicKey ) . toString ( 'hex' ) . substring ( 24 )
118- this . publicKeyChecksum = toPublicKeyChecksum ( this . publicKeyAPI )
119- this . address = address
120- this . name = name
121- }
122-
123107 /**
124- * Export the account to a PKCS8 fromatted text string. The private key is encrypted using the provided password.
108+ * Export the keypair to a PKCS8 fromatted text string. The private key is encrypted using the provided password.
125109 *
126110 * @param password Password to encrypt the private key.
127111 *
128112 * @returns The encrpted private key as a PKCS8 formatted string.
129113 *
130114 */
131- public exportToText ( password : string ) : string {
115+ public exportToString ( password : string ) : string {
132116 return this . privateKey
133117 . export ( {
134118 type : 'pkcs8' ,
@@ -146,12 +130,12 @@ export class ConvexAccount {
146130 *
147131 */
148132 public async exportToFile ( filename : string , password : string ) : Promise < unknown > {
149- return await fs . promises . writeFile ( filename , this . exportToText ( password ) )
133+ return await fs . promises . writeFile ( filename , this . exportToString ( password ) )
150134 }
151135
152136 /**
153137 * Sign a hash message. This is called by the convex API class to sign a hash returned from the `prepare` api.
154- * This signed message cryptographically proves that the account owner has access to the private key.
138+ * This signed message cryptographically proves that the keypair owner has access to the private key.
155139 *
156140 * The API calls this with a hex string, that is converted to bytes, and then sigend.
157141 * The resultant signed data is sent back as a hex string.
0 commit comments