-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
Stuff like this (impure, etc) is okay not to test programatically (i.e. test by inspection) for the moment |
@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 @@ | |||
[ |
There was a problem hiding this comment.
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
src/.external-ecmascript.js
Outdated
@@ -0,0 +1,192 @@ | |||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects |
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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)
test/light.test.ts
Outdated
@@ -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 = { |
There was a problem hiding this comment.
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
test/light.test.ts
Outdated
const tSigLong = | ||
'008c060ba60acad6594058e19e8d386adc4d4e142c3c44c140b4f15a331218abf9a5a11ef5022386a2174968f88f93fc246814b1097fb793d06b376d3452157104' | ||
|
||
await expect(L.submitEd25519Delegation({}, tReqLong, tPk, tSig)).rejects.toThrow() |
There was a problem hiding this comment.
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 | ||
|
There was a problem hiding this comment.
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
Key changes
initializeWindowWeb3
into /light/ which checks for existence of window.web3 and returns the appropriate status. (not sure how to test this one)submitEd25519Delegation
into /light/ which is uses axios to post the delegation request to the API.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