Skip to content

Commit

Permalink
Improve personal_sign compatibility (#105)
Browse files Browse the repository at this point in the history
* improve personal_sign compatibility

* Add personal_sign test
  • Loading branch information
hewigovens authored Oct 16, 2020
1 parent a72dd84 commit ad834d3
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 20 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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: '<latest_tag>'
```

Here is an example project located at `ios/TrustWeb3Provider.xcworkspace` to demonstrate how to use this provider.
Expand Down Expand Up @@ -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: '<latest_tag>'
}
```
e

## Authors

[vikmeup](https://github.com/vikmeup)
Expand Down
4 changes: 2 additions & 2 deletions dist/trust-min.js
Git LFS file not shown
4 changes: 2 additions & 2 deletions src/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ module.exports = {
"unix"
],
"quotes": [
"error",
"warn",
"double"
],
"semi": [
"error",
"always"
],
"no-console": [
"warn"
"off"
]
}
};
18 changes: 10 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand All @@ -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) {
Expand Down
40 changes: 39 additions & 1 deletion src/tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
Expand Down Expand Up @@ -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()
11 changes: 9 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ad834d3

Please sign in to comment.