Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Elixxir dApps SDK Kotlin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mobile
android
Elixxir dApps SDK Kotlin
Commits
b3e83a93
Commit
b3e83a93
authored
Aug 12, 2022
by
Kamal Bramwell
Browse files
Options
Downloads
Patches
Plain Diff
Encapsulated NDF logic
parent
9c72cfc5
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/src/main/java/io/elixxir/dapp/session/data/NdfDataSource.kt
+72
-0
72 additions, 0 deletions
...c/main/java/io/elixxir/dapp/session/data/NdfDataSource.kt
with
72 additions
and
0 deletions
app/src/main/java/io/elixxir/dapp/session/data/NdfDataSource.kt
0 → 100644
+
72
−
0
View file @
b3e83a93
package
io.elixxir.dapp.session.data
import
android.app.Application
import
bindings.Bindings
import
io.elixxir.dapp.R
import
io.elixxir.dapp.session.models.Environment
import
kotlinx.coroutines.*
typealias
Ndf
=
String
interface
NdfDataSource
{
suspend
fun
fetchNdf
():
Ndf
}
class
RemoteNdfDataSource
internal
constructor
(
private
val
app
:
Application
,
private
val
environment
:
Environment
,
private
val
defaultDispatcher
:
CoroutineDispatcher
=
Dispatchers
.
IO
)
:
NdfDataSource
{
override
suspend
fun
fetchNdf
():
Ndf
{
return
retryFetchNdf
()
}
private
suspend
fun
retryFetchNdf
(
retries
:
Int
=
0
):
Ndf
=
withContext
(
defaultDispatcher
)
{
val
ndf
=
when
(
environment
)
{
Environment
.
MainNet
->
{
downloadAndVerifySignedNdfWithUrl
(
NDF_URL_MAINNET
,
certificateFor
(
Environment
.
MainNet
)
)
}
Environment
.
ReleaseNet
->
{
downloadAndVerifySignedNdfWithUrl
(
NDF_URL_RELEASE
,
certificateFor
(
Environment
.
ReleaseNet
)
)
}
}
if
(
ndf
.
isEmpty
()
&&
retries
<=
NDF_MAX_RETRIES
)
{
ensureActive
()
delay
(
NDF_POLL_INTERVAL_MS
)
retryFetchNdf
(
retries
+
1
)
}
else
{
ndf
}
}
private
fun
downloadAndVerifySignedNdfWithUrl
(
url
:
String
,
certificate
:
String
):
Ndf
=
String
(
Bindings
.
downloadAndVerifySignedNdfWithUrl
(
url
,
certificate
))
private
fun
certificateFor
(
environment
:
Environment
):
String
{
val
certFile
:
Int
=
when
(
environment
)
{
Environment
.
MainNet
->
R
.
raw
.
mainnet
Environment
.
ReleaseNet
->
R
.
raw
.
release
}
val
reader
=
app
.
resources
.
openRawResource
(
certFile
)
.
bufferedReader
()
return
reader
.
use
{
reader
.
readText
()
}
}
companion
object
{
private
const
val
NDF_URL_MAINNET
=
"https://elixxir-bins.s3.us-west-1.amazonaws.com/ndf/mainnet.json"
private
const
val
NDF_URL_RELEASE
=
"https://elixxir-bins.s3.us-west-1.amazonaws.com/ndf/release.json"
private
const
val
NDF_MAX_RETRIES
=
29
private
const
val
NDF_POLL_INTERVAL_MS
=
1000L
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment