Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ready for review] New web3 support #4

Merged
merged 18 commits into from
Jul 25, 2018
Merged

[Ready for review] New web3 support #4

merged 18 commits into from
Jul 25, 2018

Conversation

nxtlvlrob
Copy link
Contributor

@nxtlvlrob nxtlvlrob commented Jul 23, 2018

Key changes

  • Added a function initializeWindowWeb3 into /light/ which checks for existence of window.web3 and returns the appropriate status. (not sure how to test this one)
  • Added a function submitEd25519Delegation into /light/ which is uses axios to post the delegation request to the API.
  • Also added a helper function to utils genRandomHex - was running into the issue of random hex lengths not being long enough. See screenshot + this is currently an open issue on web3.js - web3.utils.randomHex does not produce consistent length strings web3/web3.js#1490

screen shot 2018-07-23 at 3 46 10 pm

@nxtlvlrob nxtlvlrob requested a review from XertroV July 23, 2018 04:52
@nxtlvlrob nxtlvlrob changed the title New web3 support [WIP] New web3 support Jul 23, 2018
@nxtlvlrob nxtlvlrob changed the title [WIP] New web3 support [Ready for review] New web3 support Jul 23, 2018
@XertroV XertroV self-assigned this Jul 25, 2018
@XertroV
Copy link
Member

XertroV commented Jul 25, 2018

Added a function initializeWindowWeb3 into /light/ which checks for existence of window.web3 and returns the appropriate status. (not sure how to test this one)

Stuff like this (impure, etc) is okay not to test programatically (i.e. test by inspection) for the moment

@XertroV
Copy link
Member

XertroV commented Jul 25, 2018

@rob I've also added https://github.com/secure-vote/sv-lib/blob/master/CONTRIBUTING.md recently - okay for the moment but in future do not commit changes from /dist or /docs (particularly because it's /lib not /dist now). I'll remove them so all okay in that regard.

edit: heh, seems like you have /lib and /dist in there :P

@@ -0,0 +1,280 @@
[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be minified

@@ -0,0 +1,192 @@
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file shouldn't be committed AFAIK

src/ballotBox.ts Outdated
@@ -239,7 +239,11 @@ export const prepareWeb3BBVoteTx = async ({ txInfo }, { svNetwork }) => {
const { web3 } = svNetwork

assert.equal(web3Utils.isAddress(bbFarm), true, 'BBFarm address supplied is not a valid ethereum address.')
assert.equal(web3Utils.isAddress(userAddress), true, 'User address supplied is not a valid ethereum address.')
// assert.equal(
// web3Utils.isAddress(userAddress),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this commented out?

src/light.ts Outdated
return { detected: false, loaded: true }
}
const network = await detectNetwork(windowWeb3.currentProvider)
const _supported = network.id == 1 || network.id == 42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not so sure about this... feels icky and what about classic, ropsten, etc? Shoudl probs be based off our list of known networks rather than hardcoded

@@ -243,7 +280,7 @@ export const stellarPkToHex = (pubKey: string): string => {
} else {
const kp = StellarBase.Keypair.fromPublicKey(pubKey)
const rawPubKey = kp.rawPublicKey()
const hexPubKey = '0x' + rawPubKey.toString('hex')
hexPubKey = '0x' + rawPubKey.toString('hex')
Copy link
Member

@XertroV XertroV Jul 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variable not declared - any reason for removing 'const'?

src/light.ts Outdated
@@ -261,15 +298,15 @@ export const getUnsafeEd25519Delegations = async (pubKey: string, svNetwork) =>
const { unsafeEd25519DelegationAddr } = svConfig

const Ed25519Del = new web3.eth.Contract(UnsafeEd25519DelegationAbi, unsafeEd25519DelegationAddr)
const hexPubKey = stellarPkToHex(pubKey)
const delegations = await Ed25519Del.methods
.getAllForPubKey(stellarPkToHex(pubKey))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you declare hexPubKey here you should probably use it..

src/light.ts Outdated
@@ -360,3 +386,34 @@ export const ed25519DelegationIsValid = (dlgtRequest: string, pubKey: string, si
// Verify the request against the signature
return kp.verify(dlgtRequest, sigBuffer)
}

export const submitEd25519Delegation = async (SvNetwork: any, dlgtRequest: string, pubKey: string, signature: string) => {
return new Promise((resolve, reject) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

antipattern - promise in async function

src/light.ts Outdated
@@ -360,3 +386,34 @@ export const ed25519DelegationIsValid = (dlgtRequest: string, pubKey: string, si
// Verify the request against the signature
return kp.verify(dlgtRequest, sigBuffer)
}

export const submitEd25519Delegation = async (SvNetwork: any, dlgtRequest: string, pubKey: string, signature: string) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also any time you have const f = () => { return dosomething() } you should write const f = () => dosomething() instead unless there's good reason.

src/utils.ts Outdated

export const genRandomHex = (bytes: number) => {
let randomHex = null
do {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do {} while () is super ugly and rarely used because it's harder to read; we should refactor this to be a bit more intelligent; e.g genRandomHex = (nBytes) => web3Utils.randomHex(nBytes + 10).slice(0, nBytes * 2 + 2)

@@ -5,3 +5,25 @@ import * as C from '../src/const'
test('prepareEd25519Delegation works', () => {
expect(L.prepareEd25519Delegation(C.zeroAddr, 'ffffff')).toBe('0x53562d45442d455448ffffff0000000000000000000000000000000000000000')
})

test('submitEd25519Delegation works', async () => {
const SvNetwork = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have constants outside testing functions to avoid redelcaring them

const tSigLong =
'008c060ba60acad6594058e19e8d386adc4d4e142c3c44c140b4f15a331218abf9a5a11ef5022386a2174968f88f93fc246814b1097fb793d06b376d3452157104'

await expect(L.submitEd25519Delegation({}, tReqLong, tPk, tSig)).rejects.toThrow()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there any tests for a successful request?

src/const.ts Outdated
}

export const doesNetHaveIndex = (networkId, chainId) => getNetwork(networkId, chainId).indexEnsName !== undefined

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, we should alsocheck if its an empty string

@XertroV XertroV merged commit e089e09 into master Jul 25, 2018
@XertroV XertroV deleted the new-web3-support branch July 25, 2018 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants