Permalink
Browse files

replace nacl native with ports

  • Loading branch information...
JustinLove committed Jul 7, 2018
1 parent 32a90a1 commit 9027adaa927fe5f6fb775af5e79bb620995a1817
Showing with 68 additions and 126 deletions.
  1. +0 −1 elm-package.json
  2. +1 −1 notes.md
  3. +11 −0 public/admin-main.js
  4. +44 −46 src/Admin.elm
  5. +12 −29 src/Nacl.elm
  6. +0 −49 src/Native/Nacl.js
View
@@ -8,7 +8,6 @@
"test"
],
"exposed-modules": [],
"native-modules": true,
"dependencies": {
"elm-community/elm-test": "4.0.0 <= v < 5.0.0",
"elm-lang/core": "5.1.1 <= v < 6.0.0",
View
@@ -8,7 +8,7 @@
### Donation Config UI
- ports?
x ports?
### Ingame Mod
View
@@ -19,4 +19,15 @@ require(["donation_panel/donation", "donation_panel/menu", "admin"], function(Do
//console.log(dm)
setTimeout(app.ports.matchedModel.send, 0, dm)
})
nacl_factory.instantiate(function(nacl) {
app.ports.signMessage.subscribe(function(args) {
var msg = nacl.encode_utf8(args.body)
var signsk = nacl.from_hex(args.key)
var signed = nacl.crypto_sign(msg, signsk)
var response = Object.create(args)
response.body = nacl.to_hex(signed)
app.ports.signedMessage.send(response)
})
})
})
View
@@ -72,6 +72,7 @@ type Msg
| GotUpdate (Result String (List Donation))
| EmptyRequestComplete (Result Http.Error ())
| MatchedModel (Result String Donation)
| SignedMessage Nacl.SignArguments
| AdminViewMsg AVMsg
update : Msg -> Model -> (Model, Cmd Msg)
@@ -95,7 +96,7 @@ update msg model =
AdminViewMsg (SetKey signsk) ->
if (String.length signsk) == 128 then
( { model | signsk = signsk }
, Cmd.batch [ fetchGame, fetchDonations ]
, Cmd.batch [ fetchGame, fetchDonations]
)
else
(model, Cmd.none)
@@ -116,6 +117,9 @@ update msg model =
MatchedModel (Err msg) ->
let _ = Debug.log "match error" msg in
(model, Cmd.none)
SignedMessage response ->
let _ = Debug.log "signed message" response in
(model, sendSignedRequest response)
AdminViewMsg (DeleteRound round) ->
( removeRound round model
, sendDeleteRound model.signsk round
@@ -186,64 +190,66 @@ removeRound : String -> Model -> Model
removeRound round model =
{ model | rounds = List.filter (\r -> not (r.id == round)) model.rounds }
sendDeleteRound : String -> String -> Cmd Msg
sendDeleteRound key round =
sendSignedRequest : Nacl.SignArguments -> Cmd Msg
sendSignedRequest {method, url, id, body} =
Http.send EmptyRequestComplete <| Http.request
{ method = "DELETE"
{ method = method
, headers = []
, url = config.server ++ "games/" ++ round
, body = message key round round |> Http.jsonBody
, url = url
, body = message id body |> Http.jsonBody
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Nothing
, withCredentials = False
}
sendDeleteRound : String -> String -> Cmd Msg
sendDeleteRound key round =
Nacl.signMessage
{ key = key
, method = "DELETE"
, url = config.server ++ "games/" ++ round
, id = round
, body = round
}
sendDiscountLevel : String -> String -> Int -> Cmd Msg
sendDiscountLevel key round level =
Http.send EmptyRequestComplete <| Http.request
{ method = "PUT"
, headers = []
Nacl.signMessage
{ key = key
, method = "PUT"
, url = config.server ++ "games/" ++ round ++ "/discount_level"
, body = discountLevelBody round level |> message key round |> Http.jsonBody
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Nothing
, withCredentials = False
, id = round
, body = discountLevelBody round level
}
sendGameTime : String -> String -> Int -> Cmd Msg
sendGameTime key round time =
Http.send EmptyRequestComplete <| Http.request
{ method = "PUT"
, headers = []
Nacl.signMessage
{ key = key
, method = "PUT"
, url = config.server ++ "games/" ++ round ++ "/game_time"
, body = gameTimeBody round time |> message key round |> Http.jsonBody
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Nothing
, withCredentials = False
, id = round
, body = gameTimeBody round time
}
sendClearDonations : String -> Cmd Msg
sendClearDonations key =
Http.send EmptyRequestComplete <| Http.request
{ method = "DELETE"
, headers = []
Nacl.signMessage
{ key = key
, method = "DELETE"
, url = config.server ++ "donations"
, body = message key "donations" "clear" |> Http.jsonBody
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Nothing
, withCredentials = False
, id = "donations"
, body = "clear"
}
sendDonationEdit : String -> Donation -> Cmd Msg
sendDonationEdit key donation =
Http.send EmptyRequestComplete <| Http.request
{ method = "PUT"
, headers = []
Nacl.signMessage
{ key = key
, method = "PUT"
, url = config.server ++ "donations/" ++ (toString donation.id)
, body = donationEditBody donation |> message key (toString donation.id) |> Http.jsonBody
, expect = Http.expectStringResponse (\_ -> Ok ())
, timeout = Nothing
, withCredentials = False
, id = toString donation.id
, body = donationEditBody donation
}
updateDonation : (Donation -> Donation) -> Int -> Model -> Model
@@ -291,20 +297,11 @@ validNumber : String -> Bool
validNumber value =
Regex.contains (regex "^\\d+$") value
signedBody : String -> String -> String
signedBody key body =
let
msg = Nacl.encode_utf8 body
signsk = Nacl.from_hex key
signed = Nacl.crypto_sign msg signsk
in
Nacl.to_hex signed
message : String -> String -> String -> Json.Encode.Value
message key id body =
message : String -> String -> Json.Encode.Value
message id body =
Json.Encode.object
[ ("id", Json.Encode.string id)
, ("data", Json.Encode.string <| signedBody key body)
, ("data", Json.Encode.string body)
]
gameTimeBody : String -> Int -> String
@@ -345,6 +342,7 @@ subscriptions model =
[ WebSocket.listen (config.wsserver ++ "donations") receiveUpdate
, WebSocket.listen (config.wsserver ++ "options.json") receiveOptions
, matchSubscription model
, Nacl.signedMessage SignedMessage
]
receiveUpdate : String -> Msg
View
@@ -1,29 +1,12 @@
module Nacl exposing (..)
import Native.Nacl
type UInt8Array = UInt8Array
to_hex : UInt8Array -> String
to_hex =
Native.Nacl.to_hex
from_hex : String -> UInt8Array
from_hex =
Native.Nacl.from_hex
decode_utf8 : UInt8Array -> String
decode_utf8 =
Native.Nacl.decode_utf8
encode_utf8 : String -> UInt8Array
encode_utf8 =
Native.Nacl.encode_utf8
crypto_sign : UInt8Array -> UInt8Array -> UInt8Array
crypto_sign =
Native.Nacl.crypto_sign
crypto_sign_open : UInt8Array -> UInt8Array -> Maybe UInt8Array
crypto_sign_open =
Native.Nacl.crypto_sign_open
port module Nacl exposing (..)
type alias SignArguments =
{ method : String
, url : String
, id : String
, key : String
, body : String
}
port signMessage : SignArguments -> Cmd msg
port signedMessage : (SignArguments -> msg) -> Sub msg
View
@@ -1,49 +0,0 @@
var _user$project$Native_Nacl = function() {
var nacl
nacl_factory.instantiate(function(n) {nacl = n})
var uint8array = function(array) {
return {
ctor: '_UInt8Array',
value: array,
}
}
var to_hex = function(array) {
return nacl.to_hex(array.value)
}
var from_hex = function(string) {
return uint8array(nacl.from_hex(string))
}
var decode_utf8 = function(array) {
return nacl.decode_utf8(array.value)
}
var encode_utf8 = function(string) {
return uint8array(nacl.encode_utf8(string))
}
var crypto_sign = function(msgBin, signerSecretKey) {
return uint8array(nacl.crypto_sign(msgBin.value, signerSecretKey.value))
}
var crypto_sign_open = function(packetBin, signerPublicKey) {
var msg = nacl.crypto_sign_open(packetBin.value, signerPublicKey.value)
if (msg) {
_elm_lang$core$Maybe$Just(uint8array(msg))
} else {
_elm_lang$core$Maybe$Nothing
}
}
return {
to_hex: to_hex,
from_hex: from_hex,
encode_utf8: encode_utf8,
decode_utf8: decode_utf8,
crypto_sign: F2(crypto_sign),
crypto_sign_open: F2(crypto_sign_open),
};
}();

0 comments on commit 9027ada

Please sign in to comment.