Skip to content

Commit

Permalink
fixup! fixup! Support signing non-hex messages Required for signing B…
Browse files Browse the repository at this point in the history
…ity orders
  • Loading branch information
Jon-edge committed Jul 29, 2024
1 parent c0afe35 commit acba8e5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
47 changes: 18 additions & 29 deletions src/ethereum/EthereumEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
EdgeCurrencyInfo,
EdgeFetchFunction,
EdgeFreshAddress,
EdgeSignBytesOptions,
EdgeSignMessageOptions,
EdgeSpendInfo,
EdgeSpendTarget,
Expand Down Expand Up @@ -144,27 +143,6 @@ export class EthereumEngine extends CurrencyEngine<
]

this.utils = {
signBytes: (bytes: Uint8Array, privateKeys: EthereumPrivateKeys) => {
const privKey = Buffer.from(privateKeys.privateKey, 'hex')
const bufferHash = EthereumUtil.hashPersonalMessage(bytes)
const { v, r, s } = EthereumUtil.ecsign(bufferHash, privKey)

return EthereumUtil.toRpcSig(v, r, s)
},

signMessage: (message: string, privateKeys: EthereumPrivateKeys) => {
if (!isHex(message))
throw new Error(
'EthereumEngine: signMessage() requires a hex message parameter'
)
const privKey = Buffer.from(privateKeys.privateKey, 'hex')
const messageBuffer = hexToBuf(message)
const messageHash = EthereumUtil.hashPersonalMessage(messageBuffer)
const { v, r, s } = EthereumUtil.ecsign(messageHash, privKey)

return EthereumUtil.toRpcSig(v, r, s)
},

signTypedData: (
typedData: EIP712TypedDataParam,
privateKeys: EthereumPrivateKeys
Expand Down Expand Up @@ -1225,18 +1203,29 @@ export class EthereumEngine extends CurrencyEngine<
}
}

return this.utils.signMessage(message, ethereumPrivateKeys)
if (!isHex(message))
throw new Error(
'EthereumEngine: signMessage() requires a hex message parameter'
)

const privKey = Buffer.from(privateKeys.privateKey, 'hex')
const messageBuffer = hexToBuf(message)
const messageHash = EthereumUtil.hashPersonalMessage(messageBuffer)
const { v, r, s } = EthereumUtil.ecsign(messageHash, privKey)

return EthereumUtil.toRpcSig(v, r, s)
}

async signBytes(
bytes: Uint8Array,
privateKeys: JsonObject,
opts: EdgeSignBytesOptions
): Promise<string> {
async signBytes(bytes: Uint8Array, privateKeys: JsonObject): Promise<string> {
const ethereumPrivateKeys = asEthereumPrivateKeys(
this.currencyInfo.pluginId
)(privateKeys)
return this.utils.signBytes(bytes, ethereumPrivateKeys)

const privKey = Buffer.from(ethereumPrivateKeys.privateKey, 'hex')
const bufferHash = EthereumUtil.hashPersonalMessage(bytes)
const { v, r, s } = EthereumUtil.ecsign(bufferHash, privKey)

return EthereumUtil.toRpcSig(v, r, s)
}

async signTx(
Expand Down
2 changes: 0 additions & 2 deletions src/ethereum/ethereumTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,6 @@ export interface EIP712TypedDataParam {
}

export interface EthereumUtils {
signMessage: (message: string, privateKeys: EthereumPrivateKeys) => string
signBytes: (bytes: Uint8Array, privateKeys: EthereumPrivateKeys) => string
signTypedData: (
typedData: EIP712TypedDataParam,
privateKeys: EthereumPrivateKeys
Expand Down

0 comments on commit acba8e5

Please sign in to comment.