Skip to content

Commit 77764d3

Browse files
committed
chore: wip
1 parent 35b9644 commit 77764d3

3 files changed

Lines changed: 88 additions & 87 deletions

File tree

src/algorithms/asymmetric/ed25519.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ interface Ed25519Options {
5858
encoding?: 'binary' | 'utf8'
5959
}
6060

61-
interface Ed25519 {
61+
export interface Ed25519 {
6262
constants: Ed25519Constants
6363
generateKeyPair: (options?: Ed25519Options) => Ed25519KeyPair
6464
privateKeyFromAsn1: (obj: any) => { privateKeyBytes: BufferSource }
@@ -72,17 +72,15 @@ interface ExtendedError extends Error {
7272
errors?: any[]
7373
}
7474

75-
const ed25519 = {} as Ed25519
76-
77-
ed25519.constants = {
75+
const constants: Ed25519Constants = {
7876
PUBLIC_KEY_BYTE_LENGTH: 32,
7977
PRIVATE_KEY_BYTE_LENGTH: 64,
8078
SEED_BYTE_LENGTH: 32,
8179
SIGN_BYTE_LENGTH: 64,
8280
HASH_BYTE_LENGTH: 64,
83-
}
81+
} as const
8482

85-
ed25519.generateKeyPair = function (options) {
83+
export function generateKeyPair(options: Ed25519Options) {
8684
options = options || {}
8785
let seed = options.seed
8886
if (seed === undefined) {
@@ -122,7 +120,7 @@ ed25519.generateKeyPair = function (options) {
122120
* @returns {object} keyInfo - The key information.
123121
* @returns {Buffer|Uint8Array} keyInfo.privateKeyBytes - 32 private key bytes.
124122
*/
125-
ed25519.privateKeyFromAsn1 = function (obj: any) {
123+
export function privateKeyFromAsn1(obj: any) {
126124
const capture = {} as { privateKeyOid?: string, privateKey?: ByteStringBuffer }
127125
const errors: any[] = []
128126
const valid = asn1.validate(obj, privateKeyValidator, capture, errors)
@@ -152,7 +150,7 @@ ed25519.privateKeyFromAsn1 = function (obj: any) {
152150
*
153151
* @return {Buffer|Uint8Array} - 32 public key bytes.
154152
*/
155-
ed25519.publicKeyFromAsn1 = function (obj: any): Buffer | Uint8Array {
153+
export function publicKeyFromAsn1(obj: any): Buffer | Uint8Array {
156154
const capture = {} as { publicKeyOid?: string, ed25519PublicKey?: Uint8Array }
157155
const errors: any[] = []
158156
const valid = asn1.validate(obj, publicKeyValidator, capture, errors)
@@ -177,7 +175,7 @@ ed25519.publicKeyFromAsn1 = function (obj: any): Buffer | Uint8Array {
177175
})
178176
}
179177

180-
ed25519.publicKeyFromPrivateKey = function (options) {
178+
export function publicKeyFromPrivateKey(options: Ed25519Options): Buffer | Uint8Array {
181179
options = options || {}
182180
const privateKey = messageToNativeBuffer({
183181
message: options.privateKey,
@@ -197,7 +195,7 @@ ed25519.publicKeyFromPrivateKey = function (options) {
197195
return pk
198196
}
199197

200-
ed25519.sign = function (options) {
198+
export function sign(options: Ed25519Options): Buffer | Uint8Array {
201199
options = options || {}
202200
const msg = messageToNativeBuffer(options)
203201
let privateKey = messageToNativeBuffer({
@@ -233,12 +231,8 @@ export function verify(options: Ed25519Options): boolean {
233231

234232
const msg = messageToNativeBuffer(options)
235233

236-
if (options.signature === undefined) {
237-
throw new TypeError(
238-
'"options.signature" must be a node.js Buffer, a Uint8Array, a forge '
239-
+ 'ByteBuffer, or a binary string.',
240-
)
241-
}
234+
if (options.signature === undefined)
235+
throw new TypeError('"options.signature" must be a Node.js/Bun Buffer, a Uint8Array, a ByteBuffer, or a binary string.',)
242236

243237
const sig = messageToNativeBuffer({
244238
message: options.signature,
@@ -1345,7 +1339,7 @@ function scalarbase(p: GFArray, s: number[]): void {
13451339

13461340
// Add type conversion helpers
13471341
function arrayToBuffer(arr: number[]): Buffer | Uint8Array {
1348-
return new NativeBuffer(arr)
1342+
return typeof Buffer !== 'undefined' ? Buffer.from(arr) : Uint8Array.from(arr)
13491343
}
13501344

13511345
function bufferToGF(buffer: BufferSource): GF {
@@ -1359,3 +1353,15 @@ function gfToBuffer(g: GF): Buffer | Uint8Array {
13591353

13601354
return arrayToBuffer(arr)
13611355
}
1356+
1357+
export const ed25519: Ed25519 = {
1358+
constants,
1359+
generateKeyPair,
1360+
privateKeyFromAsn1,
1361+
publicKeyFromAsn1,
1362+
publicKeyFromPrivateKey,
1363+
sign,
1364+
verify,
1365+
}
1366+
1367+
export default ed25519

src/utils/index.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,25 @@ export class ByteStringBuffer {
1414
public read: number
1515
private _constructedStringLength: number
1616

17-
constructor(b?: string | ArrayBuffer | ArrayBufferView) {
17+
constructor(b?: string | ArrayBuffer | ArrayBufferView, encoding?: 'utf8' | 'binary') {
1818
// Initialize properties
1919
this.data = ''
2020
this.read = 0
2121
this._constructedStringLength = 0
2222

2323
if (b !== undefined) {
2424
if (typeof b === 'string') {
25-
this.data = b
25+
if (encoding === 'utf8') {
26+
const encoder = new TextEncoder()
27+
const uint8Array = encoder.encode(b)
28+
this.data = String.fromCharCode(...uint8Array)
29+
} else if (encoding === 'binary') {
30+
this.data = b
31+
}
2632
}
2733
else if (isArrayBuffer(b) || isArrayBufferView(b)) {
28-
if (typeof Buffer !== 'undefined' && b instanceof Buffer) {
29-
this.data = b.toString('binary')
30-
}
31-
else {
32-
// convert native buffer to forge buffer
33-
const arr = new Uint8Array(b as ArrayBuffer)
34-
try {
35-
this.data = String.fromCharCode(...Array.from(arr))
36-
}
37-
catch (e) {
38-
for (let i = 0; i < arr.length; ++i) {
39-
this.putByte(arr[i])
40-
}
41-
}
42-
}
34+
const view = new Uint8Array(b instanceof ArrayBuffer ? b : b.buffer)
35+
this.data = String.fromCharCode(...view)
4336
}
4437
else if (b instanceof ByteStringBuffer) {
4538
// copy existing buffer
@@ -54,6 +47,7 @@ export class ByteStringBuffer {
5447
this.read = b.read
5548
}
5649
}
50+
this._constructedStringLength = this.data.length
5751
}
5852

5953
/**

0 commit comments

Comments
 (0)