Skip to content

Commit d5ff30e

Browse files
committed
reorganize code
1 parent e85d8c9 commit d5ff30e

File tree

4 files changed

+62
-59
lines changed

4 files changed

+62
-59
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ The following contracts are available in each of the packages:
5151
# COTI v2 Typescript SDK
5252

5353
> [!NOTE]
54-
> Please refer to the latest [tags](https://github.com/coti-io/coti-sdk-typescript/tags) to find the most stable version to use.
54+
> Please refer to the latest [tags](https://github.com/coti-io/coti-sdk-typescript/tags) to find the most stable version
55+
> to use.
5556
> All tagged versions are available to install via [npmjs](https://www.npmjs.com/package/@coti-io/coti-sdk-typescript)
5657
5758
The COTI Typescript SDK is composed of two main components:

src/account/onboard-contract.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
export const ONBOARD_CONTRACT_ADDRESS = "0xadb3952a07B2c9fA7dbd2F583cd2F0412C97eC33"
22
export const ONBOARD_CONTRACT_ABI = [
3-
{
4-
"anonymous": false,
5-
"inputs": [
6-
{
7-
"indexed": true,
8-
"internalType": "address",
9-
"name": "_from",
10-
"type": "address"
11-
},
12-
{
13-
"indexed": false,
14-
"internalType": "bytes",
15-
"name": "userKey",
16-
"type": "bytes"
17-
}
18-
],
19-
"name": "AccountOnboarded",
20-
"type": "event"
21-
},
22-
{
23-
"inputs": [
24-
{
25-
"internalType": "bytes",
26-
"name": "signedEK",
27-
"type": "bytes"
28-
},
29-
{
30-
"internalType": "bytes",
31-
"name": "signature",
32-
"type": "bytes"
33-
}
34-
],
35-
"name": "OnboardAccount",
36-
"outputs": [],
37-
"stateMutability": "nonpayable",
38-
"type": "function"
39-
}
3+
{
4+
"anonymous": false,
5+
"inputs": [
6+
{
7+
"indexed": true,
8+
"internalType": "address",
9+
"name": "_from",
10+
"type": "address"
11+
},
12+
{
13+
"indexed": false,
14+
"internalType": "bytes",
15+
"name": "userKey",
16+
"type": "bytes"
17+
}
18+
],
19+
"name": "AccountOnboarded",
20+
"type": "event"
21+
},
22+
{
23+
"inputs": [
24+
{
25+
"internalType": "bytes",
26+
"name": "signedEK",
27+
"type": "bytes"
28+
},
29+
{
30+
"internalType": "bytes",
31+
"name": "signature",
32+
"type": "bytes"
33+
}
34+
],
35+
"name": "OnboardAccount",
36+
"outputs": [],
37+
"stateMutability": "nonpayable",
38+
"type": "function"
39+
}
4040
]

src/crypto_utils.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function decrypt(key: Uint8Array, r: Uint8Array, ciphertext: Uint8Array):
5858

5959
export function generateRSAKeyPair(): { publicKey: Uint8Array; privateKey: Uint8Array } {
6060
// Generate a new RSA key pair
61-
const rsaKeyPair = forge.pki.rsa.generateKeyPair({ bits: 2048 })
61+
const rsaKeyPair = forge.pki.rsa.generateKeyPair({bits: 2048})
6262

6363
// Convert keys to DER format
6464
const privateKey = forge.asn1.toDer(forge.pki.privateKeyToAsn1(rsaKeyPair.privateKey)).data
@@ -76,7 +76,7 @@ export function decryptRSA(privateKey: Uint8Array, ciphertext: string): string {
7676

7777
// Decrypt using RSA-OAEP
7878
const rsaPrivateKey = forge.pki.privateKeyFromPem(privateKeyPEM);
79-
79+
8080
const decrypted = rsaPrivateKey.decrypt(forge.util.hexToBytes(ciphertext), 'RSA-OAEP', {
8181
md: forge.md.sha256.create()
8282
});
@@ -133,15 +133,15 @@ export function buildInputText(
133133
const keyBytes = encodeKey(sender.userKey)
134134

135135
// Encrypt the plaintext using AES key
136-
const { ciphertext, r } = encrypt(keyBytes, plaintextBytes)
136+
const {ciphertext, r} = encrypt(keyBytes, plaintextBytes)
137137
const ct = new Uint8Array([...ciphertext, ...r])
138138

139139
// Convert the ciphertext to BigInt
140140
const ctInt = decodeUint(ct)
141141

142142
const signature = signInputText(sender, contractAddress, functionSelector, ctInt);
143143

144-
return { ctInt, signature }
144+
return {ctInt, signature}
145145
}
146146

147147
export async function buildStringInputText(
@@ -157,8 +157,8 @@ export async function buildStringInputText(
157157
let encryptedStr = new Array<{ ciphertext: bigint, signature: Uint8Array }>(plaintext.length)
158158

159159
for (let i = 0; i < plaintext.length; i++) {
160-
const { ctInt, signature } = buildInputText(BigInt(encodedStr[i]), sender, contractAddress, functionSelector)
161-
encryptedStr[i] = { ciphertext: ctInt, signature }
160+
const {ctInt, signature} = buildInputText(BigInt(encodedStr[i]), sender, contractAddress, functionSelector)
161+
encryptedStr[i] = {ciphertext: ctInt, signature}
162162
}
163163

164164
return encryptedStr
@@ -227,38 +227,38 @@ export function encodeString(str: string): Uint8Array {
227227

228228
export function encodeKey(userKey: string): Uint8Array {
229229
const keyBytes = new Uint8Array(16)
230-
230+
231231
for (let i = 0; i < 32; i += 2) {
232-
keyBytes[i / 2] = parseInt(userKey.slice(i, i+2), HEX_BASE)
232+
keyBytes[i / 2] = parseInt(userKey.slice(i, i + 2), HEX_BASE)
233233
}
234-
234+
235235
return keyBytes
236236
}
237-
237+
238238
export function encodeUint(plaintext: bigint): Uint8Array {
239239
// Convert the plaintext to bytes in little-endian format
240240

241241
const plaintextBytes = new Uint8Array(BLOCK_SIZE) // Allocate a buffer of size 16 bytes
242-
242+
243243
for (let i = 15; i >= 0; i--) {
244-
plaintextBytes[i] = Number(plaintext & BigInt(255))
245-
plaintext >>= BigInt(8)
244+
plaintextBytes[i] = Number(plaintext & BigInt(255))
245+
plaintext >>= BigInt(8)
246246
}
247-
247+
248248
return plaintextBytes
249249
}
250250

251251
export function decodeUint(plaintextBytes: Uint8Array): bigint {
252252
const plaintext: Array<string> = []
253-
253+
254254
let byte = ''
255-
255+
256256
for (let i = 0; i < plaintextBytes.length; i++) {
257-
byte = plaintextBytes[i].toString(HEX_BASE).padStart(2, '0') // ensure that the zero byte is represented using two digits
258-
259-
plaintext.push(byte)
257+
byte = plaintextBytes[i].toString(HEX_BASE).padStart(2, '0') // ensure that the zero byte is represented using two digits
258+
259+
plaintext.push(byte)
260260
}
261-
261+
262262
return BigInt("0x" + plaintext.join(""))
263263
}
264264

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@
1010
"declaration": true,
1111
"outDir": "./dist"
1212
},
13-
"include": ["src/**/*"]
13+
"include": [
14+
"src/**/*"
15+
]
1416
}

0 commit comments

Comments
 (0)