wip eth login (#141)

This commit is contained in:
Martti Malmi 2022-07-13 16:12:30 +03:00 committed by GitHub
parent 8a94a6a057
commit 19a4869aae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 4 deletions

View File

@ -49,6 +49,7 @@
"dependencies": {
"@zxing/library": "^0.18.6",
"autolinker": "^3.14.3",
"elliptic": "^6.5.4",
"fuse.js": "^6.6.2",
"gun": "mmalmi/gun",
"history": "^5.3.0",

View File

@ -8,6 +8,7 @@ import iris from './iris-lib';
import _ from 'lodash';
import Fuse from "./lib/fuse.basic.esm.min";
import localforage from './lib/localforage.min';
import { ec as EC } from 'elliptic';
let key;
let myName;
@ -161,6 +162,65 @@ function updateGroups() {
});
}
const isHex = (maybeHex) =>
maybeHex.length !== 0 && maybeHex.length % 2 === 0 && !/[^a-fA-F0-9]/u.test(maybeHex);
const hexToUint8Array = (hexString) => {
if (!isHex(hexString)) {
throw new Error('Not a hex string');
}
return Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
}
function arrayToBase64Url(array) {
return btoa(String.fromCharCode.apply(null, array)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
}
function keyPairFromHash(hash) {
const ec = new EC('p256');
const keyPair = ec.keyFromPrivate(new Uint8Array(hash));
let privKey = keyPair.getPrivate().toArray("be", 32);
let x = keyPair.getPublic().getX().toArray("be", 32);
let y = keyPair.getPublic().getY().toArray("be", 32);
privKey = arrayToBase64Url(privKey);
x = arrayToBase64Url(x);
y = arrayToBase64Url(y);
const kp = { pub: `${x}.${y}`, priv: privKey };
return kp;
}
async function ethereumLogin(name) {
const accounts = await window.ethereum.request({method: 'eth_accounts'});
if (accounts.length > 0) {
const message = "I'm trusting this application with an irrevocable access key to my Iris account.";
const signature = await window.ethereum.request({method: 'personal_sign', params: [accounts[0], message]});
const signatureBytes = hexToUint8Array(signature.substring(2));
const hash1 = await window.crypto.subtle.digest('SHA-256', signatureBytes);
const hash2 = await window.crypto.subtle.digest('SHA-256', hash1);
const signingKey = keyPairFromHash(hash1);
const encryptionKey = keyPairFromHash(hash2);
const k = {
pub: signingKey.pub,
priv: signingKey.priv,
epub: encryptionKey.pub,
epriv: encryptionKey.priv
};
login(k);
setTimeout(async () => {
State.public.user().get('profile').get('name').once(existingName => {
if (typeof existingName !== 'string' || existingName === '') {
name = name || Helpers.generateName();
State.public.user().get('profile').put({a:null});
State.public.user().get('profile').get('name').put(name);
}
});
}, 2000);
}
}
function login(k) {
const shouldRefresh = !!key;
key = k;
@ -508,4 +568,4 @@ function followChatLink(str) {
}
}
export default {init, followChatLink, getKey, getPubKey, updateUserSearchIndex, getUserSearchIndex, getMyName, getMyProfilePhoto, getMyChatLink, createChatLink, ourActivity, login, logOut, addFollow, removeFollow, loginAsNewUser, DEFAULT_SETTINGS, channels, newChannel, addChannel, processMessage, subscribeToMsgs };
export default {init, followChatLink, getKey, getPubKey, ethereumLogin, updateUserSearchIndex, getUserSearchIndex, getMyName, getMyProfilePhoto, getMyChatLink, createChatLink, ourActivity, login, logOut, addFollow, removeFollow, loginAsNewUser, DEFAULT_SETTINGS, channels, newChannel, addChannel, processMessage, subscribeToMsgs };

View File

@ -85,7 +85,7 @@ class Login extends Component {
<p><button id="sign-up" type="submit">{t('new_user_go')}</button></p>
<br />
<p><a href="#" id="show-existing-account-login" onClick={() => this.setState({showSwitchAccount: true})}>{t('already_have_an_account')}</a></p>
{/* window.ethereum && <p><a href="#" onClick={() => Session.ethereumLogin()}>{t('ethereum_login')}</a></p>*/}
{window.ethereum && <p><a href="#" onClick={() => Session.ethereumLogin()}>{t('ethereum_login')}</a></p>}
<p>
<LanguageSelector />
</p>

View File

@ -4551,9 +4551,9 @@ electron-to-chromium@^1.4.17:
resolved "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.47.tgz"
integrity sha512-ZHc8i3/cgeCRK/vC7W2htAG6JqUmOUgDNn/f9yY9J8UjfLjwzwOVEt4MWmgJAdvmxyrsR5KIFA/6+kUHGY0eUA==
elliptic@^6.5.3:
elliptic@^6.5.3, elliptic@^6.5.4:
version "6.5.4"
resolved "https://registry.npmjs.org/elliptic/-/elliptic-6.5.4.tgz"
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.5.4.tgz#da37cebd31e79a1367e941b592ed1fbebd58abbb"
integrity sha512-iLhC6ULemrljPZb+QutR5TQGB+pdW6KGD5RSegS+8sorOZT+rdQFbsQFJgvN3eRqNALqJer4oQ16YvJHlU8hzQ==
dependencies:
bn.js "^4.11.9"