@@ -780,6 +780,7 @@ static u8 *handle_sign_option_will_fund_offer(struct hsmd_client *c,
780780 return towire_hsmd_sign_option_will_fund_offer_reply (NULL , & sig );
781781}
782782
783+ /*
783784static void payer_key_tweak(const struct pubkey *bolt12,
784785 const u8 *publictweak, size_t publictweaklen,
785786 struct sha256 *tweak)
@@ -794,48 +795,63 @@ static void payer_key_tweak(const struct pubkey *bolt12,
794795 sha256_update(&sha, publictweak, publictweaklen);
795796 sha256_done(&sha, tweak);
796797}
798+ */
797799
798- /*~ lightningd asks us to sign a bolt12 (e.g. offer). */
800+ static void node_blinded_privkey (const struct pubkey * path_pubkey , struct privkey * blinded_privkey )
801+ {
802+ struct secret ss ;
803+ struct secret node_id_blinding ;
804+
805+ node_key (blinded_privkey , NULL );
806+
807+ /* BOLT #4:
808+ * - $`ss_i = SHA256(e_i * N_i) = SHA256(k_i * E_i)`$
809+ * (ECDH shared secret known only by $`N_r`$ and $`N_i`$)
810+ */
811+ if (secp256k1_ecdh (secp256k1_ctx , ss .data ,
812+ & path_pubkey -> pubkey , blinded_privkey -> secret .data ,
813+ NULL , NULL ) != 1 )
814+ hsmd_status_failed (STATUS_FAIL_INTERNAL_ERROR ,
815+ "Could not compute ss from path_key." );
816+
817+ /* BOLT #4:
818+ * - $`B_i = HMAC256(\text{"blinded\_node\_id"}, ss_i) * N_i`$
819+ * (blinded `node_id` for $`N_i`$, private key known only by $`N_i`$)
820+ */
821+ subkey_from_hmac ("blinded_node_id" , & ss , & node_id_blinding );
822+
823+ if (secp256k1_ec_seckey_tweak_mul (secp256k1_ctx ,
824+ blinded_privkey -> secret .data ,
825+ node_id_blinding .data ) != 1 )
826+ hsmd_status_failed (STATUS_FAIL_INTERNAL_ERROR ,
827+ "Could tweak bolt12 key." );
828+ }
829+
830+ /*~ lightningd asks us to sign a bolt12 invoice. */
799831static u8 * handle_sign_bolt12 (struct hsmd_client * c , const u8 * msg_in )
800832{
801833 char * messagename , * fieldname ;
802834 struct sha256 merkle , sha ;
803835 struct bip340sig sig ;
804836 secp256k1_keypair kp ;
805- u8 * publictweak ;
837+ struct pubkey * path_pubkey ;
806838
807839 if (!fromwire_hsmd_sign_bolt12 (tmpctx , msg_in ,
808840 & messagename , & fieldname , & merkle ,
809- & publictweak ))
841+ & path_pubkey ))
810842 return hsmd_status_malformed_request (c , msg_in );
811843
812844 sighash_from_merkle (messagename , fieldname , & merkle , & sha );
813845
814- if (!publictweak ) {
846+ if (!path_pubkey ) {
815847 node_schnorrkey (& kp );
816848 } else {
817- /* If we're tweaking key, we use bolt12 key */
818- struct privkey tweakedkey ;
819- struct pubkey bolt12 ;
820- struct sha256 tweak ;
849+ struct privkey blinded_privkey ;
821850
822- if (secp256k1_ec_pubkey_create (secp256k1_ctx , & bolt12 .pubkey ,
823- secretstuff .bolt12 .data ) != 1 )
824- hsmd_status_failed (STATUS_FAIL_INTERNAL_ERROR ,
825- "Could derive bolt12 public key." );
826-
827- payer_key_tweak (& bolt12 , publictweak , tal_bytelen (publictweak ),
828- & tweak );
829-
830- tweakedkey .secret = secretstuff .bolt12 ;
831- if (secp256k1_ec_seckey_tweak_add (secp256k1_ctx ,
832- tweakedkey .secret .data ,
833- tweak .u .u8 ) != 1 )
834- hsmd_status_failed (STATUS_FAIL_INTERNAL_ERROR ,
835- "Could tweak bolt12 key." );
851+ node_blinded_privkey (path_pubkey , & blinded_privkey );
836852
837853 if (secp256k1_keypair_create (secp256k1_ctx , & kp ,
838- tweakedkey .secret .data ) != 1 )
854+ blinded_privkey .secret .data ) != 1 )
839855 hsmd_status_failed (STATUS_FAIL_INTERNAL_ERROR ,
840856 "Failed to derive bolt12 keypair" );
841857 }
0 commit comments