Skip to content

Commit ccf9a57

Browse files
committed
rename classes and cleanup
1 parent bfb57d6 commit ccf9a57

File tree

11 files changed

+287
-255
lines changed

11 files changed

+287
-255
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Beta Testing
44

5+
### Release v0.1.4
6+
+ Rename ConvexAccount to Account
7+
+ Rename ConvexAPI to API
8+
+ Split ConvexAccount to KeyPair
9+
510
### Relase 0.1.3
611
+ Add address '#' identifier before each address number.
712

src/ConvexAPI.ts renamed to src/API.ts

Lines changed: 68 additions & 71 deletions
Large diffs are not rendered by default.

src/Account.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
3+
Account.ts class to manage address and name.
4+
5+
*/
6+
7+
import { KeyPair } from 'KeyPair'
8+
9+
export class Account {
10+
readonly keyPair: KeyPair // KeyPair object
11+
readonly address: BigInt // address for this account
12+
readonly name: string // name of the registered account
13+
14+
constructor(keyPair: KeyPair, address: BigInt, name: string) {
15+
this.keyPair = keyPair
16+
this.address = address
17+
this.name = name
18+
}
19+
20+
/**
21+
* Creates a new account
22+
*
23+
* @returns a new Account Object
24+
*/
25+
public static create(keyPair: KeyPair, address?: BigInt, name?: string): Account {
26+
return new Account(keyPair, address, name);
27+
}
28+
29+
/**
30+
* Sign text, and return the signature
31+
*
32+
* @param text Text to sign
33+
*
34+
* @returns a signature of the signed text
35+
*
36+
*/
37+
public sign(text: string): string {
38+
return this.keyPair.sign(text);
39+
}
40+
}

src/Errors.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
33
44
5-
ConvexAPI Errors
5+
Convex Errors
66
77
*/
88

9-
class ConvexAPIBaseError extends Error {
9+
class APIBaseError extends Error {
1010
readonly source: string
1111
readonly code: string
1212
readonly text: string
@@ -20,13 +20,13 @@ class ConvexAPIBaseError extends Error {
2020
}
2121
}
2222

23-
export class ConvexAPIError extends ConvexAPIBaseError {
23+
export class APIError extends APIBaseError {
2424
constructor(source: string, code: string, text: string) {
2525
super(source, code, text, 'ConvexAPIError')
2626
}
2727
}
2828

29-
export class ConvexAPIRequestError extends ConvexAPIBaseError {
29+
export class APIRequestError extends APIBaseError {
3030
constructor(source: string, code: string, text: string) {
3131
super(source, code, text, 'ConvexAPIRequestError')
3232
}

src/Interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
*/
55

6-
export interface IConvexAccountInformation {
6+
export interface IAccountInformation {
77
address: BigInt
88
isLibrary: boolean
99
isActor: boolean
Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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

1212
import { 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.

src/Registry.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@
66
77
*/
88

9-
import { ConvexAccount } from './ConvexAccount'
10-
import { ConvexAPI } from './ConvexAPI'
9+
import { Account } from './Account'
10+
import { API } from './API'
1111
import { toAddress } from './Utils'
1212
import { IRegistryItem } from './Interfaces'
1313

1414
const QUERY_ACCOUNT_ADDRESS = BigInt(9)
1515

1616
export class Registry {
1717
/**
18-
* Convex API object to call registry commands
18+
* Registry object to call registry commands
1919
*/
20-
readonly convex: ConvexAPI
20+
readonly convex: API
2121
address: BigInt
2222
protected items: { [name: string]: IRegistryItem } = {}
2323

2424
/**
2525
* Initaliizes a new Registry object, you need to provide a ConvexAPI Object.
2626
*
27-
* @param convex ConvexAPI object to access the convex network.
27+
* @param convex API object to access the convex network.
2828
*
2929
*/
30-
public constructor(convex: ConvexAPI) {
30+
public constructor(convex: API) {
3131
this.convex = convex
3232
}
3333

@@ -85,7 +85,7 @@ export class Registry {
8585
* @returns an IRegistryItem object with the new address and owner address of the registered item.
8686
*
8787
*/
88-
public async register(name: string, registerAddress: BigInt, account: ConvexAccount): Promise<IRegistryItem> {
88+
public async register(name: string, registerAddress: BigInt, account: Account): Promise<IRegistryItem> {
8989
const address = await this.getAddress()
9090
const registerLine = `(call #${address} (register {:name "${name}"}))`
9191
const registerResult = await this.convex.send(registerLine, account)

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
*
77
*/
88

9-
export { ConvexAccount } from './ConvexAccount'
10-
export { ConvexAPI } from './ConvexAPI'
11-
export { ConvexAPIRequestError, ConvexAPIError } from './Errors'
12-
export { IConvexAccountInformation } from './Interfaces'
9+
export { Account } from './Account'
10+
export { API } from './API'
11+
export { APIRequestError, APIError } from './Errors'
12+
export { IAccountInformation } from './Interfaces'
13+
export { KeyPair } from './KeyPair'

0 commit comments

Comments
 (0)