@@ -2,6 +2,7 @@ import { Cardano, Serialization } from '@cardano-sdk/core';
22import { HexBlob } from '@cardano-sdk/util' ;
33import { HexBytes } from '@lace-sdk/util' ;
44
5+ import type { Ed25519KeyHashHex } from '@cardano-sdk/crypto' ;
56import type { GroupedAddress } from '@cardano-sdk/key-management' ;
67import type { CardanoKeyAgent } from '@lace-contract/cardano-context' ;
78import type {
@@ -18,11 +19,21 @@ const COSE_KEY_ALG = 3;
1819const COSE_KEY_CRV = - 1 ;
1920const COSE_KEY_X = - 2 ;
2021const STAKE_KEY_DERIVATION_PATH = { role : 2 , index : 0 } ;
22+ const DREP_KEY_DERIVATION_PATH = { role : 3 , index : 0 } ;
2123
22- const getDerivationPath = (
23- signWith : Cardano . PaymentAddress | Cardano . RewardAccount ,
24- knownAddresses : GroupedAddress [ ] ,
25- ) : { role : number ; index : number } => {
24+ interface GetDerivationPathParams {
25+ address : Cardano . Address ;
26+ signWith : Cardano . PaymentAddress | Cardano . RewardAccount ;
27+ knownAddresses : GroupedAddress [ ] ;
28+ dRepKeyHash : Ed25519KeyHashHex | undefined ;
29+ }
30+
31+ const getDerivationPath = ( {
32+ address,
33+ signWith,
34+ knownAddresses,
35+ dRepKeyHash,
36+ } : GetDerivationPathParams ) : { role : number ; index : number } => {
2637 if ( Cardano . isRewardAccount ( signWith ) ) {
2738 const matchingAddress = knownAddresses . find (
2839 addr => addr . rewardAccount === signWith ,
@@ -36,6 +47,14 @@ const getDerivationPath = (
3647 return STAKE_KEY_DERIVATION_PATH ;
3748 }
3849
50+ if (
51+ dRepKeyHash &&
52+ address . getType ( ) === Cardano . AddressType . EnterpriseKey &&
53+ address . getProps ( ) . paymentPart ?. hash === dRepKeyHash
54+ ) {
55+ return DREP_KEY_DERIVATION_PATH ;
56+ }
57+
3958 const matchingAddress = knownAddresses . find (
4059 addr => addr . address === signWith ,
4160 ) ;
@@ -46,7 +65,7 @@ const getDerivationPath = (
4665 } ;
4766 }
4867
49- return { role : 0 , index : 0 } ;
68+ throw new Error ( `Unknown signWith address: ${ signWith } ` ) ;
5069} ;
5170
5271const createProtectedHeaders = ( addressBytes : Uint8Array ) : Uint8Array => {
@@ -109,19 +128,32 @@ const createCoseKey = (
109128 return writer . encode ( ) ;
110129} ;
111130
131+ export interface Cip8SignDataParams {
132+ keyAgent : CardanoKeyAgent ;
133+ request : CardanoSignDataRequest ;
134+ knownAddresses : GroupedAddress [ ] ;
135+ dRepKeyHash ?: Ed25519KeyHashHex ;
136+ }
137+
112138/** Signs data per CIP-8 by constructing COSE structures without WASM. */
113- export const cip8SignData = async (
114- keyAgent : CardanoKeyAgent ,
115- request : CardanoSignDataRequest ,
116- knownAddresses : GroupedAddress [ ] ,
117- ) : Promise < CardanoSignDataResult > => {
139+ export const cip8SignData = async ( {
140+ keyAgent,
141+ request,
142+ knownAddresses,
143+ dRepKeyHash,
144+ } : Cip8SignDataParams ) : Promise < CardanoSignDataResult > => {
118145 const address = Cardano . Address . fromString ( request . signWith ) ;
119146 if ( ! address ) {
120147 throw new Error ( `Invalid address: ${ request . signWith } ` ) ;
121148 }
122149
123150 const addressBytes = Buffer . from ( address . toBytes ( ) , 'hex' ) ;
124- const derivationPath = getDerivationPath ( request . signWith , knownAddresses ) ;
151+ const derivationPath = getDerivationPath ( {
152+ address,
153+ signWith : request . signWith ,
154+ knownAddresses,
155+ dRepKeyHash,
156+ } ) ;
125157
126158 const protectedHeadersBytes = createProtectedHeaders ( addressBytes ) ;
127159 const payloadBytes = Buffer . from ( request . payload , 'hex' ) ;
0 commit comments