4747import org .tron .core .store .DynamicPropertiesStore ;
4848import org .tron .protos .Protocol .Block ;
4949import org .tron .protos .Protocol .BlockHeader ;
50- import org .tron .protos .Protocol .Key ;
5150import org .tron .protos .Protocol .PQScheme ;
5251import org .tron .protos .Protocol .PQAuthSig ;
53- import org .tron .protos .Protocol .Permission ;
5452import org .tron .protos .Protocol .Transaction ;
5553
5654@ Slf4j (topic = "capsule" )
@@ -203,41 +201,36 @@ private Sha256Hash getRawHash() {
203201 public boolean validateSignature (DynamicPropertiesStore dynamicPropertiesStore ,
204202 AccountStore accountStore ) throws ValidateSignatureException {
205203 BlockHeader header = block .getBlockHeader ();
206- boolean hasLegacy = ! header .getWitnessSignature ().isEmpty ();
207- boolean hasPq = header . hasPqAuthSig ();
204+ byte [] witnessAccountAddress = header .getRawData ().getWitnessAddress ()
205+ . toByteArray ();
208206
209- if ( hasLegacy && hasPq ) {
210- throw new ValidateSignatureException (
211- "witness_signature and pq_auth_sig are mutually exclusive" ) ;
212- }
213- if (! hasLegacy && ! hasPq ) {
214- throw new ValidateSignatureException ( "missing witness signature" );
207+ byte [] witnessPermissionAddress ;
208+ if ( dynamicPropertiesStore . getAllowMultiSign () != 1 ) {
209+ witnessPermissionAddress = witnessAccountAddress ;
210+ } else {
211+ witnessPermissionAddress = accountStore . get ( witnessAccountAddress )
212+ . getWitnessPermissionAddress ( );
215213 }
216214
217- byte [] witnessAccountAddress = header .getRawData ().getWitnessAddress ().toByteArray ();
218- if (hasPq ) {
219- return validatePQSignature (dynamicPropertiesStore , accountStore ,
220- witnessAccountAddress , header .getPqAuthSig ());
215+ if (dynamicPropertiesStore .isAnyPqSchemeAllowed ()) {
216+ boolean hasLegacy = !header .getWitnessSignature ().isEmpty ();
217+ boolean hasPq = header .hasPqAuthSig ();
218+ if (hasLegacy && hasLegacy ) {
219+ throw new ValidateSignatureException (
220+ "witness_signature and pq_auth_sig are mutually exclusive" );
221+ }
222+ if (!hasLegacy && !hasPq ) {
223+ throw new ValidateSignatureException ("missing witness signature" );
224+ }
225+ return validatePQSignature (dynamicPropertiesStore , accountStore , witnessPermissionAddress ,
226+ header .getPqAuthSig ());
221227 }
222- return validateLegacySignature (dynamicPropertiesStore , accountStore , witnessAccountAddress );
223- }
224228
225- private boolean validateLegacySignature (DynamicPropertiesStore dynamicPropertiesStore ,
226- AccountStore accountStore , byte [] witnessAccountAddress )
227- throws ValidateSignatureException {
228229 try {
229230 byte [] sigAddress = SignUtils .signatureToAddress (getRawHash ().getBytes (),
230- TransactionCapsule .getBase64FromByteString (
231- block .getBlockHeader ().getWitnessSignature ()),
231+ TransactionCapsule .getBase64FromByteString (header .getWitnessSignature ()),
232232 CommonParameter .getInstance ().isECKeyCryptoEngine ());
233- if (dynamicPropertiesStore .getAllowMultiSign () != 1 ) {
234- return Arrays .equals (sigAddress , witnessAccountAddress );
235- }
236- AccountCapsule witnessAccount = accountStore .get (witnessAccountAddress );
237- if (witnessAccount == null ) {
238- throw new ValidateSignatureException ("witness account does not exist" );
239- }
240- byte [] witnessPermissionAddress = witnessAccount .getWitnessPermissionAddress ();
233+
241234 return Arrays .equals (sigAddress , witnessPermissionAddress );
242235 } catch (SignatureException e ) {
243236 throw new ValidateSignatureException (e .getMessage ());
@@ -250,8 +243,11 @@ private boolean validateLegacySignature(DynamicPropertiesStore dynamicProperties
250243 * the witness account's Witness Permission keys[].
251244 */
252245 private boolean validatePQSignature (DynamicPropertiesStore dynamicPropertiesStore ,
253- AccountStore accountStore , byte [] witnessAccountAddress , PQAuthSig pqAuthSig )
246+ AccountStore accountStore , byte [] witnessPermissionAddress , PQAuthSig pqAuthSig )
254247 throws ValidateSignatureException {
248+ /*
249+ Verify the PQ scheme is supported and proposal opened
250+ */
255251 PQScheme scheme = pqAuthSig .getScheme ();
256252 if (!PQSchemeRegistry .contains (scheme )) {
257253 throw new ValidateSignatureException (
@@ -262,38 +258,22 @@ private boolean validatePQSignature(DynamicPropertiesStore dynamicPropertiesStor
262258 "pq_auth_sig scheme " + scheme + " is not activated" );
263259 }
264260
265- AccountCapsule accountCapsule = accountStore .get (witnessAccountAddress );
266- Permission witnessPermission = null ;
267- if (accountCapsule != null && accountCapsule .getInstance ().hasWitnessPermission ()) {
268- witnessPermission = accountCapsule .getInstance ().getWitnessPermission ();
269- }
270- if (witnessPermission == null || witnessPermission .getKeysCount () == 0 ) {
271- throw new ValidateSignatureException (
272- "pq_auth_sig present but witness permission is not configured" );
273- }
274-
275261 byte [] publicKey = pqAuthSig .getPublicKey ().toByteArray ();
276262 if (publicKey .length != PQSchemeRegistry .getPublicKeyLength (scheme )) {
277263 throw new ValidateSignatureException (
278264 "pq_auth_sig public key length mismatch for scheme " + scheme );
279265 }
280- byte [] signature = pqAuthSig .getSignature ().toByteArray ();
281- if (!PQSchemeRegistry .isValidSignatureLength (scheme , signature .length )) {
282- throw new ValidateSignatureException (
283- "pq_auth_sig signature length mismatch for scheme " + scheme );
284- }
285266
286267 byte [] derivedAddr = PQSchemeRegistry .computeAddress (scheme , publicKey );
287- Key matched = null ;
288- for (Key k : witnessPermission .getKeysList ()) {
289- if (Arrays .equals (k .getAddress ().toByteArray (), derivedAddr )) {
290- matched = k ;
291- break ;
292- }
268+ if (!Arrays .equals (derivedAddr , witnessPermissionAddress )) {
269+ throw new ValidateSignatureException (
270+ "pq_auth_sig public key does not match witness permission address" );
293271 }
294- if (matched == null ) {
272+
273+ byte [] signature = pqAuthSig .getSignature ().toByteArray ();
274+ if (!PQSchemeRegistry .isValidSignatureLength (scheme , signature .length )) {
295275 throw new ValidateSignatureException (
296- "pq_auth_sig public key does not match any witness permission key" );
276+ "pq_auth_sig signature length mismatch for scheme " + scheme );
297277 }
298278
299279 byte [] digest = getRawHash ().getBytes ();
@@ -419,10 +399,13 @@ public long getTimeStamp() {
419399 return this .block .getBlockHeader ().getRawData ().getTimestamp ();
420400 }
421401
422- public boolean hasWitnessSignature () {
402+ public boolean hasWitnessSignature (DynamicPropertiesStore dynamicPropertiesStore ) {
423403 BlockHeader header = getInstance ().getBlockHeader ();
424- return !header .getWitnessSignature ().isEmpty ()
425- || !header .getPqAuthSig ().getSignature ().isEmpty ();
404+ boolean hasLegacySignature = !header .getWitnessSignature ().isEmpty ();
405+ if (!dynamicPropertiesStore .isAnyPqSchemeAllowed ()) {
406+ return hasLegacySignature ;
407+ }
408+ return hasLegacySignature || !header .getPqAuthSig ().getSignature ().isEmpty ();
426409 }
427410
428411 @ Override
0 commit comments