Skip to content

Commit

Permalink
work on ethereum
Browse files Browse the repository at this point in the history
  • Loading branch information
jon1012 committed Feb 21, 2020
1 parent 5f7e9f9 commit 7f5d203
Show file tree
Hide file tree
Showing 5 changed files with 6,349 additions and 118 deletions.
5 changes: 2 additions & 3 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ parserOptions:
ecmaVersion: 8
sourceType: module
rules:
indent:
- error
- 2
no-deprecated-api:
- off
comma-dangle:
- error
- never
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@
"bs58": "^4.0.1",
"eciesjs": "^0.3.1",
"elliptic": "^6.5.1",
"ethers": "^4.0.42",
"ethers": "^4.0.45",
"ripemd160": "^2.0.2",
"secp256k1": "^3.6.2",
"sha.js": "^2.4.11",
"web3": "^1.2.4",
"rollup": "^1.31.0",
"rollup-plugin-commonjs": "^9.2.0",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-resolve": "^4.0.0"
"rollup-plugin-node-resolve": "^4.0.0",
"secp256k1": "^3.6.2",
"sha.js": "^2.4.11",
"web3": "^1.2.4"
},
"files": [
"dist"
Expand Down
55 changes: 54 additions & 1 deletion src/api/ethereum.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,64 @@
import * as bip39 from 'bip39'
const ethers = require('ethers')

function get_verification_buffer(message) {
// Returns a serialized string to verify the message integrity
return Buffer.from(`${message.chain}\n${message.sender}\n${message.type}\n${message.item_hash}`)
}

export async function sign(w3, address, message) {
export async function w3_sign(w3, address, message) {
let buffer = get_verification_buffer(message)
let signed = await w3.eth.personal.sign(buffer.toString(), address, '')
message.signature = signed
return message
}

export async function new_account({path = "m/44'/60'/0'/0/0"} = {}) {
let mnemonics = bip39.generateMnemonic()
console.log(ethers, mnemonics)
return import_account({
'mnemonics': mnemonics,
'path': path
})
}

async function _from_wallet(wallet) {
if (wallet) {
let account = {
'private_key': wallet.privateKey,
'mnemonics': wallet.mnemonic,
'address': wallet.address,
'type': 'ETH',
'provider': 'integrated'
}
if (name)
account['name'] = name
else
account['name'] = account['address']

return account
}
return null
}

export async function import_account({
private_key = null, mnemonics = null, path = "m/44'/60'/0'/0/0",
name = null} = {}){

let wallet = null
if (mnemonics) {
wallet = ethers.Wallet.fromMnemonic(mnemonics, path)
} else if (private_key !== null) {
wallet = ethers.Wallet(private_key)
}
return await _from_wallet(wallet)
}

export async function from_provider(provider) {
// You should likely pass web3.currentProvider
const ethprovider = new ethers.providers.Web3Provider(provider)

// There is only ever up to one account in MetaMask exposed
const signer = ethprovider.getSigner()
return _from_wallet(signer)
}
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as aggregates from './api/aggregates.js';
import * as nuls from './api/nuls.js';
import * as nuls2 from './api/nuls2.js';
import * as ethereum from './api/ethereum.js';
import * as posts from './api/posts.js';
import * as store from './api/store.js';
import * as encryption from './api/encryption.js';
export {aggregates, nuls, nuls2, posts, store, encryption};
export {aggregates, nuls, nuls2, ethereum, posts, store, encryption};

export {
ipfs_push, storage_push,
Expand Down
Loading

0 comments on commit 7f5d203

Please sign in to comment.