Skip to content

Commit

Permalink
Add personal_sign
Browse files Browse the repository at this point in the history
  • Loading branch information
imyugioh committed Feb 11, 2021
1 parent 1ff5684 commit 0906372
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"bignumber.js": "^9.0.0",
"clean-webpack-plugin": "^3.0.0",
"copy-webpack-plugin": "^6.0.0",
"elliptic": "^6.5.4",
"ethjs-util": "^0.1.6",
"file-loader": "^6.0.0",
"file-saver": "^2.0.2",
Expand Down
5 changes: 3 additions & 2 deletions src/content/WalletProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ class WalletProvider {
}
});
}
sign(data: string) {
sign(msgData: string | Uint8Array, prefixMsg = "Ethereum Signed Message") {
return new Promise(async (resolve, reject) => {
try {
const res = await sendAsyncMessageToContentScript({
type: THIRDPARTY_PERSONAL_SIGN_REQUEST,
payload: {
data,
msgData,
prefixMsg,
},
});
if (res.rejected) {
Expand Down
55 changes: 44 additions & 11 deletions src/popup/pages/API/PersonalSign.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div class="sign__address">{{ wallet.address }}</div>
<span class="action_caption">Data</span>
<div class="data_container">
{{ msgData }}
{{ getString(signData) }}
</div>
<div v-if="!loading">
<div v-if="!wallet.isLedger" class="password-content">
Expand Down Expand Up @@ -76,8 +76,10 @@
<script>
import { decryptKeyStore } from "services/AccountService";
import { mapState, mapGetters } from "vuex";
import { sign } from "@harmony-js/crypto";
import { joinSignature, hexZeroPad, keccak256 } from "@harmony-js/crypto";
import _ from "lodash";
import { strip0x } from "@harmony-js/utils";
import elliptic from "elliptic";
import {
GET_WALLET_SERVICE_STATE,
THIRDPARTY_PERSONAL_SIGN_CONNECT,
Expand All @@ -93,7 +95,7 @@ export default {
host: "",
password: "",
hasError: false,
msgData: "",
signData: false,
caption: LEDGER_CONFIRM_PREPARE,
privateKey: null,
wallet: {
Expand All @@ -109,15 +111,46 @@ export default {
}),
},
methods: {
async personal_sign(msgData) {
getString(signData) {
const { msgData } = signData;
return typeof msgData === "string"
? msgData
: Buffer.from(Object.values(msgData)).toString();
},
async personal_sign(signData) {
try {
let signature;
signature = sign(msgData, this.privateKey);
const { msgData, prefixMsg } = signData;
const data =
typeof msgData === "string"
? Buffer.from(msgData, "utf8")
: Buffer.from(Object.values(msgData));
const secp256k1 = elliptic.ec("secp256k1");
const prefix = Buffer.from(
`\u0019${prefixMsg}:\n${data.length.toString()}`,
"utf-8"
);
const msgHashHarmony = keccak256(Buffer.concat([prefix, data])).slice(
2
);
const keyPair = secp256k1.keyFromPrivate(
strip0x(this.privateKey),
"hex"
);
const signature = keyPair.sign(msgHashHarmony, { canonical: true });
const result = {
recoveryParam: signature.recoveryParam,
r: hexZeroPad("0x" + signature.r.toString(16), 32),
s: hexZeroPad("0x" + signature.s.toString(16), 32),
v: 27 + signature.recoveryParam,
};
chrome.runtime.sendMessage({
action: THIRDPARTY_PERSONAL_SIGN_SUCCESS_RESPONSE,
payload: {
data: signature,
data: joinSignature(result),
},
});
} catch (err) {
Expand Down Expand Up @@ -154,7 +187,7 @@ export default {
this.privateKey = privateKey;
this.$store.commit("loading", true);
await this.personal_sign(this.msgData);
await this.personal_sign(this.signData);
this.$store.commit("loading", false);
},
Expand All @@ -178,10 +211,10 @@ export default {
chrome.runtime.sendMessage(
{ action: GET_WALLET_SERVICE_STATE },
async ({ state } = {}) => {
if (state && state.msgData && state.session) {
if (state && state.signData && state.session) {
try {
const { msgData, session } = state;
this.msgData = msgData;
const { signData, session } = state;
this.signData = signData;
this.host = session.host;
this.wallet = _.find(this.wallets.accounts, {
address: session.account.address,
Expand Down
6 changes: 3 additions & 3 deletions src/services/APIService.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class APIService {
constructor() {
this.params = null;
this.txnInfo = null;
this.msgData = null;
this.signData = null;
this.type = null;
this.sender = null;
this.host = "";
Expand All @@ -156,7 +156,7 @@ class APIService {
type: this.type,
host: this.host,
txnInfo: this.txnInfo,
msgData: this.msgData,
signData: this.signData,
params: this.params,
session: this.activeSession,
};
Expand Down Expand Up @@ -201,7 +201,7 @@ class APIService {
const store = this.getVuexStore();
this.sender = sender.tab.id;
this.host = getHostNameFromTab(sender.tab);
this.msgData = payload.data;
this.signData = payload;
const session = await this.getSession(this.host);
if (session.exist) {
const findAcc = _.find(store.wallets.accounts, {
Expand Down

0 comments on commit 0906372

Please sign in to comment.