-
Notifications
You must be signed in to change notification settings - Fork 34
ErgoAuthUtils added for EIP-28 #157
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b8af768
ErgoAuthUtils added for EIP-28
MrStahlfelge dd49dfd
ErgoAuthUtils added for EIP-28, fix Scala 2.11
MrStahlfelge efaa2fa
ergoauth: some cleanups
aslesarenko f7c9bee
Introduced SigmaProp appkit class and used where SigmaBoolean was use…
MrStahlfelge be587eb
ergoauth: introduced SigmaPropInterpreter + code cleanup
aslesarenko f1c4855
Address.getSigmaBoolean improved
MrStahlfelge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
appkit/src/test/scala/org/ergoplatform/appkit/ErgoAuthSpec.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package org.ergoplatform.appkit | ||
|
|
||
| import org.scalatest.{Matchers, PropSpec} | ||
| import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks | ||
| import scorex.util.Random | ||
| import sigmastate.interpreter.HintsBag | ||
|
|
||
| import java.nio.charset.StandardCharsets | ||
|
|
||
| class ErgoAuthSpec extends PropSpec with Matchers with ScalaCheckDrivenPropertyChecks | ||
| with AppkitTestingCommon { | ||
|
|
||
| property("ErgoAuth address roundtrip") { | ||
| // server side | ||
| val serializedSigmaBoolean = ErgoAuthUtils.serializeSigmaBoolean(address) | ||
| // message to sign should be something random and not repeating | ||
| val requestedMessage = addrStr + System.currentTimeMillis().toString | ||
|
|
||
| // transferred to client, and now we are on client side | ||
| val signedMessage = new String(Random.randomBytes(16)) + requestedMessage + | ||
| new String(Random.randomBytes(32)) | ||
|
aslesarenko marked this conversation as resolved.
|
||
| val signature = new ColdErgoClient(address.getNetworkType, Parameters.ColdClientMaxBlockCost) | ||
| .execute { ctx: BlockchainContext => | ||
|
|
||
| val prover = ctx.newProverBuilder().withMnemonic(mnemonic, SecretString.empty()).build() | ||
| prover.signMessage(ErgoAuthUtils.deserializeSigmaBoolean(serializedSigmaBoolean), | ||
| signedMessage.getBytes(StandardCharsets.UTF_8), | ||
| HintsBag.empty) | ||
| } | ||
|
|
||
| // transferred to server... | ||
| ErgoAuthUtils.verifyResponse(address, requestedMessage, signedMessage, signature) shouldBe (true) | ||
|
aslesarenko marked this conversation as resolved.
Outdated
|
||
|
|
||
| // and in case someone wanted to fool us | ||
| ErgoAuthUtils.verifyResponse(address, | ||
| requestedMessage, | ||
| signedMessage, | ||
| new Array[Byte](0)) shouldBe (false) | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
common/src/main/java/org/ergoplatform/appkit/ErgoAuthUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package org.ergoplatform.appkit; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| import sigmastate.Values.SigmaBoolean; | ||
|
|
||
| /** | ||
| * Helper utilities for EIP-28 ErgoAuth | ||
| */ | ||
| public class ErgoAuthUtils { | ||
|
|
||
| /** | ||
| * Serializes the given SigmaBoolean to be sent in ErgoAuthRequest. | ||
| */ | ||
| public static byte[] serializeSigmaBoolean(SigmaBoolean sigmaBoolean) { | ||
| return Iso.isoSigmaBooleanToByteArray().to(sigmaBoolean); | ||
| } | ||
|
|
||
| /** | ||
| * Converts given address to SigmaBoolean and serializes it to be sent in ErgoAuthRequest. | ||
| */ | ||
| public static byte[] serializeSigmaBoolean(Address address) { | ||
|
aslesarenko marked this conversation as resolved.
Outdated
|
||
| return serializeSigmaBoolean(address.getPublicKey()); | ||
| } | ||
|
|
||
| /** | ||
| * Deserializes a SigmaBoolean from byte array | ||
| */ | ||
| public static SigmaBoolean deserializeSigmaBoolean(byte[] serializedSigmaBoolean) { | ||
| return Iso.isoSigmaBooleanToByteArray().from(serializedSigmaBoolean); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies an ErgoAuthResponse | ||
| * | ||
| * @param sigmaBoolean the SigmaBoolean needed to be fulfilled for signing the message | ||
| * @param originalMessage the original message sent in ErgoAuthRequest, needs to be contained | ||
| * in signedMessage | ||
| * @param signedMessage the message signed by client | ||
| * @param signature signature for signedMessage | ||
| * @return whether verification is successful | ||
| */ | ||
| public static boolean verifyResponse(SigmaBoolean sigmaBoolean, String originalMessage, | ||
| String signedMessage, byte[] signature) { | ||
|
|
||
| if (!signedMessage.contains(originalMessage)) | ||
| return false; | ||
|
|
||
| return Signature.verifySignature(sigmaBoolean, | ||
| signedMessage.getBytes(StandardCharsets.UTF_8), | ||
| signature); | ||
| } | ||
|
|
||
| /** | ||
| * Verifies an ErgoAuthResponnse for a given address. | ||
| * See {@link #verifyResponse(SigmaBoolean, String, String, byte[])} for more information. | ||
| */ | ||
| public static boolean verifyResponse(Address address, String originalMessage, | ||
| String signedMessage, byte[] signature) { | ||
| return verifyResponse(address.getPublicKey(), originalMessage, signedMessage, signature); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 4 additions & 7 deletions
11
lib-impl/src/main/java/org/ergoplatform/appkit/impl/ColdBlockchainContext.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.