Skip to content

Commit 07f4844

Browse files
committed
feat: add sign data module
1 parent 8c428ac commit 07f4844

File tree

8 files changed

+2250
-6
lines changed

8 files changed

+2250
-6
lines changed

packages/evolution/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
},
4141
"devDependencies": {
4242
"@dcspark/cardano-multiplatform-lib-nodejs": "^6.2.0",
43+
"@emurgo/cardano-message-signing-nodejs": "^1.1.0",
4344
"@types/dockerode": "^3.3.43",
4445
"tinybench": "^5.0.0",
4546
"tsx": "^4.20.4",

packages/evolution/src/core/PrivateKey.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)