@@ -816,6 +816,7 @@ static u8 *handle_sign_option_will_fund_offer(struct hsmd_client *c,
816816 return towire_hsmd_sign_option_will_fund_offer_reply (NULL , & sig );
817817}
818818
819+ /*
819820static void payer_key_tweak(const struct pubkey *bolt12,
820821 const u8 *publictweak, size_t publictweaklen,
821822 struct sha256 *tweak)
@@ -830,48 +831,63 @@ static void payer_key_tweak(const struct pubkey *bolt12,
830831 sha256_update(&sha, publictweak, publictweaklen);
831832 sha256_done(&sha, tweak);
832833}
834+ */
833835
834- /*~ lightningd asks us to sign a bolt12 (e.g. offer). */
836+ static void node_blinded_privkey (const struct pubkey * path_pubkey , struct privkey * blinded_privkey )
837+ {
838+ struct secret ss ;
839+ struct secret node_id_blinding ;
840+
841+ node_key (blinded_privkey , NULL );
842+
843+ /* BOLT #4:
844+ * - $`ss_i = SHA256(e_i * N_i) = SHA256(k_i * E_i)`$
845+ * (ECDH shared secret known only by $`N_r`$ and $`N_i`$)
846+ */
847+ if (secp256k1_ecdh (secp256k1_ctx , ss .data ,
848+ & path_pubkey -> pubkey , blinded_privkey -> secret .data ,
849+ NULL , NULL ) != 1 )
850+ hsmd_status_failed (STATUS_FAIL_INTERNAL_ERROR ,
851+ "Could not compute ss from path_key." );
852+
853+ /* BOLT #4:
854+ * - $`B_i = HMAC256(\text{"blinded\_node\_id"}, ss_i) * N_i`$
855+ * (blinded `node_id` for $`N_i`$, private key known only by $`N_i`$)
856+ */
857+ subkey_from_hmac ("blinded_node_id" , & ss , & node_id_blinding );
858+
859+ if (secp256k1_ec_seckey_tweak_mul (secp256k1_ctx ,
860+ blinded_privkey -> secret .data ,
861+ node_id_blinding .data ) != 1 )
862+ hsmd_status_failed (STATUS_FAIL_INTERNAL_ERROR ,
863+ "Could tweak bolt12 key." );
864+ }
865+
866+ /*~ lightningd asks us to sign a bolt12 invoice. */
835867static u8 * handle_sign_bolt12 (struct hsmd_client * c , const u8 * msg_in )
836868{
837869 char * messagename , * fieldname ;
838870 struct sha256 merkle , sha ;
839871 struct bip340sig sig ;
840872 secp256k1_keypair kp ;
841- u8 * publictweak ;
873+ struct pubkey * path_pubkey ;
842874
843875 if (!fromwire_hsmd_sign_bolt12 (tmpctx , msg_in ,
844876 & messagename , & fieldname , & merkle ,
845- & publictweak ))
877+ & path_pubkey ))
846878 return hsmd_status_malformed_request (c , msg_in );
847879
848880 sighash_from_merkle (messagename , fieldname , & merkle , & sha );
849881
850- if (!publictweak ) {
882+ if (!path_pubkey ) {
851883 node_schnorrkey (& kp );
852884 } else {
853- /* If we're tweaking key, we use bolt12 key */
854- struct privkey tweakedkey ;
855- struct pubkey bolt12 ;
856- struct sha256 tweak ;
885+ struct privkey blinded_privkey ;
857886
858- if (secp256k1_ec_pubkey_create (secp256k1_ctx , & bolt12 .pubkey ,
859- secretstuff .bolt12 .data ) != 1 )
860- hsmd_status_failed (STATUS_FAIL_INTERNAL_ERROR ,
861- "Could derive bolt12 public key." );
862-
863- payer_key_tweak (& bolt12 , publictweak , tal_bytelen (publictweak ),
864- & tweak );
865-
866- tweakedkey .secret = secretstuff .bolt12 ;
867- if (secp256k1_ec_seckey_tweak_add (secp256k1_ctx ,
868- tweakedkey .secret .data ,
869- tweak .u .u8 ) != 1 )
870- hsmd_status_failed (STATUS_FAIL_INTERNAL_ERROR ,
871- "Could tweak bolt12 key." );
887+ node_blinded_privkey (path_pubkey , & blinded_privkey );
872888
873889 if (secp256k1_keypair_create (secp256k1_ctx , & kp ,
874- tweakedkey .secret .data ) != 1 )
890+ blinded_privkey .secret .data ) != 1 )
875891 hsmd_status_failed (STATUS_FAIL_INTERNAL_ERROR ,
876892 "Failed to derive bolt12 keypair" );
877893 }
0 commit comments