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

Fix sponsorships in Gardens UI #907

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@types/sanitize-html": "^2.6.2",
"assert": "^2.0.0",
"bignumber.js": "^9.0.0",
"brightid_sdk_v5": "^1.0.2",
"buffer": "^6.0.3",
"canvas-confetti": "^1.4.0",
"clipboard-polyfill": "^3.0.0-pre5",
Expand Down
12 changes: 11 additions & 1 deletion src/components/Profile/BrightIdModal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect, useState } from 'react'
import QRCode from 'qrcode.react'
import {
Button,
GU,
Info,
Modal,
Expand All @@ -15,6 +16,7 @@ import { sponsorUser } from '@/services/sponsorUser'

function BrightIdModal({ account, addressExist, visible, onClose }) {
const [error, setError] = useState(null)
const [hasScannedQr, setHasScannedQr] = useState(false)

const deepLink = `${BRIGHT_ID_APP_DEEPLINK}/${account}`
const { width } = useViewport()
Expand Down Expand Up @@ -85,7 +87,7 @@ function BrightIdModal({ account, addressExist, visible, onClose }) {
>
Connect with BrightID
</h5>
{addressExist ? (
{addressExist && hasScannedQr ? (
<div
css={`
display: flex;
Expand Down Expand Up @@ -141,6 +143,14 @@ function BrightIdModal({ account, addressExist, visible, onClose }) {
Scanning this code will prevent any previously connected addresses
from connecting to BrightID in the future through any 1Hive apps
</Info>
<Button
onClick={() => setHasScannedQr(true)}
css={`
margin-top: ${3 * GU}px;
`}
>
Continue after Scanning QR Code
</Button>
</>
)}
</div>
Expand Down
95 changes: 11 additions & 84 deletions src/services/sponsorUser.js
Original file line number Diff line number Diff line change
@@ -1,98 +1,25 @@
import tweetNacl from 'tweetnacl'
import tweetNaclUtils from 'tweetnacl-util'
import stringify from 'fast-json-stable-stringify'

import { CONTEXT_ID } from '../constants'
import { BRIGHTID_SUBSCRIPTION_ENDPOINT, UTC_API_ENDPOINT } from '../endpoints'
import { NO_CONTENT } from './brightIdResponseCodes'
import { NO_CONTENT } from './responseCodes'
import env from '../environment'
import { sponsor } from 'brightid_sdk_v5'

export async function sponsorUser(account) {
try {
let timestamp
const privateKey = env('NODE_PK')

if (!privateKey) {
return { error: 'No private key found for the node' }
}

try {
const dateResponse = await fetch(UTC_API_ENDPOINT, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
})

const dateResponseJson = await dateResponse.json()
const dateString = dateResponseJson.currentDateTime
const currentDate = new Date(dateString)
timestamp = currentDate.getTime()
} catch (error) {
// fallback to system time in case something went wrong with the utc time api
timestamp = Date.now()
}
const privateKey = env('NODE_PK')

const op = {
v: 5,
name: 'Sponsor',
app: CONTEXT_ID,
timestamp,
contextId: account,
}
const message = getMessage(op)
const messageUint8Array = Buffer.from(message)

const privateKeyUint8Array = tweetNaclUtils.decodeBase64(privateKey)

const signedMessageUint8Array = tweetNacl.sign.detached(
messageUint8Array,
privateKeyUint8Array
)

op.sig = tweetNaclUtils.encodeBase64(signedMessageUint8Array)

const endpoint = `${BRIGHTID_SUBSCRIPTION_ENDPOINT}`
const rawResponse = await fetch(endpoint, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(op),
})

if (rawResponse.ok) {
return {
error: null,
}
}

const response = await rawResponse.json()
if (!privateKey) {
return { error: 'No private key found for the node' }
}

if (response.code === NO_CONTENT) {
try {
await sponsor(privateKey, CONTEXT_ID, account)
} catch (err) {
if (err.code === NO_CONTENT) {
return {
error: null,
}
}

return {
error: response.errorMessage,
}
} catch (err) {
console.error(err)
return { error: err }
}
}

function getMessage(op) {
const signedOp = {}
for (const k in op) {
if (['sig', 'sig1', 'sig2', 'hash'].includes(k)) {
continue
error: err.errorMessage,
}
signedOp[k] = op[k]
}
return stringify(signedOp)
}
16 changes: 13 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3739,7 +3739,7 @@ axios@^0.18.0:
follow-redirects "1.5.10"
is-buffer "^2.0.2"

axios@^0.21.1:
axios@^0.21.0, axios@^0.21.1:
version "0.21.4"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575"
integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==
Expand Down Expand Up @@ -4464,7 +4464,7 @@ base-x@^3.0.2:
dependencies:
safe-buffer "^5.0.1"

base64-js@^1.0.2, base64-js@^1.3.1:
base64-js@^1.0.2, base64-js@^1.3.1, base64-js@^1.5.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a"
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==
Expand Down Expand Up @@ -4637,6 +4637,16 @@ braces@^3.0.1, braces@~3.0.2:
dependencies:
fill-range "^7.0.1"

brightid_sdk_v5@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/brightid_sdk_v5/-/brightid_sdk_v5-1.0.2.tgz#9f6d065bc4fa09357f2fa86238cccf14d7e7475f"
integrity sha512-6s7bxsp8shxEy78HmSaoVnFKSzkYP8tOS6Qk1q3lWm07QTC/LP3z1gDbh4CBnxHRqpjPnknLdvCiFjaDXLgCgw==
dependencies:
axios "^0.21.0"
base64-js "^1.5.1"
fast-json-stable-stringify "^2.1.0"
tweetnacl "^1.0.3"

brorand@^1.0.1, brorand@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/brorand/-/brorand-1.1.0.tgz#12c25efe40a45e3c323eb8675a0a0ce57b22371f"
Expand Down Expand Up @@ -14210,7 +14220,7 @@ tweetnacl@^0.14.3, tweetnacl@~0.14.0:
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64"
integrity sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q=

tweetnacl@^1.0.0:
tweetnacl@^1.0.0, tweetnacl@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-1.0.3.tgz#ac0af71680458d8a6378d0d0d050ab1407d35596"
integrity sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==
Expand Down