Skip to content

Commit

Permalink
fixup! Support signing non-hex messages Required for signing Bity orders
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon-edge committed Jul 30, 2024
1 parent b46ee60 commit cbdafa2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"chai": "^4.2.0",
"clipanion": "^4.0.0-rc.2",
"crypto-browserify": "^3.12.0",
"edge-core-js": "^2.9.0",
"edge-core-js": "https://github.com/EdgeApp/edge-core-js.git#0acc4059ceab416f01f98ba17c826c1820d2aba4",
"esbuild-loader": "^2.20.0",
"eslint": "^8.19.0",
"eslint-config-standard-kit": "0.15.1",
Expand Down
28 changes: 25 additions & 3 deletions src/ethereum/EthereumEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
EdgeCurrencyInfo,
EdgeFetchFunction,
EdgeFreshAddress,
EdgeSignBytesOptions,
EdgeSignMessageOptions,
EdgeSpendInfo,
EdgeSpendTarget,
Expand Down Expand Up @@ -143,11 +144,21 @@ 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 = isHex(message)
? hexToBuf(message)
: Buffer.from(message)
const messageBuffer = hexToBuf(message)
const messageHash = EthereumUtil.hashPersonalMessage(messageBuffer)
const { v, r, s } = EthereumUtil.ecsign(messageHash, privKey)

Expand Down Expand Up @@ -1217,6 +1228,17 @@ export class EthereumEngine extends CurrencyEngine<
return this.utils.signMessage(message, ethereumPrivateKeys)
}

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

async signTx(
edgeTransaction: EdgeTransaction,
privateKeys: JsonObject
Expand Down
1 change: 1 addition & 0 deletions src/ethereum/ethereumTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ 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
7 changes: 3 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3987,10 +3987,9 @@ [email protected]:
bindings "^1.2.1"
nan "^2.0.9"

edge-core-js@^2.9.0:
version "2.9.0"
resolved "https://registry.yarnpkg.com/edge-core-js/-/edge-core-js-2.9.0.tgz#99a73c9b7903637f3551ef8cfb771e821193145c"
integrity sha512-hRxiJSe0NZtiokb4zmDr4fFnAspGN6i8dsVqRfOGeOzyJwm/1yo0K1lx70A1pkXHmNIrsirrp80wPcmLVSvl+Q==
"edge-core-js@https://github.com/EdgeApp/edge-core-js.git#0acc4059ceab416f01f98ba17c826c1820d2aba4":
version "2.9.1"
resolved "https://github.com/EdgeApp/edge-core-js.git#0acc4059ceab416f01f98ba17c826c1820d2aba4"
dependencies:
aes-js "^3.1.0"
base-x "^4.0.0"
Expand Down

0 comments on commit cbdafa2

Please sign in to comment.