Skip to content

Commit

Permalink
cmd: add blinded path helper commands
Browse files Browse the repository at this point in the history
Add a blinded-key option to the parse command so that it can be used to
test parsing of an onion for hops in a blinded route. Also add a helper
nextephemeral command.
  • Loading branch information
ellemouton committed Aug 17, 2023
1 parent a800c9f commit f971c75
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 10 deletions.
6 changes: 6 additions & 0 deletions cmd/example-data/onion-blinded.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"session_key": "4343434343434343434343434343434343434343434343434343434343434343",
"associated_data": "4242424242424242424242424242424242424242424242424242424242424242",
"blinding_point": "034e09f450a80c3d252b258aba0a61215bf60dda3b0dc78ffb0736ea1259dfd8a0",
"onion": "000288b48876fb0dc0d7375233ccaf2910dc0dc81ba52e5a7906f00d75e0d58dbd4bb7c2714870529410735f0951e72cbe981e2e167c0d8f3de33a36e39e78465aea2acad1e23c78b6fd342d63e37d214c912b4a0be344618f779138edc1b42a5ca3218ca2fea4be427f6cd0d387160db2bf6c2ba8e82941c8cf3626bd6bed7187f633012ef49df38f6b12963cb639e9eed1b9d269dcebcbd0b25287aa536ec85e7320b02e193122199a745ccbaaebd37f5d4b71f52f9b50feeb793eeef56924a046bc5e7003f6253e0284a8d3fe2e42c3564050f1e753cd32cc258ac0ffa6e05eecad5ba1286f78252e60dd884a65405ab673a85ba52adfa65c1086d4bb37ba2e0848adb2b04379775ad798492b14e8997f30ffa9cf5d432bdf5b246fce008fd876399beed827db58195f4f6192f6ff4ec63cb17fdcb497cb7aec26846a71dd8dca02fc3bb14dd7231a4d62a981bec54b71eb20331096dfa214a0ff4489ee96db663826ae8c850e9f06baa52a47b8eb576363f97e742aab2dc616acc6e74588e1d2ac16694febc90abaf5b1c684163c0e615a68d32633f01934adc8c6bf91fa3fd7aad033b7596d60402494e45e2c1632c40f7bfbd88a81a896a1d28ed6338c83e1eeaa467945d59998eb456c95f94bf1892e8f326ec2d5e0196b7073f106febc6ab8ca5bcc23f77ffc819bc1b5debce418ccc7d8391bbf33bceee6110beba170121bd99f54c956e64970bdab31227b03ee0ea3f01fbd9bd74015f6f82d04fab072e8f85f4370d09f41ee3e48eb959767bd989abb4eea42c4daa0437a7f747d7f9b70eb87b9f9b0b6f283b8205912601a432999b8869fd9fe5bad3572edac24da7184f9298f21ff60923db277264d29c846dd2f228f6fc53b6b60364237de64773f803f174ed10229c374f603ccc5fd3a62cb413ffe6f5630dc646bb33f231b2350537ec39e5d3f2fe1a1cb019ed0b18ad14019cad27afcca8ad70387ca110394c0432774f1aa1fa404b2e086c84a55388d3bd102501c78ef925cce89d76fa04c3f20f2d1f0ce507ac8b37b7913e3949ba12bbc5a4f6bac37c2415622d365bc8b83709a28e3d46f3850c89a3ff4d027fef6e3e4ce5c6c85f663c7eaec3c9730106fb82f53249a905533cfabee812aae51965b24b42f7ab471967bc8e73354e69141ee26a1f03684d5fb9c256a34de8257210e0390dd3962db521ae0a3bdab28300610ab2a634b699e5f092da5a061609ef6414bd805c8171f54ad6f285fb64ce0becca0b61188badcf8ef21190dad629e3fb3e89f55ebba829919540ebf5f8ae4283836d3c9133c1ca3365f6b9394916730411650686e0c2ab9c53b6cda9efdd5cfcb53ba9b6962bb6aa49d0a83a87460b60a9c7d2643ee99afe652883795f14014ec5df61b1e30c041c1fa6487f3c82f1ded5f83ffbef5017e197b7fb77be3b36e284a15e57d45bf9316dcaf97eb78ee4642b731ba05c5063bce1333fab4af6da97c80a96ee599b4df823efbedc250c0abba9783da7ddf2414b2a4774ff2880a7dc6791103e18b8631e39743cf9e87aed71700daa5dc72fdae520324741f92ea3d510ff555dea5e45f15cda87272d4559a12d4777680acb06993840e3c748da82c16cae556015fb2acd0335da11a3388575394048ab71199793ab706abc9d68add2075d79a5cc0f779845ee8b98951be61fd293d6c15b9d4653935bf17cf50bd31f8b79e60dba0e7fd6864754fd94262485a4f65e7eb3e1922f51b1a4dd2b4fd2c20d94d1213fbe90bd603dfc7e15176382e3ce0f43f980d44d23bf3c57f54a15f42c171a8f2511e28ac178c6f01396e50397a57ffb09c5e6c315bd3ae7983577c1a0386c6d5d9a2223438e321b0fedfdee58fa452d57dc11a256834bb49ac9deeec88e4bf563c7340f44a240caec941c7e50f09cf"
}
6 changes: 6 additions & 0 deletions cmd/example-data/onion-blinded2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"session_key": "4444444444444444444444444444444444444444444444444444444444444444",
"associated_data": "4242424242424242424242424242424242424242424242424242424242424242",
"blinding_point": "031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f",
"onion": "0003f25471c0f2ff549a7fd7859100306bb6c294c209f34c77f782897f184b967c498efc246bdb8e060a6d1cf8dd0d4d732e33311fb96c9e9f1274005fa3d08b41704a1b7224c6300a7caead7baa0a8263eba2e0de6956ee8e4a1958264f47e4cf20d194eb576f5bd249ee4fece563f80fd76dc3eaca8f956188406d83195752b5c90c4b2a5e7ac3a8d5c62b17b551aff48ef6842a7e9326832c9a4a2fd415011150a9e71beb901fd9747bac8add1c694b612730dc86b5b19a0bbbc675947a953316e3303d7b30c182f94def9206671edac9a3ec3e52d28fc28247a1c73ab751bf61c82c3950f617e758f79bd0ba294defb20466eaf1e801462046baad3aec3e5b8868a7b037f23d73a47a7e74c77107334f37388cff863e452820c61d89728fa75c84bc7cdfc06dcdd1911f5f803353926d073efd65251380e174913aae03318ea5b6f0ec83998c55ab99bef62803ea2da9f6d1ea892b90efc4f8ffb685a5201a781da2e6ac5923645638c9709ae32171a00c0cd3d8c7eedfb06b4eedc7d3e566987e2e3805a038f21d78ded5d6c7137a5e8e592f3180ee4d5f4e1289176f67fc38690d0958bc82e240b72b10577f340f1e14b8633f0b6d9729ff4618be2a972400a015a871ba33be70335f652a8d70f2bd32421d6ac2af781d667dad787d6aef4505a15d046579e46eebe757444cffca6d0610f0dd36a7ce57af969bd0c3f7006298ef406a25f689daf58f875d44d2423ebf195b503f11c37c506ea6abe50a463f7bb5e9b964604d832724de768513f6b38bf71715e1feea8a6e86797788d487146891564919da1372016ed8f08c7fcbff66a4a65a3d0fcd8e3daac6eba41f5d65ef2d8075364a9e78b3a273549f6eac4abb72e912e237990367e0d43e89945f8ac3907be5a6c662139485a50cb5ce3f0ba08586c39f6c515368ec3f91b72295f1b7a73a9df322ae9a45d363d6c616be3300083764cbdee31221f25a318f095feacb09f957c96db30fccca47a0215b576c3ed925a0bad05d6400abe318c11f36628c387a4ee38832182cd44b3cd48e5422c1f1e3b57218dfe72c611f5415127720e60f6e2400607e61841b76de1704bcbeb0daf1377ccb2253916de2b6d490bb71ba0a44fea2e94f2423d723934557d5905e01b2b80232a884e258d46dc92ea11e0818d0ece5b914f02049866e151801ab8c9aea155479b354dc91151fb9ba43277458f9760dd859faaa139e3b9ab36a1dbc36a93ef2c90598b20cb30ef3c4f23a2d6178b4d1da668fb328a25d84d30a132d9f2a6a988cbe2e5c2be01cb6db4b4725a50d6cdacf5fb083e7d650a25bec1407fbc047d26076c7596429a29606ad527e97ef0824ad6c1b05831a3e5b71c63a528918a3301cdd4061fc1fcce3da601961f2602a2b002ac8404125c2d52666263858a923e197efcda873c32d86897352e4f2264ad6a1b48acc0fe78ff55cb442cb2bb5fa2880810e1d00aa0247057fb80b7ed36cf9647af41b44ee4a63ee2d6f652526404572520a7d2d9dcde4e62df0c3be89f8471550594cdd16a51a9cacc58729c092c68506162fe65edc2314055d389f724ced189d826a546b5c4d08a43d977b3cf033de5760b71a7cc38ee5851592031aafb467a89b3b6c7ed67b15d44c48d6baedce3e95e08ec7c55038f3eba90ccb900895734f0fb7efe54961ce493369cc56416898a9bed7c2482871c15a7f1eb5ed17c33657fc31333539c2dfb59461af09e7049228113b5c9feea5a6e9959c18c51b19c90995afb9c76f2c0c820964cd7989c993a73925818a656c6a18dcd1a1e3782b2eae06dd5a41250ec2d1c203626ab9920c1673339eff04b1eb0cab85ef5909f571f9b83cdf21697c9f5cfa1c76e7bca955510e2126b3bb989a4ac21cf948f965e48bc363d2997437797b4f770e8b65"
}
6 changes: 6 additions & 0 deletions cmd/example-data/onion-blinded3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"session_key": "4545454545454545454545454545454545454545454545454545454545454545",
"associated_data": "4242424242424242424242424242424242424242424242424242424242424242",
"blinding_point": "03e09038ee76e50f444b19abf0a555e8697e035f62937168b80adf0931b31ce52a",
"onion": "0002ef43c4dfe9aee14248c445406ac7980474ce106c504d9025f57963739130adfd06eb26201baee8866af2d1b7a7ab26595349dad002af0590193aaa8f400ab394f5994ec831aeeecb64421c566e3556cbdd7e7e50deb1fc49fd5e007308ab6494415514abff978899623f9b6065ca1e243bb78170118e8b8c8b53b750b59cc1ec017d167adbb3aabab7c2d84fbf94f5d827239f4c2b9d2c3cfe68fe5641f25e386202a4b6edff2a71e700229df7230c8ca31bd5588f04799e9640c9c20a47cba713f3cc5ad3202e14bb520880f2a8409d8e7835cae21b48a651c2d47fe6af785889ab98f1416f6e4ad67a66ae681e9a8828bad3f9b6890221c4a7ec80531d6b63eb30843f613ce644795bc8bcee60e8f7b36f3fd04de762f103c52efaf36a2f3bbbaac482d6271dc4180c10bcc076c04d06ea7fd8fb6a647e0e10523b05da2d89e4139fb55c2315cd01bdcbd57587fef8442d7ff5620630fd2d2e79739d90be811bf2cba60415d6cba2cea14ba1859f3122cd905c4e12e3e2a1ab6fab54b2ec40e434626e2d3c3195c02c82a8bd64d226c2328ac72ca12197d9908eaf54333717448ce6ed73adc0ac05e2ee1d735131d87918beb8995993dc8f63fe10f2c8eba2be7ab8bb44d9f78f59ef3e4c180bd75e4eef2381450c6f0480d543997305f1d07815993b5aca8d88d474966d9abec93bb069a16aa2da75b87f94576e01d08a17d3e0e3d0370f010733a7d7affb12cdf94c259a62607fce71003535c4727305de5ff7bba3840922844b3a45f62c29715fccf440517ef121450f6962396fba9b07036d085582405dcae6ee95964b66bc7c85b8d02d90091500db3cebf6de584f86b7b55335a8c9aa26381b00747f055cc458a2cadfccf9c29702bf941447beaca6583cca09492a57d4b03b2ca00dbaf41dfd6a9b249381626a7debe475735a7e39e77a363eccf14669046f656cc09ad448da8d8b545e6a604f46dc481786d09a94c63cf23f49ba367d2929466364dbce2a8ffce3dadf8f4cef8a56e1fefa1a3304a953fe83018e57d8a95694b02d994fea2630a9a3d5f1e2f6d6142d503ec4152871f7122d7e566a03261f554639e7a759e0e73846f71d5cace37d91336fc9ca9396bf64ca2cf45fa2db779b3b5c63b04f1c0c1fb79fdfcf5a82b0202df934ae1720a7ce1e047cbec3f82737b50168c974f4623cacce87e3f5bd5232caca7956d28ffedcf11ac5998662c5f6b13c6126584ca2e894d3fcbad4d130bbe22e88a135e0020cdd43853e0b3af3800e9544854d211e873cf68ab683578d501d69ec5dc7fce42ac436d58243880c1b88227b0681c6c9dd8a8ad0793202b15ab63b787b748e258da3e68d0e649fc4ac081a71de8adbc891c113d5f722686b6ac4ed9e3cc247bc4a4643416f480627e9de20f7307f434a499f5c6951c2e8b3ff51d455bf65ceb5ee3dee47b968ac2642e13d8a68f903b73627c2e75788fecca5836371a908eea4f1ea44db2315bc185f77e478efeaaa4da2da13fe7aeaa79ed1d04876a8b2b7b333c5de8c4c9a50274c2eb7b9bd2a3630c57173174781fc9785235f830cefa1c82080eaffdef257f18eedc9ddfd25a696a11a3dc56cd836be72f5f4a2cbb6316d5d3b1ad91a7ec7d877f28d2c29a5525b0b24362699281b0e3b48f38caf1085045fe9089f9e6fb29e4b47aa4cecf68c9bf72073469bd9beeea5e88bfe554cb6a81231149ba7fe7784c154fd8b0f9179ecdf1e9fd5c2939ec1ab16df9cbe9359101ebce933d4f65d3f66f87afaecfe9c046b52f4878b6c430329df7bd879fba8864fcbd9b782bf545734699b9b5a66b466dcedc0c9368803b5b0f1232950cef398ad3e057a5db964bd3e5c8a5717b30b41601a4f11ad63afe404cb6f1e8ea5fd7a8e085b65ca5136146febf4d47928dcc9a9e0"
}
88 changes: 78 additions & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ func main() {
},
},
},
{
Name: "nextephemeral",
Usage: "A helper to compute the next ephemeral key " +
"given the current ephemeral key and a " +
"private key",
Action: nextEphemeral,
Flags: []cli.Flag{
cli.StringFlag{
Name: "priv",
Required: true,
},
cli.StringFlag{
Name: "pub",
Required: true,
},
},
},
{
Name: "generate",
Usage: "Build a new onion.",
Expand Down Expand Up @@ -207,44 +224,58 @@ func generate(ctx *cli.Context) error {
type onionInfo struct {
SessionKey string `json:"session_key"`
AssociatedData string `json:"associated_data"`
BlindingPoint string `json:"blinding_point"`
Onion string `json:"onion"`
}

func parseOnionInfo(info *onionInfo) (*sphinx.OnionPacket, *btcec.PrivateKey,
[]byte, error) {
[]byte, *btcec.PublicKey, error) {

sessionKeyBytes, err := hex.DecodeString(info.SessionKey)
if err != nil {
return nil, nil, nil, fmt.Errorf("unable to decode the "+
return nil, nil, nil, nil, fmt.Errorf("unable to decode the "+
"sessionKey %v: %v", info.SessionKey, err)
}

if len(sessionKeyBytes) != 32 {
return nil, nil, nil, fmt.Errorf("session priv key must be " +
"32 bytes long")
return nil, nil, nil, nil, fmt.Errorf("session priv key must " +
"be 32 bytes long")
}

sessionKey, _ := btcec.PrivKeyFromBytes(sessionKeyBytes)

assocData, err := hex.DecodeString(info.AssociatedData)
if err != nil {
return nil, nil, nil, fmt.Errorf("unable to decode the "+
return nil, nil, nil, nil, fmt.Errorf("unable to decode the "+
"associate data %v: %v", info.AssociatedData, err)
}

onion, err := hex.DecodeString(info.Onion)
if err != nil {
return nil, nil, nil, fmt.Errorf("unable to decode the "+
return nil, nil, nil, nil, fmt.Errorf("unable to decode the "+
"onion %v: %v", info.Onion, err)
}

var packet sphinx.OnionPacket
err = packet.Decode(bytes.NewBuffer(onion))
if err != nil {
return nil, nil, nil, err
return nil, nil, nil, nil, err
}

return &packet, sessionKey, assocData, nil
var blindingPoint *btcec.PublicKey
if info.BlindingPoint != "" {
bpBytes, err := hex.DecodeString(info.BlindingPoint)
if err != nil {
return nil, nil, nil, nil, err
}

blindingPoint, err = btcec.ParsePubKey(bpBytes)
if err != nil {
return nil, nil, nil, nil, err
}
}

return &packet, sessionKey, assocData, blindingPoint, nil
}

func peel(ctx *cli.Context) error {
Expand All @@ -260,7 +291,9 @@ func peel(ctx *cli.Context) error {
return err
}

packet, sessionKey, assocData, err := parseOnionInfo(&info)
packet, sessionKey, assocData, blindingPoint, err := parseOnionInfo(
&info,
)
if err != nil {
return err
}
Expand All @@ -272,7 +305,9 @@ func peel(ctx *cli.Context) error {
s.Start()
defer s.Stop()

p, err := s.ProcessOnionPacket(packet, assocData, 10)
p, err := s.ProcessOnionPacket(
packet, assocData, 10, sphinx.WithBlindingPoint(blindingPoint),
)
if err != nil {
return err
}
Expand All @@ -286,3 +321,36 @@ func peel(ctx *cli.Context) error {

return nil
}

func nextEphemeral(ctx *cli.Context) error {
privKeyByte, err := hex.DecodeString(ctx.String("priv"))
if err != nil {
return err
}
if len(privKeyByte) != 32 {
return fmt.Errorf("private key must be 32 bytes")
}

privKey, _ := btcec.PrivKeyFromBytes(privKeyByte)

pubKeyBytes, err := hex.DecodeString(ctx.String("pub"))
if err != nil {
return err
}

pubKey, err := btcec.ParsePubKey(pubKeyBytes)
if err != nil {
return err
}

nextBlindedKey, err := sphinx.NextEphemeral(
&sphinx.PrivKeyECDH{PrivKey: privKey}, pubKey,
)
if err != nil {
return err
}

fmt.Printf("%x\n", nextBlindedKey.SerializeCompressed())

return nil
}

0 comments on commit f971c75

Please sign in to comment.