-
Notifications
You must be signed in to change notification settings - Fork 30
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 all 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
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
| 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 round trip") { | ||
| val sigmaPropFromAddress = SigmaProp.createFromAddress(address) | ||
| // ---- server side ---- | ||
| val serializedSigmaBoolean = sigmaPropFromAddress.toBytes | ||
| // 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 ---- | ||
|
|
||
| // EIP-28: "the wallet app adds some own bytes to the obtained message from ErgoAuthRequest" | ||
| val signedMessage = new String(Random.randomBytes(16)) + requestedMessage + | ||
| new String(Random.randomBytes(32)) | ||
| val signature = new ColdErgoClient(address.getNetworkType, Parameters.ColdClientMaxBlockCost) | ||
| .execute { ctx: BlockchainContext => | ||
|
|
||
| val prover = ctx.newProverBuilder().withMnemonic(mnemonic, SecretString.empty()).build() | ||
| prover.signMessage(SigmaProp.parseFromBytes(serializedSigmaBoolean), | ||
| signedMessage.getBytes(StandardCharsets.UTF_8), | ||
| HintsBag.empty) | ||
| } | ||
|
|
||
| // ---- transferred to server... ---- | ||
| ErgoAuthUtils.verifyResponse(sigmaPropFromAddress, | ||
| requestedMessage, signedMessage, signature) shouldBe true | ||
|
|
||
| // and in case someone wanted to fool us | ||
| ErgoAuthUtils.verifyResponse(sigmaPropFromAddress, | ||
| 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
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
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
| package org.ergoplatform.appkit; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
|
|
||
| /** | ||
| * Helper utilities for EIP-28 ErgoAuth | ||
| */ | ||
| public class ErgoAuthUtils { | ||
|
|
||
| /** | ||
| * Verifies an ErgoAuthResponse | ||
| * | ||
| * @param sigmaProp the Sigma proposition 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(SigmaProp sigmaProp, String originalMessage, | ||
| String signedMessage, byte[] signature) { | ||
|
|
||
| if (!signedMessage.contains(originalMessage)) | ||
| return false; | ||
|
|
||
| return Signature.verifySignature(sigmaProp, | ||
| signedMessage.getBytes(StandardCharsets.UTF_8), | ||
| 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
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
39 changes: 39 additions & 0 deletions
39
common/src/main/java/org/ergoplatform/appkit/SigmaProp.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,39 @@ | ||
| package org.ergoplatform.appkit; | ||
|
|
||
| import sigmastate.Values; | ||
|
|
||
| /** | ||
| * Proposition which can be proven and verified by sigma protocol. | ||
| */ | ||
| public class SigmaProp { | ||
| private final Values.SigmaBoolean sigmaBoolean; | ||
|
|
||
| public SigmaProp(Values.SigmaBoolean sigmaBoolean) { | ||
| this.sigmaBoolean = sigmaBoolean; | ||
| } | ||
|
|
||
| public Values.SigmaBoolean getSigmaBoolean() { | ||
| return sigmaBoolean; | ||
| } | ||
|
|
||
| /** | ||
| * Serializes this SigmaProp. | ||
| */ | ||
| public byte[] toBytes() { | ||
| return Iso.isoSigmaBooleanToByteArray().to(sigmaBoolean); | ||
| } | ||
|
|
||
| /** | ||
| * @return SigmaProp equal to the one that was serialized with {@link #toBytes()} | ||
| */ | ||
| public static SigmaProp parseFromBytes(byte[] serializedBytes) { | ||
| return new SigmaProp(Iso.isoSigmaBooleanToByteArray().from(serializedBytes)); | ||
| } | ||
|
|
||
| /** | ||
| * @return SigmaProp from Address. Note that only SigmaBoolean addresses can be used | ||
| */ | ||
| public static SigmaProp createFromAddress(Address address) { | ||
| return new SigmaProp(address.getSigmaBoolean()); | ||
| } | ||
| } |
15 changes: 15 additions & 0 deletions
15
common/src/main/java/org/ergoplatform/appkit/SigmaPropInterpreter.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,15 @@ | ||
| package org.ergoplatform.appkit | ||
|
|
||
| import sigmastate.eval.IRContext | ||
| import sigmastate.interpreter.Interpreter.error | ||
| import sigmastate.interpreter.{PrecompiledScriptProcessor, Interpreter, InterpreterContext} | ||
|
|
||
| /** Simple light-weight interpreter that don't require IRContext and hence cannot perform | ||
| * script reduction, but it can however verify sigma-protocol propositions [[SigmaProp]]. | ||
| */ | ||
| object SigmaPropInterpreter extends Interpreter { | ||
| override type CTX = InterpreterContext | ||
| override val IR: IRContext = null | ||
| override def precompiledScriptProcessor: PrecompiledScriptProcessor = | ||
| error("Script reduction is not supported") | ||
| } |
Oops, something went wrong.
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.