@@ -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
13471341function arrayToBuffer ( arr : number [ ] ) : Buffer | Uint8Array {
1348- return new NativeBuffer ( arr )
1342+ return typeof Buffer !== 'undefined' ? Buffer . from ( arr ) : Uint8Array . from ( arr )
13491343}
13501344
13511345function 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
0 commit comments