@@ -86,23 +86,25 @@ export const FromBech32 = Schema.transformOrFail(Schema.String, Schema.typeSchem
8686 E . gen ( function * ( ) {
8787 const privateKeyBytes = yield * ParseResult . encodeEither ( FromBytes ) ( toA )
8888 const words = bech32 . toWords ( privateKeyBytes )
89- return bech32 . encode ( "ed25519e_sk" , words , 1023 )
89+ // Auto-select prefix based on key length (32 bytes = normal, 64 bytes = extended)
90+ const prefix = privateKeyBytes . length === 32 ? "ed25519_sk" : "ed25519e_sk"
91+ return bech32 . encode ( prefix , words , 1023 )
9092 } ) ,
9193 decode : ( fromA , _ , ast ) =>
9294 E . gen ( function * ( ) {
9395 const { prefix, words } = yield * ParseResult . try ( {
9496 try : ( ) => bech32 . decode ( fromA as any , 1023 ) ,
9597 catch : ( error ) => new ParseResult . Type ( ast , fromA , `Failed to decode bech32 string: ${ ( error as Error ) . message } ` )
9698 } )
97- if ( prefix !== "ed25519e_sk" ) {
98- throw new ParseResult . Type ( ast , fromA , `Expected ed25519e_sk prefix, got ${ prefix } ` )
99+ if ( prefix !== "ed25519e_sk" && prefix !== "ed25519_sk" ) {
100+ throw new ParseResult . Type ( ast , fromA , `Expected ed25519e_sk or ed25519_sk prefix, got ${ prefix } ` )
99101 }
100102 const decoded = bech32 . fromWords ( words )
101103 return yield * ParseResult . decodeEither ( FromBytes ) ( decoded )
102104 } )
103105} ) . annotations ( {
104106 identifier : "PrivateKey.FromBech32" ,
105- description : "Transforms Bech32 string (ed25519e_sk1...) to PrivateKey"
107+ description : "Transforms Bech32 string (ed25519e_sk1... or ed25519_sk1... ) to PrivateKey"
106108} )
107109
108110/**
@@ -140,7 +142,8 @@ export const fromHex = Schema.decodeSync(FromHex)
140142
141143/**
142144 * Parse a PrivateKey from a Bech32 string.
143- * Expected format: ed25519e_sk1...
145+ * Supports both extended (ed25519e_sk1...) and normal (ed25519_sk1...) formats.
146+ * Compatible with CML.PrivateKey.from_bech32().
144147 *
145148 * @since 2.0.0
146149 * @category parsing
@@ -169,7 +172,10 @@ export const toHex = Schema.encodeSync(FromHex)
169172
170173/**
171174 * Convert a PrivateKey to a Bech32 string.
172- * Format: ed25519e_sk1...
175+ * Automatically selects the appropriate prefix based on key length:
176+ * - 32 bytes → ed25519_sk1... (normal key)
177+ * - 64 bytes → ed25519e_sk1... (extended key)
178+ * Compatible with CML.PrivateKey.to_bech32().
173179 *
174180 * @since 2.0.0
175181 * @category encoding
0 commit comments