@@ -4,7 +4,7 @@ import { Buffer } from 'node:buffer';
4
4
import type { TransportDef , TransportType } from 'https://deno.land/x/polkadot/hw-ledger-transports/types.ts' ;
5
5
import type { AccountOptionsGeneric , LedgerAddress , LedgerSignature , LedgerVersion } from './types.ts' ;
6
6
7
- import { PolkadotGenericApp } from 'https://esm.sh/@zondax/ledger-substrate@1.0.0 ' ;
7
+ import { PolkadotGenericApp } from 'https://esm.sh/@zondax/ledger-substrate@1.1.1 ' ;
8
8
9
9
import { transports } from 'https://deno.land/x/polkadot/hw-ledger-transports/mod.ts' ;
10
10
import { hexAddPrefix , u8aToBuffer , u8aWrapBytes } from 'https://deno.land/x/polkadot/util/mod.ts' ;
@@ -42,8 +42,8 @@ async function wrapError <T extends WrappedResult> (promise: Promise<T>): Promis
42
42
return result ;
43
43
}
44
44
45
- /** @internal Wraps a sign/signRaw call and returns the associated signature */
46
- function sign ( method : 'sign ' | 'signRaw ' , message : Uint8Array , slip44 : number , accountIndex = 0 , addressOffset = 0 ) : ( app : PolkadotGenericApp ) => Promise < LedgerSignature > {
45
+ /** @internal Wraps a signEd25519/signRawEd25519 call and returns the associated signature */
46
+ function sign ( method : 'signEd25519 ' | 'signRawEd25519 ' , message : Uint8Array , slip44 : number , accountIndex = 0 , addressOffset = 0 ) : ( app : PolkadotGenericApp ) => Promise < LedgerSignature > {
47
47
const bip42Path = `m/44'/${ slip44 } '/${ accountIndex } '/${ 0 } '/${ addressOffset } '` ;
48
48
49
49
return async ( app : PolkadotGenericApp ) : Promise < LedgerSignature > => {
@@ -55,7 +55,22 @@ function sign (method: 'sign' | 'signRaw', message: Uint8Array, slip44: number,
55
55
} ;
56
56
}
57
57
58
- /** @internal Wraps a signWithMetadata call and returns the associated signature */
58
+ /** @internal Wraps a signEcdsa/signRawEcdsa call and returns the associated signature */
59
+ function signEcdsa ( method : 'signEcdsa' | 'signRawEcdsa' , message : Uint8Array , slip44 : number , accountIndex = 0 , addressOffset = 0 ) : ( app : PolkadotGenericApp ) => Promise < LedgerSignature > {
60
+ const bip42Path = `m/44'/${ slip44 } '/${ accountIndex } '/${ 0 } '/${ addressOffset } '` ;
61
+
62
+ return async ( app : PolkadotGenericApp ) : Promise < LedgerSignature > => {
63
+ const { r, s, v } = await wrapError ( app [ method ] ( bip42Path , u8aToBuffer ( message ) ) ) ;
64
+
65
+ const signature = Buffer . concat ( [ r , s , v ] ) ;
66
+
67
+ return {
68
+ signature : hexAddPrefix ( signature . toString ( 'hex' ) )
69
+ } ;
70
+ } ;
71
+ }
72
+
73
+ /** @internal Wraps a signWithMetadataEd25519 call and returns the associated signature */
59
74
function signWithMetadata ( message : Uint8Array , slip44 : number , accountIndex = 0 , addressOffset = 0 , { metadata } : Partial < AccountOptionsGeneric > = { } ) : ( app : PolkadotGenericApp ) => Promise < LedgerSignature > {
60
75
const bip42Path = `m/44'/${ slip44 } '/${ accountIndex } '/${ 0 } '/${ addressOffset } '` ;
61
76
@@ -66,7 +81,28 @@ function signWithMetadata (message: Uint8Array, slip44: number, accountIndex = 0
66
81
67
82
const bufferMsg = Buffer . from ( message ) ;
68
83
69
- const { signature } = await wrapError ( app . signWithMetadata ( bip42Path , bufferMsg , metadata ) ) ;
84
+ const { signature } = await wrapError ( app . signWithMetadataEd25519 ( bip42Path , bufferMsg , metadata ) ) ;
85
+
86
+ return {
87
+ signature : hexAddPrefix ( signature . toString ( 'hex' ) )
88
+ } ;
89
+ } ;
90
+ }
91
+
92
+ /** @internal Wraps a signWithMetadataEcdsa call and returns the associated signature */
93
+ function signWithMetadataEcdsa ( message : Uint8Array , slip44 : number , accountIndex = 0 , addressOffset = 0 , { metadata } : Partial < AccountOptionsGeneric > = { } ) : ( app : PolkadotGenericApp ) => Promise < LedgerSignature > {
94
+ const bip42Path = `m/44'/${ slip44 } '/${ accountIndex } '/${ 0 } '/${ addressOffset } '` ;
95
+
96
+ return async ( app : PolkadotGenericApp ) : Promise < LedgerSignature > => {
97
+ if ( ! metadata ) {
98
+ throw new Error ( 'The metadata option must be present when using signWithMetadata' ) ;
99
+ }
100
+
101
+ const bufferMsg = Buffer . from ( message ) ;
102
+
103
+ const { r, s, v } = await wrapError ( app . signWithMetadataEcdsa ( bip42Path , bufferMsg , metadata ) ) ;
104
+
105
+ const signature = Buffer . concat ( [ r , s , v ] ) ;
70
106
71
107
return {
72
108
signature : hexAddPrefix ( signature . toString ( 'hex' ) )
@@ -115,14 +151,31 @@ export class LedgerGeneric {
115
151
}
116
152
117
153
/**
118
- * @description Returns the address associated with a specific account & address offset. Optionally
154
+ * @description Returns the address associated with a specific Ed25519 account & address offset. Optionally
119
155
* asks for on-device confirmation
120
156
*/
121
157
public async getAddress ( ss58Prefix : number , confirm = false , accountIndex = 0 , addressOffset = 0 ) : Promise < LedgerAddress > {
122
158
const bip42Path = `m/44'/${ this . #slip44} '/${ accountIndex } '/${ 0 } '/${ addressOffset } '` ;
123
159
124
160
return this . withApp ( async ( app : PolkadotGenericApp ) : Promise < LedgerAddress > => {
125
- const { address, pubKey } = await wrapError ( app . getAddress ( bip42Path , ss58Prefix , confirm ) ) ;
161
+ const { address, pubKey } = await wrapError ( app . getAddressEd25519 ( bip42Path , ss58Prefix , confirm ) ) ;
162
+
163
+ return {
164
+ address,
165
+ publicKey : hexAddPrefix ( pubKey )
166
+ } ;
167
+ } ) ;
168
+ }
169
+
170
+ /**
171
+ * @description Returns the address associated with a specific ecdsa account & address offset. Optionally
172
+ * asks for on-device confirmation
173
+ */
174
+ public async getAddressEcdsa ( confirm = false , accountIndex = 0 , addressOffset = 0 ) {
175
+ const bip42Path = `m/44'/${ this . #slip44} '/${ accountIndex } '/${ 0 } '/${ addressOffset } '` ;
176
+
177
+ return this . withApp ( async ( app : PolkadotGenericApp ) : Promise < LedgerAddress > => {
178
+ const { address, pubKey } = await wrapError ( app . getAddressEcdsa ( bip42Path , confirm ) ) ;
126
179
127
180
return {
128
181
address,
@@ -150,14 +203,28 @@ export class LedgerGeneric {
150
203
* @description Signs a transaction on the Ledger device. This requires the LedgerGeneric class to be instantiated with `chainId`, and `metaUrl`
151
204
*/
152
205
public async sign ( message : Uint8Array , accountIndex ?: number , addressOffset ?: number ) : Promise < LedgerSignature > {
153
- return this . withApp ( sign ( 'sign ' , message , this . #slip44, accountIndex , addressOffset ) ) ;
206
+ return this . withApp ( sign ( 'signEd25519 ' , message , this . #slip44, accountIndex , addressOffset ) ) ;
154
207
}
155
208
156
209
/**
157
210
* @description Signs a message (non-transactional) on the Ledger device
158
211
*/
159
212
public async signRaw ( message : Uint8Array , accountIndex ?: number , addressOffset ?: number ) : Promise < LedgerSignature > {
160
- return this . withApp ( sign ( 'signRaw' , u8aWrapBytes ( message ) , this . #slip44, accountIndex , addressOffset ) ) ;
213
+ return this . withApp ( sign ( 'signRawEd25519' , u8aWrapBytes ( message ) , this . #slip44, accountIndex , addressOffset ) ) ;
214
+ }
215
+
216
+ /**
217
+ * @description Signs a transaction on the Ledger device with Ecdsa. This requires the LedgerGeneric class to be instantiated with `chainId`, and `metaUrl`
218
+ */
219
+ public async signEcdsa ( message : Uint8Array , accountIndex ?: number , addressOffset ?: number ) : Promise < LedgerSignature > {
220
+ return this . withApp ( signEcdsa ( 'signEcdsa' , u8aWrapBytes ( message ) , this . #slip44, accountIndex , addressOffset ) ) ;
221
+ }
222
+
223
+ /**
224
+ * @description Signs a message with Ecdsa (non-transactional) on the Ledger device
225
+ */
226
+ public async signRawEcdsa ( message : Uint8Array , accountIndex ?: number , addressOffset ?: number ) : Promise < LedgerSignature > {
227
+ return this . withApp ( signEcdsa ( 'signRawEcdsa' , u8aWrapBytes ( message ) , this . #slip44, accountIndex , addressOffset ) ) ;
161
228
}
162
229
163
230
/**
@@ -167,6 +234,13 @@ export class LedgerGeneric {
167
234
return this . withApp ( signWithMetadata ( message , this . #slip44, accountIndex , addressOffset , options ) ) ;
168
235
}
169
236
237
+ /**
238
+ * @description Signs a transaction on the ledger device for an ecdsa signature provided some metadata.
239
+ */
240
+ public async signWithMetadataEcdsa ( message : Uint8Array , accountIndex ?: number , addressOffset ?: number , options ?: Partial < AccountOptionsGeneric > ) {
241
+ return this . withApp ( signWithMetadataEcdsa ( message , this . #slip44, accountIndex , addressOffset , options ) ) ;
242
+ }
243
+
170
244
/**
171
245
* @internal
172
246
*
0 commit comments