def test_generate_signature(self): raw_hex_data = '523c5fcf74823831756f0bcb3634234f10b3beb1c05595058534577752ad2d9f' account = Account(raw_hex_data, SignatureScheme.SHA256withECDSA) msg = 'test'.encode('utf-8') signature = account.generate_signature(msg) result = account.verify_signature(msg, signature) self.assertEqual(True, result)
def add_multi_sign_transaction(self, m: int, pub_keys: List[bytes] or List[str], signer: Account): """ This interface is used to generate an Transaction object which has multi signature. """ for index, pk in enumerate(pub_keys): if isinstance(pk, str): pub_keys[index] = pk.encode('ascii') pub_keys = ProgramBuilder.sort_public_keys(pub_keys) tx_hash = self.hash256() sig_data = signer.generate_signature(tx_hash) if self.sig_list is None or len(self.sig_list) == 0: self.sig_list = [] elif len(self.sig_list) >= TX_MAX_SIG_SIZE: raise SDKException( ErrorCode.param_err( 'the number of transaction signatures should not be over 16' )) else: for i in range(len(self.sig_list)): if self.sig_list[i].public_keys == pub_keys: if len(self.sig_list[i].sig_data) + 1 > len(pub_keys): raise SDKException( ErrorCode.param_err('too more sigData')) if self.sig_list[i].m != m: raise SDKException(ErrorCode.param_err('M error')) self.sig_list[i].sig_data.append(sig_data) return sig = Sig(pub_keys, m, [sig_data]) self.sig_list.append(sig)
def test_signature(self): for strength in self.strengths: master_key = HDPrivateKey.master_key_from_mnemonic( self.mnemonic.generate(strength)) acct = Account(master_key.hex()) signature = acct.generate_signature(self.msg) self.assertTrue(acct.verify_signature(self.msg, signature)) root_sk = HDPrivateKey.from_path(master_key)[-1] root_pk = root_sk.public_key bip32_root_pk = root_pk.b58encode() for index in range(10): child_sk = HDPrivateKey.from_path(root_sk, f'0/{index}')[-1] child_pk = HDPublicKey.from_path( HDPublicKey.b58decode(bip32_root_pk), f'0/{index}')[-1] child_acct = Account(child_sk.hex()) signature = child_acct.generate_signature(self.msg) handler = SignatureHandler(SignatureScheme.SHA256withECDSA) handler.verify_signature(child_pk.hex(), self.msg, signature)
def add_sign_transaction(self, signer: Account): """ This interface is used to add signature into the transaction. """ if self.sig_list is None or len(self.sig_list) == 0: self.sig_list = [] elif len(self.sig_list) >= TX_MAX_SIG_SIZE: raise SDKException( ErrorCode.param_err( 'the number of transaction signatures should not be over 16' )) tx_hash = self.hash256() sig_data = signer.generate_signature(tx_hash) sig = Sig([signer.get_public_key_bytes()], 1, [sig_data]) self.sig_list.append(sig)
def generate_signature(self, iss: Account, verify_kid: bool = True): if not isinstance(self.__head, Header) or not isinstance( self.__payload, Payload): raise SDKException( ErrorCode.other_error('Please set claim parameters first.')) if verify_kid: key_index = int(self.__head.kid.split('-')[1]) result = self.__sdk.native_vm.ont_id().verify_signature( iss.get_ont_id(), key_index, iss) if not result: raise SDKException( ErrorCode.other_error('Issuer account error.')) b64_head = self.__head.to_base64() b64_payload = self.__payload.to_base64() msg = f'{b64_head}.{b64_payload}'.encode('utf-8') self.__signature = iss.generate_signature(msg) return self.__signature