Skip to content

Commit

Permalink
Support eth_signTypedData_v4 (#149)
Browse files Browse the repository at this point in the history
* support eth_signTypedData_v4
* update package-lock.json
* try npm install
* show typed message
  • Loading branch information
hewigovens authored May 28, 2021
1 parent 6388538 commit b8bf6b0
Show file tree
Hide file tree
Showing 13 changed files with 22,101 additions and 6,391 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

- name: npm install, build, and test
run: |
npm ci
npm install
npm run build --if-present
npm test
working-directory: src
4 changes: 2 additions & 2 deletions dist/trust-min.js
Git LFS file not shown
5 changes: 2 additions & 3 deletions ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use_frameworks!
platform :ios, '11.0'
platform :ios, '12.0'

target 'TrustWeb3Provider_Example' do
use_frameworks! :linkage => :static
pod 'TrustWeb3Provider', :path => '../'
pod 'TrustWalletCore'
pod 'SwiftProtobuf'

target 'TrustWeb3Provider_Tests' do
inherit! :search_paths
Expand Down
17 changes: 8 additions & 9 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
PODS:
- SwiftProtobuf (1.14.0)
- TrustWalletCore (2.5.1):
- TrustWalletCore/Core (= 2.5.1)
- TrustWalletCore/Core (2.5.1):
- SwiftProtobuf (1.17.0)
- TrustWalletCore (2.6.9):
- TrustWalletCore/Core (= 2.6.9)
- TrustWalletCore/Core (2.6.9):
- TrustWalletCore/Types
- TrustWalletCore/Types (2.5.1):
- TrustWalletCore/Types (2.6.9):
- SwiftProtobuf
- TrustWeb3Provider (0.4.0)

DEPENDENCIES:
- SwiftProtobuf
- TrustWalletCore
- TrustWeb3Provider (from `../`)

Expand All @@ -23,10 +22,10 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
SwiftProtobuf: ed465ed18402f998fd117f631da27e0168e6b59c
TrustWalletCore: a6faaf299efb739d6bebba451dd2fb975edba834
SwiftProtobuf: 9c85136c6ba74b0a1b84279dbf0f6db8efb714e0
TrustWalletCore: b7f1b813a881fb6ad1a54bb5b9348f917dbf9afc
TrustWeb3Provider: c4fd1102867679e39224abf4449748cb4bd2a3a3

PODFILE CHECKSUM: 8983898624024172b19a1e0acdeeb4229d50f3fd
PODFILE CHECKSUM: 8d19f94c2edff45b1a7b1dbc9915f8746172e56a

COCOAPODS: 1.10.1
18 changes: 0 additions & 18 deletions ios/TrustWeb3Provider.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@
607FACCD1AFB9204008FA782 /* Frameworks */,
607FACCE1AFB9204008FA782 /* Resources */,
9B817E892CA041381AC440F9 /* [CP] Copy Pods Resources */,
18F19AD2B2E6FCC4FDC02A1A /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -280,23 +279,6 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
18F19AD2B2E6FCC4FDC02A1A /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-TrustWeb3Provider_Example/Pods-TrustWeb3Provider_Example-frameworks-${CONFIGURATION}-input-files.xcfilelist",
);
name = "[CP] Embed Pods Frameworks";
outputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-TrustWeb3Provider_Example/Pods-TrustWeb3Provider_Example-frameworks-${CONFIGURATION}-output-files.xcfilelist",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-TrustWeb3Provider_Example/Pods-TrustWeb3Provider_Example-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
6F4DDF0DB98752309D4DFBCD /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@
</BuildableReference>
</BuildableProductRunnable>
<EnvironmentVariables>
<EnvironmentVariable
key = "INFURA_API_KEY"
value = "376c261ffd284d46afa15e8aa5dc1e36"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
</LaunchAction>
<ProfileAction
Expand Down
40 changes: 36 additions & 4 deletions ios/TrustWeb3Provider/DAppWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class DAppWebViewController: UIViewController {
@IBOutlet weak var urlField: UITextField!

var homepage: String {
return "https://bscscan.com/address/0x05ff2b0db69458a0750badebc4f9e13add608c7f"
return "http://js-eth-sign.surge.sh/"
}

let privateKey = PrivateKey(data: Data(hexString: "0x4646464646464646464646464646464646464646464646464646464646464646")!)!
Expand Down Expand Up @@ -101,14 +101,21 @@ extension DAppWebViewController: WKScriptMessageHandler {
return
}
handleSignMessage(id: id, data: data, addPrefix: false)
break
case .signPersonalMessage:
guard let data = extractMessage(json: json) else {
print("data is missing")
return
}
handleSignMessage(id: id, data: data, addPrefix: true)
break
case .signTypedMessage:
guard
let data = extractMessage(json: json),
let raw = extractRaw(json: json)
else {
print("data or raw json is missing")
return
}
handleSignTypedMessage(id: id, data: data, raw: raw)
case .ecRecover:
guard let tuple = extractSignature(json: json) else {
print("signature or message is missing")
Expand All @@ -119,7 +126,6 @@ extension DAppWebViewController: WKScriptMessageHandler {
DispatchQueue.main.async {
self.webview.sendResult(recovered, to: id)
}
break
case .addEthereumChain:
guard let (chainId, name, rpcUrls) = extractChainInfo(json: json) else { return }
alert(title: name, message: "chainId: \(chainId)\n \(rpcUrls.joined(separator: "\n")))")
Expand Down Expand Up @@ -161,6 +167,22 @@ extension DAppWebViewController: WKScriptMessageHandler {
present(alert, animated: true, completion: nil)
}

func handleSignTypedMessage(id: Int64, data: Data, raw: String) {
let alert = UIAlertController(
title: "Sign Typed Message",
message: raw,
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "Cancel", style: .destructive, handler: { [weak webview] _ in
webview?.sendError("Canceled", to: id)
}))
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak webview] _ in
let signed = self.signMessage(data: data, addPrefix: false)
webview?.sendResult("0x" + signed.hexString, to: id)
}))
present(alert, animated: true, completion: nil)
}

func alert(title: String, message: String) {
let alert = UIAlertController(
title: title,
Expand Down Expand Up @@ -205,6 +227,16 @@ extension DAppWebViewController: WKScriptMessageHandler {
return (chainId: string, name: name, rpcUrls: urls)
}

private func extractRaw(json: [String: Any]) -> String? {
guard
let params = json["object"] as? [String: Any],
let raw = params["raw"] as? String
else {
return nil
}
return raw
}

private func signMessage(data: Data, addPrefix: Bool = true) -> Data {
let message = addPrefix ? Hash.keccak256(data: ethereumMessage(for: data)) : data
var signed = privateKey.sign(digest: message, curve: .secp256k1)!
Expand Down
5 changes: 5 additions & 0 deletions ios/TrustWeb3Provider/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
</dict>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
Expand Down
14 changes: 10 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Utils from "./utils";
import IdMapping from "./id_mapping";
import { EventEmitter } from "events";
import isUtf8 from "isutf8";
import { TypedDataUtils } from "eth-sig-util";

class TrustWeb3Provider extends EventEmitter {
constructor(config) {
Expand Down Expand Up @@ -162,9 +163,11 @@ class TrustWeb3Provider extends EventEmitter {
return this.personal_sign(payload);
case "personal_ecRecover":
return this.personal_ecRecover(payload);
case "eth_signTypedData":
case "eth_signTypedData_v3":
return this.eth_signTypedData(payload);
return this.eth_signTypedData(payload, false);
case "eth_signTypedData":
case "eth_signTypedData_v4":
return this.eth_signTypedData(payload, true);
case "eth_sendTransaction":
return this.eth_sendTransaction(payload);
case "eth_requestAccounts":
Expand Down Expand Up @@ -248,9 +251,12 @@ class TrustWeb3Provider extends EventEmitter {
});
}

eth_signTypedData(payload) {
eth_signTypedData(payload, useV4) {
const message = JSON.parse(payload.params[1]);
const hash = TypedDataUtils.sign(message, useV4);
this.postMessage("signTypedMessage", payload.id, {
data: payload.params[1],
data: "0x" + hash.toString("hex"),
raw: payload.params[1],
});
}

Expand Down
Loading

0 comments on commit b8bf6b0

Please sign in to comment.