Skip to content

Commit

Permalink
ethSig using interpolation
Browse files Browse the repository at this point in the history
... rather than stupid Rholang tricks
  • Loading branch information
dckc committed Dec 10, 2018
1 parent d09b079 commit bb08e15
Showing 1 changed file with 25 additions and 44 deletions.
69 changes: 25 additions & 44 deletions src/ethSig.rho
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ new trace(`rho:io:stderr`), rl(`rho:registry:lookup`),
rCh, bufCh, pubCh, sigCh, itemsCh,
sigDER, pkDER in
{
rl!(`rho:id:gbxhost7zscewrxsghqgc9nnwaju1h8xthemunp7661aum8z1nb6ye`, *rCh) |
rl!(`rho:id:epm7u46j9h7yzqsrx7kffyaxotcc1ma76z4om7ui8hiewdpjdotakh`, *rCh) |

trace!("ethereumjs-tx README test case") |

Expand All @@ -12,62 +12,43 @@ sigDER, pkDER in
pkDER!(
// > tx.getSenderPublicKey().toString('hex')
("6d9038945ff8f4669201ba1e806c9a46a5034a578e4d52c03152198538039294" ++
"4efd6c702504d9130573bb939f5c124af95d38168546cc7207a7e0baf14172ff"),
"4efd6c702504d9130573bb939f5c124af95d38168546cc7207a7e0baf14172ff").hexToBytes(),
*pubCh) |

sigDER!(
"00" ++ // TODO: prevent f2 from signaling negative in sigDER
// > tx.r.toString('hex')
"f2d54d3399c9bcd3ac3482a5ffaeddfe68e9a805375f626b4f2f8cf530c2d95a",
"f2d54d3399c9bcd3ac3482a5ffaeddfe68e9a805375f626b4f2f8cf530c2d95a".hexToBytes(),
// > tx.s.toString('hex')
"5b3bb54e6e8db52083a9b674e578c843a87c292f0383ddba168573808d36dc8e",
"5b3bb54e6e8db52083a9b674e578c843a87c292f0383ddba168573808d36dc8e".hexToBytes(),
*sigCh) |

// ASN1 Distinguished Encoding Rules (DER) for public key
// TODO: ++ on ByteArray
contract pkDER(@{pk /\ String}, return) = {
contract pkDER(@{pk /\ ByteArray}, return) = {
trace!({"pkDER": pk}) |
return!(("04" ++ // OctetString tagged
pk).hexToBytes())
return!("${OctetString}${pk}".toUtf8Bytes() %% { "OctetString": 4, "pk": pk })
}
|
contract sigDER(@{r /\ String}, @{s /\ String}, return) = {
// ASN1 Distinguished Encoding Rules (DER) for signature
// i.e. SEQUENCE { r INTEGER, S INTEGER }
contract sigDER(@{r /\ ByteArray}, @{s /\ ByteArray}, return) = {
trace!({"sigDERr": r, "s": s}) |
// stupid Rholang tricks...
new lenRSCh, lenRCh, lenSCh, hexDigit, hexByte in {
for(@lenRS <- lenRSCh; @lenR <- lenRCh; @lenS <- lenSCh) {
trace!({"lenRS": lenRS}) |
return!((
// DER Sequence (tag 16); 4 + lenR + lenS
"30" ++ lenRS ++
// DER Integer
"02" ++ lenR ++ r ++
"02" ++ lenS ++ s).hexToBytes())
new zpad, rpCh, spCh in {
zpad!(r, *rpCh) | zpad!(s, *spCh) |
for(@rp <- rpCh; @sp <- spCh) {
return!("${SEQUENCE}${lenRS}${INTEGER}${lenR}${r}${INTEGER}${lenS}${s}".toUtf8Bytes() %% {
"SEQUENCE": 48, // 0x20 + tag 0x10
"lenRS": 2 + 1 + 1 + r.length() + 1 + s.length(),
"INTEGER": 2,
"r": rp, "lenR": rp.length(),
"s": sp, "lenS": sp.length()
})
} |
hexByte!(4 + (r.length() + s.length()) / 2, *lenRSCh) |
hexByte!(r.length() / 2, *lenRCh) | hexByte!(s.length() / 2, *lenSCh) |

// singleton byte sequence
// TODO: add Rholang method to make ByteArray from list of int
// TODO: add ++ on ByteArray
contract hexByte(@{i /\ Integer}, return) = {
trace!({"hexByte": i}) |
new d0Ch, d1Ch in {
hexDigit!(i / 16, *d1Ch) |
hexDigit!(i - (i / 16 * 16), *d0Ch) |
for(@d0 <- d0Ch; @d1 <- d1Ch) {
return!(d1 ++ d0)
}
}
} |
contract hexDigit(@{i /\ Integer}, d) = {
trace!({"hexDigit": i}) |
match i {
0 => { d!("0") } 1 => { d!("1") } 2 => { d!("2") } 3 => { d!("3") }
4 => { d!("4") } 5 => { d!("5") } 6 => { d!("6") } 7 => { d!("7") }
8 => { d!("8") } 9 => { d!("9") } 10 => { d!("a") } 11 => { d!("b") }
12 => { d!("c") } 13 => { d!("d") } 14 => { d!("e") } 15 => { d!("f") }
_ => trace!({ "bad hex digit": i })
contract zpad(@{bn /\ ByteArray}, return) = {
// trace!({"zpad": bn.nth(0)}) |
if (bn.nth(0) >= 128) {
return!("00".hexToBytes() ++ bn)
} else {
return!(bn)
}
}
}
Expand Down

0 comments on commit bb08e15

Please sign in to comment.