Remove event descriptor parameter from SigningVersion.calcOutcomeHash() (#4796)

This commit is contained in:
Chris Stewart 2022-09-23 06:17:38 -05:00 committed by GitHub
parent 9518826882
commit 4cb47c4ef4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 54 deletions

View file

@ -282,7 +282,7 @@ object OracleEvent {
enum.outcomes.exists { outcome => enum.outcomes.exists { outcome =>
val attestationType = EnumAttestation(outcome) val attestationType = EnumAttestation(outcome)
val hash = val hash =
signingVersion.calcOutcomeHash(enum, attestationType.bytes) signingVersion.calcOutcomeHash(attestationType.bytes)
announcement.publicKey.verify(hash, announcement.publicKey.verify(hash,
sig) && outcome == tlvOutcomes.head sig) && outcome == tlvOutcomes.head
} }
@ -299,7 +299,7 @@ object OracleEvent {
val validSign = signOutcomes.exists { attestationType => val validSign = signOutcomes.exists { attestationType =>
val hash = val hash =
signingVersion.calcOutcomeHash(dd, attestationType.bytes) signingVersion.calcOutcomeHash(attestationType.bytes)
announcement.publicKey.verify( announcement.publicKey.verify(
hash, hash,
attestations.head) && tlvOutcomes.head.toString == attestationType.outcomeString attestations.head) && tlvOutcomes.head.toString == attestationType.outcomeString
@ -319,7 +319,7 @@ object OracleEvent {
case (sig, outcome) => case (sig, outcome) =>
digitOutcomes.exists { attestationType => digitOutcomes.exists { attestationType =>
val hash = val hash =
signingVersion.calcOutcomeHash(dd, attestationType.bytes) signingVersion.calcOutcomeHash(attestationType.bytes)
announcement.publicKey.verify( announcement.publicKey.verify(
hash, hash,
sig) && attestationType.outcomeString == outcome.toString sig) && attestationType.outcomeString == outcome.toString

View file

@ -13,15 +13,11 @@ sealed abstract class SigningVersion {
def calcAnnouncementHash(eventTLV: OracleEventTLV): ByteVector def calcAnnouncementHash(eventTLV: OracleEventTLV): ByteVector
/** Calculates the bytes to sign for an event outcome */ /** Calculates the bytes to sign for an event outcome */
def calcOutcomeHash( def calcOutcomeHash(bytes: ByteVector): ByteVector
descriptor: EventDescriptorTLV,
bytes: ByteVector): ByteVector
/** Calculates the bytes to sign for an event outcome */ /** Calculates the bytes to sign for an event outcome */
final def calcOutcomeHash( final def calcOutcomeHash(string: String): ByteVector = {
descriptor: EventDescriptorTLV, calcOutcomeHash(CryptoUtil.serializeForHash(string))
string: String): ByteVector = {
calcOutcomeHash(descriptor, CryptoUtil.serializeForHash(string))
} }
} }
@ -41,9 +37,7 @@ object SigningVersion extends StringFactory[SigningVersion] {
override def calcAnnouncementHash(eventTLV: OracleEventTLV): ByteVector = override def calcAnnouncementHash(eventTLV: OracleEventTLV): ByteVector =
CryptoUtil.taggedSha256(eventTLV.bytes, "DLCv0/Announcement").bytes CryptoUtil.taggedSha256(eventTLV.bytes, "DLCv0/Announcement").bytes
override def calcOutcomeHash( override def calcOutcomeHash(byteVector: ByteVector): ByteVector =
descriptor: EventDescriptorTLV,
byteVector: ByteVector): ByteVector =
CryptoUtil.taggedSha256(byteVector, "DLCv0/Outcome").bytes CryptoUtil.taggedSha256(byteVector, "DLCv0/Outcome").bytes
} }
@ -61,14 +55,8 @@ object SigningVersion extends StringFactory[SigningVersion] {
override def calcAnnouncementHash(eventTLV: OracleEventTLV): ByteVector = override def calcAnnouncementHash(eventTLV: OracleEventTLV): ByteVector =
CryptoUtil.sha256(eventTLV.bytes).bytes CryptoUtil.sha256(eventTLV.bytes).bytes
override def calcOutcomeHash( override def calcOutcomeHash(byteVector: ByteVector): ByteVector = {
descriptor: EventDescriptorTLV, CryptoUtil.sha256(byteVector).bytes
byteVector: ByteVector): ByteVector = {
descriptor match {
case _: EnumEventDescriptorV0TLV |
_: DigitDecompositionEventDescriptorV0TLV =>
CryptoUtil.sha256(byteVector).bytes
}
} }
} }
@ -86,14 +74,8 @@ object SigningVersion extends StringFactory[SigningVersion] {
override def calcAnnouncementHash(eventTLV: OracleEventTLV): ByteVector = override def calcAnnouncementHash(eventTLV: OracleEventTLV): ByteVector =
CryptoUtil.sha256DLCAnnouncement(eventTLV.bytes).bytes CryptoUtil.sha256DLCAnnouncement(eventTLV.bytes).bytes
override def calcOutcomeHash( override def calcOutcomeHash(byteVector: ByteVector): ByteVector = {
descriptor: EventDescriptorTLV, CryptoUtil.sha256DLCAttestation(byteVector).bytes
byteVector: ByteVector): ByteVector = {
descriptor match {
case _: EnumEventDescriptorV0TLV |
_: DigitDecompositionEventDescriptorV0TLV =>
CryptoUtil.sha256DLCAttestation(byteVector).bytes
}
} }
} }

View file

@ -289,8 +289,7 @@ class DLCOracleTest extends DLCOracleFixture {
assert(completedEvent.attestation == sig.sig) assert(completedEvent.attestation == sig.sig)
assert(completedEvent.outcomes == Vector(EnumAttestation(outcome))) assert(completedEvent.outcomes == Vector(EnumAttestation(outcome)))
val descriptor = completedEvent.eventDescriptorTLV val hash = SigningVersion.latest.calcOutcomeHash(outcome)
val hash = SigningVersion.latest.calcOutcomeHash(descriptor, outcome)
assert(dlcOracle.publicKey.verify(hash, sig)) assert(dlcOracle.publicKey.verify(hash, sig))
assert( assert(
@ -334,10 +333,8 @@ class DLCOracleTest extends DLCOracleFixture {
DigitDecompositionAttestation(1)) DigitDecompositionAttestation(1))
assert(completedEvent.outcomes == signOutcome +: digitOutcomes) assert(completedEvent.outcomes == signOutcome +: digitOutcomes)
val descriptor = completedEvent.eventDescriptorTLV
// Sign Signature Check // Sign Signature Check
val signHash = SigningVersion.latest.calcOutcomeHash(descriptor, "-") val signHash = SigningVersion.latest.calcOutcomeHash("-")
val signSig = completedEvent.signatures.head val signSig = completedEvent.signatures.head
assert(dlcOracle.publicKey.verify(signHash, signSig)) assert(dlcOracle.publicKey.verify(signHash, signSig))
assert( assert(
@ -348,7 +345,6 @@ class DLCOracleTest extends DLCOracleFixture {
// 100s Place signature Check // 100s Place signature Check
val hash100 = val hash100 =
SigningVersion.latest.calcOutcomeHash( SigningVersion.latest.calcOutcomeHash(
descriptor,
DigitDecompositionAttestation(3).bytes) DigitDecompositionAttestation(3).bytes)
val sig100 = completedEvent.signatures(1) val sig100 = completedEvent.signatures(1)
assert(dlcOracle.publicKey.verify(hash100, sig100)) assert(dlcOracle.publicKey.verify(hash100, sig100))
@ -359,7 +355,6 @@ class DLCOracleTest extends DLCOracleFixture {
// 10s Place signature Check // 10s Place signature Check
val hash10 = val hash10 =
SigningVersion.latest.calcOutcomeHash( SigningVersion.latest.calcOutcomeHash(
descriptor,
DigitDecompositionAttestation(2).bytes) DigitDecompositionAttestation(2).bytes)
val sig10 = completedEvent.signatures(2) val sig10 = completedEvent.signatures(2)
assert(dlcOracle.publicKey.verify(hash10, sig10)) assert(dlcOracle.publicKey.verify(hash10, sig10))
@ -370,7 +365,6 @@ class DLCOracleTest extends DLCOracleFixture {
// 1s Place signature Check // 1s Place signature Check
val hash1 = val hash1 =
SigningVersion.latest.calcOutcomeHash( SigningVersion.latest.calcOutcomeHash(
descriptor,
DigitDecompositionAttestation(1).bytes) DigitDecompositionAttestation(1).bytes)
val sig1 = completedEvent.signatures(3) val sig1 = completedEvent.signatures(3)
assert(dlcOracle.publicKey.verify(hash1, sig1)) assert(dlcOracle.publicKey.verify(hash1, sig1))
@ -412,11 +406,9 @@ class DLCOracleTest extends DLCOracleFixture {
DigitDecompositionAttestation(11)) DigitDecompositionAttestation(11))
assert(completedEvent.outcomes == signOutcome +: digitOutcomes) assert(completedEvent.outcomes == signOutcome +: digitOutcomes)
val descriptor = completedEvent.eventDescriptorTLV
// Sign Signature Check // Sign Signature Check
val signHash = val signHash =
SigningVersion.latest.calcOutcomeHash(descriptor, "-") SigningVersion.latest.calcOutcomeHash("-")
val signSig = completedEvent.signatures.head val signSig = completedEvent.signatures.head
assert(dlcOracle.publicKey.verify(signHash, signSig)) assert(dlcOracle.publicKey.verify(signHash, signSig))
assert( assert(
@ -427,7 +419,6 @@ class DLCOracleTest extends DLCOracleFixture {
// 100s Place signature Check // 100s Place signature Check
val hash100 = val hash100 =
SigningVersion.latest.calcOutcomeHash( SigningVersion.latest.calcOutcomeHash(
descriptor,
DigitDecompositionAttestation(7).bytes) DigitDecompositionAttestation(7).bytes)
val sig100 = completedEvent.signatures(1) val sig100 = completedEvent.signatures(1)
assert(dlcOracle.publicKey.verify(hash100, sig100)) assert(dlcOracle.publicKey.verify(hash100, sig100))
@ -438,7 +429,6 @@ class DLCOracleTest extends DLCOracleFixture {
// 10s Place signature Check // 10s Place signature Check
val hash10 = val hash10 =
SigningVersion.latest.calcOutcomeHash( SigningVersion.latest.calcOutcomeHash(
descriptor,
DigitDecompositionAttestation(8).bytes) DigitDecompositionAttestation(8).bytes)
val sig10 = completedEvent.signatures(2) val sig10 = completedEvent.signatures(2)
assert(dlcOracle.publicKey.verify(hash10, sig10)) assert(dlcOracle.publicKey.verify(hash10, sig10))
@ -449,7 +439,6 @@ class DLCOracleTest extends DLCOracleFixture {
// 1s Place signature Check // 1s Place signature Check
val hash1 = val hash1 =
SigningVersion.latest.calcOutcomeHash( SigningVersion.latest.calcOutcomeHash(
descriptor,
DigitDecompositionAttestation(11).bytes) DigitDecompositionAttestation(11).bytes)
val sig1 = completedEvent.signatures(3) val sig1 = completedEvent.signatures(3)
assert(dlcOracle.publicKey.verify(hash1, sig1)) assert(dlcOracle.publicKey.verify(hash1, sig1))
@ -490,12 +479,9 @@ class DLCOracleTest extends DLCOracleFixture {
DigitDecompositionAttestation(0)) DigitDecompositionAttestation(0))
assert(completedEvent.outcomes == digitOutcomes) assert(completedEvent.outcomes == digitOutcomes)
val descriptor = completedEvent.eventDescriptorTLV
// 100s Place signature Check // 100s Place signature Check
val hash100 = val hash100 =
SigningVersion.latest.calcOutcomeHash( SigningVersion.latest.calcOutcomeHash(
descriptor,
DigitDecompositionAttestation(0).bytes) DigitDecompositionAttestation(0).bytes)
val sig100 = completedEvent.signatures.head val sig100 = completedEvent.signatures.head
assert(dlcOracle.publicKey.verify(hash100, sig100)) assert(dlcOracle.publicKey.verify(hash100, sig100))
@ -507,7 +493,6 @@ class DLCOracleTest extends DLCOracleFixture {
// 10s Place signature Check // 10s Place signature Check
val hash10 = val hash10 =
SigningVersion.latest.calcOutcomeHash( SigningVersion.latest.calcOutcomeHash(
descriptor,
DigitDecompositionAttestation(1).bytes) DigitDecompositionAttestation(1).bytes)
val sig10 = completedEvent.signatures(1) val sig10 = completedEvent.signatures(1)
assert(dlcOracle.publicKey.verify(hash10, sig10)) assert(dlcOracle.publicKey.verify(hash10, sig10))
@ -518,7 +503,6 @@ class DLCOracleTest extends DLCOracleFixture {
// 1s Place signature Check // 1s Place signature Check
val hash1 = val hash1 =
SigningVersion.latest.calcOutcomeHash( SigningVersion.latest.calcOutcomeHash(
descriptor,
DigitDecompositionAttestation(0).bytes) DigitDecompositionAttestation(0).bytes)
val sig1 = completedEvent.signatures(2) val sig1 = completedEvent.signatures(2)
assert(dlcOracle.publicKey.verify(hash1, sig1)) assert(dlcOracle.publicKey.verify(hash1, sig1))

View file

@ -334,8 +334,8 @@ case class DLCOracle()(implicit val conf: DLCOracleAppConfig)
s"No event saved with nonce ${nonce.hex} $outcome")) s"No event saved with nonce ${nonce.hex} $outcome"))
} }
hash = eventDb.signingVersion.calcOutcomeHash(eventDb.eventDescriptorTLV, hash = eventDb.signingVersion
outcome.outcomeString) .calcOutcomeHash(outcome.outcomeString)
eventOutcomeOpt <- eventOutcomeDAO.find(nonce, hash) eventOutcomeOpt <- eventOutcomeDAO.find(nonce, hash)
eventOutcomeDb <- eventOutcomeOpt match { eventOutcomeDb <- eventOutcomeOpt match {
case Some(value) => Future.successful(value) case Some(value) => Future.successful(value)

View file

@ -22,14 +22,14 @@ trait EventDbUtil {
enum.outcomes.map { outcome => enum.outcomes.map { outcome =>
val attestationType = EnumAttestation(outcome) val attestationType = EnumAttestation(outcome)
val hash = val hash =
signingVersion.calcOutcomeHash(enum, attestationType.bytes) signingVersion.calcOutcomeHash(attestationType.bytes)
EventOutcomeDb(nonce, outcome, hash) EventOutcomeDb(nonce, outcome, hash)
} }
case decomp: DigitDecompositionEventDescriptorV0TLV => case decomp: DigitDecompositionEventDescriptorV0TLV =>
val signDbs = decomp match { val signDbs = decomp match {
case _: SignedDigitDecompositionEventDescriptor => case _: SignedDigitDecompositionEventDescriptor =>
val plusHash = signingVersion.calcOutcomeHash(decomp, "+") val plusHash = signingVersion.calcOutcomeHash("+")
val minusHash = signingVersion.calcOutcomeHash(decomp, "-") val minusHash = signingVersion.calcOutcomeHash("-")
Vector(EventOutcomeDb(nonces.head, "+", plusHash), Vector(EventOutcomeDb(nonces.head, "+", plusHash),
EventOutcomeDb(nonces.head, "-", minusHash)) EventOutcomeDb(nonces.head, "-", minusHash))
case _: UnsignedDigitDecompositionEventDescriptor => case _: UnsignedDigitDecompositionEventDescriptor =>
@ -47,7 +47,7 @@ trait EventDbUtil {
0.until(decomp.base.toInt).map { num => 0.until(decomp.base.toInt).map { num =>
val attestationType = DigitDecompositionAttestation(num) val attestationType = DigitDecompositionAttestation(num)
val hash = val hash =
signingVersion.calcOutcomeHash(decomp, attestationType.bytes) signingVersion.calcOutcomeHash(attestationType.bytes)
EventOutcomeDb(nonce, num.toString, hash) EventOutcomeDb(nonce, num.toString, hash)
} }
} }