From ad834d3028aea6321fd62b298a0a594d624a8c33 Mon Sep 17 00:00:00 2001 From: hewigovens <360470+hewigovens@users.noreply.github.com> Date: Fri, 16 Oct 2020 13:35:07 +0800 Subject: [PATCH] Improve personal_sign compatibility (#105) * improve personal_sign compatibility * Add personal_sign test --- README.md | 9 ++++----- dist/trust-min.js | 4 ++-- src/.eslintrc.js | 4 ++-- src/index.js | 18 ++++++++++-------- src/tests/test.js | 40 +++++++++++++++++++++++++++++++++++++++- src/utils.js | 11 +++++++++-- 6 files changed, 66 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 861d205c..5eb44029 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # TrustWeb3Provider -[![Pod Version](https://img.shields.io/cocoapods/v/TrustWeb3Provider.svg?style=flat)](http://cocoapods.org/pods/TrustWeb3Provider) -[![Jitpack Version](https://jitpack.io/v/TrustWallet/trust-web3-provider.svg)](https://jitpack.io/#TrustWallet/trust-web3-provider/0.2.1) +![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/trustwallet/trust-web3-provider) [![License](https://img.shields.io/cocoapods/l/TrustWeb3Provider.svg?style=flat)](http://cocoapods.org/pods/TrustWeb3Provider) [![Platform](https://img.shields.io/cocoapods/p/TrustWeb3Provider.svg?style=flat)](http://cocoapods.org/pods/TrustWeb3Provider) [![Platform](https://img.shields.io/badge/platform-android-lightgrey.svg)](https://jitpack.io/#TrustWallet/trust-web3-provider/0.2.1) @@ -24,7 +23,7 @@ TrustWeb3Provider is available through [CocoaPods](http://cocoapods.org). To ins it, simply add the following line to your Podfile: ```ruby -pod 'TrustWeb3Provider' +pod 'TrustWeb3Provider', git: 'https://github.com/trustwallet/trust-web3-provider', tag: '' ``` Here is an example project located at `ios/TrustWeb3Provider.xcworkspace` to demonstrate how to use this provider. @@ -80,10 +79,10 @@ Step 2. Add the dependency ```groovy dependencies { - implementation group: 'com.trustwallet', name: 'web3-provider', version: '0.4.2' + implementation group: 'com.trustwallet', name: 'web3-provider', version: '' } ``` -e + ## Authors [vikmeup](https://github.com/vikmeup) diff --git a/dist/trust-min.js b/dist/trust-min.js index 4c0f9579..f885ee9c 100644 --- a/dist/trust-min.js +++ b/dist/trust-min.js @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:93ffe1eac4c7c904c92ce870e6e334ce8037191b4208ee90fd45b57592168e0a -size 535058 +oid sha256:3fe80a1f5c4051e72d46485f26a79001c6007380ee60a75abf38db760712d661 +size 535220 diff --git a/src/.eslintrc.js b/src/.eslintrc.js index ecc50cdf..b55bcfad 100644 --- a/src/.eslintrc.js +++ b/src/.eslintrc.js @@ -17,7 +17,7 @@ module.exports = { "unix" ], "quotes": [ - "error", + "warn", "double" ], "semi": [ @@ -25,7 +25,7 @@ module.exports = { "always" ], "no-console": [ - "warn" + "off" ] } }; diff --git a/src/index.js b/src/index.js index 63a1c677..c665c7d8 100644 --- a/src/index.js +++ b/src/index.js @@ -201,13 +201,7 @@ class TrustWeb3Provider extends EventEmitter { } eth_sign(payload) { - const data = payload.params[1]; - var buffer; - if ((typeof (data) === "string")) { - buffer = Utils.hexToBuffer(data); - } else { - buffer = Buffer.from(data); - } + const buffer = Utils.messageToBuffer(payload.params[1]); const hex = buffer.toString("hex"); if (isUtf8(buffer)) { this.postMessage("signPersonalMessage", payload.id, {data: hex}); @@ -217,7 +211,15 @@ class TrustWeb3Provider extends EventEmitter { } personal_sign(payload) { - this.postMessage("signPersonalMessage", payload.id, {data: payload.params[0]}); + const message = payload.params[0]; + const buffer = Utils.messageToBuffer(message); + if (buffer.length === 0) { + // hex it + const hex = Buffer.from(message).toString("hex"); + this.postMessage("signPersonalMessage", payload.id, {data: hex}); + } else { + this.postMessage("signPersonalMessage", payload.id, {data: message}); + } } personal_ecRecover(payload) { diff --git a/src/tests/test.js b/src/tests/test.js index 005a2894..aeb4fd4b 100644 --- a/src/tests/test.js +++ b/src/tests/test.js @@ -23,6 +23,12 @@ const ropsten = { rpcUrl: "https://ropsten.infura.io/apikey", }; +const bsc = { + address: "0x9d8A62f656a8d1615C1294fd71e9CFb3E4855A4F", + chainId: 56, + rpcUrl: "https://bsc-dataseed1.binance.orge", +} + describe("TrustWeb3Provider constructor tests", () => { test("test constructor.name", () => { const provider = new Trust({}); @@ -128,4 +134,36 @@ describe("TrustWeb3Provider constructor tests", () => { }); }); -}); // describe() + test("test personal_sign", done => { + const provider = new Trust(bsc); + const signed = "0xf3a9e21a3238b025b7edf5013876548cfb2f2a838aca573de88c91ea9aecf7190cd6330a0172bd5d106841647831f30065f644eddc2f86091e1bb370c9ff833f1c"; + + window.webkit = { + messageHandlers: { + signPersonalMessage: { + postMessage: (message) => { + const buffer = Buffer.from(message.object.data); + if (buffer.length === 0) { + throw new Error("message is not hex!") + } + provider.sendResponse(message.id, signed); + } + } + } + }; + + const request = { + "method": "personal_sign", + "params": ["{\"version\":\"0.1.2\",\"timestamp\":\"1602823075\",\"token\":\"0x4b0f1812e5df2a09796481ff14017e6005508003\",\"type\":\"vote\",\"payload\":{\"proposal\":\"QmSV53XuYi28XfdNHDhBVp2ZQwzeewQNBcaDedRi9PC6eY\",\"choice\":1,\"metadata\":{}}}", "0x9d8A62f656a8d1615C1294fd71e9CFb3E4855A4F"], + "id": 1602823075454 + }; + + expect(Buffer.from(request.params[0], 'hex').length).toEqual(0); + + provider.request(request).then(result => { + expect(result).toEqual(signed); + done(); + }); + }); + +}); // end of top describe() diff --git a/src/utils.js b/src/utils.js index 3e6c34bc..60a0293c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -39,8 +39,15 @@ class Utils { return "0x" + hexString; } - static hexToBuffer(str) { - return Buffer.from(str.replace("0x", ""), "hex"); + // message: Bytes | string + static messageToBuffer(message) { + var buffer; + if ((typeof (message) === "string")) { + buffer = Buffer.from(message.replace("0x", ""), "hex"); + } else { + buffer = Buffer.from(message); + } + return buffer; } static bufferToHex(buf) {