(keyResponse: GetKeyResponse | GetTlsKeyResponse)
| 26 | * This method applies SHA256 hashing to the complete key material for enhanced security. |
| 27 | */ |
| 28 | export function toViemAccountSecure(keyResponse: GetKeyResponse | GetTlsKeyResponse) { |
| 29 | // Keep legacy behavior for GetTlsKeyResponse, but with warning. |
| 30 | if (keyResponse.__name__ === 'GetTlsKeyResponse') { |
| 31 | console.warn('toViemAccountSecure: Please don\'t use `deriveKey` method to get key, use `getKey` instead.') |
| 32 | try { |
| 33 | // Get supported hash algorithm by `openssl list -digest-algorithms`, but it's not guaranteed to be supported by node.js |
| 34 | const hex = crypto.createHash('sha256').update(keyResponse.asUint8Array()).digest('hex') |
| 35 | return privateKeyToAccount(`0x${hex}`) |
| 36 | } catch (err) { |
| 37 | throw new Error('toViemAccountSecure: missing sha256 support, please upgrade your openssl and node.js') |
| 38 | } |
| 39 | } |
| 40 | const hex = Array.from(keyResponse.key).map(b => b.toString(16).padStart(2, '0')).join('') |
| 41 | return privateKeyToAccount(`0x${hex}`) |
| 42 | } |
no test coverage detected