Example #1
0
    def __create_account(self, label: str, pwd: str, salt: bytes,
                         priv_key: bytes, account_flag: bool):
        account = Account(priv_key, self.scheme)
        # initialization
        if self.scheme == SignatureScheme.SHA256withECDSA:
            acct = AccountData()
        else:
            raise ValueError("scheme type is error")
        # set key
        if pwd != None:
            acct.key = account.export_gcm_encrypted_private_key(
                pwd, salt,
                Scrypt().get_n())
            pwd = None
        else:
            acct.key = account.serialize_private_key().hex()

        acct.address = account.get_address_base58()
        # set label
        if label == None or label == "":
            label = str(uuid.uuid4())[0:8]
        if account_flag:
            for index in range(len(self.wallet_in_mem.accounts)):
                if acct.address == self.wallet_in_mem.accounts[index].address:
                    raise ValueError("wallet account exists")

            if len(self.wallet_in_mem.accounts) == 0:
                acct.isDefault = True
                self.wallet_in_mem.defaultAccountAddress = acct.address
            acct.label = label
            acct.salt = base64.b64encode(salt).decode()
            acct.publicKey = account.serialize_public_key().hex()
            self.wallet_in_mem.accounts.append(acct)
        else:
            for index in range(len(self.wallet_in_mem.identities)):
                if self.wallet_in_mem.identities[
                        index].ontid == did_ont + acct.address:
                    raise ValueError("wallet identity exists")
            idt = Identity()
            idt.ontid = did_ont + acct.address
            idt.label = label
            if len(self.wallet_in_mem.identities) == 0:
                idt.isDefault = True
                self.wallet_in_mem.defaultOntid = idt.ontid
            ctl = Control(id="keys-1",
                          key=acct.key,
                          salt=base64.b64encode(salt).decode(),
                          address=acct.address,
                          public_key=account.serialize_public_key().hex())
            idt.controls.append(ctl)
            self.wallet_in_mem.identities.append(idt)
        return account
 def test_serialize_private_key(self):
     hex_private_key = "523c5fcf74823831756f0bcb3634234f10b3beb1c05595058534577752ad2d9f"
     hex_public_key = "03036c12be3726eb283d078dff481175e96224f0b0c632c7a37e10eb40fe6be889"
     base58_addr = "ANH5bHrrt111XwNEnuPZj6u95Dd6u7G4D6"
     wif = b'KyyZpJYXRfW8CXxH2B6FRq5AJsyTH7PjPACgBht4xRjstz4mxkeJ'
     account = Account(hex_private_key, SignatureScheme.SHA256withECDSA)
     hex_serialize_private_key = account.serialize_private_key().hex()
     self.assertEqual(hex_private_key, hex_serialize_private_key)
     self.assertEqual(account.serialize_public_key().hex(), hex_public_key)
     self.assertEqual(account.export_wif(), wif)
     self.assertEqual(account.get_address_base58(), base58_addr)
 def add_sign_transaction(self, tx: Transaction, signer: Account):
     if tx.sigs == None or len(tx.sigs) == 0:
         tx.sigs = []
     elif len(tx.sigs) >= Common.TX_MAX_SIG_SIZE:
         raise Exception(
             "the number of transaction signatures should not be over 16")
     tx_hash = tx.hash256()
     sig_data = signer.generate_signature(tx_hash,
                                          signer.get_signature_scheme())
     sig = Sig([signer.serialize_public_key()], 1, [sig_data])
     tx.sigs.append(sig)
     return tx
Example #4
0
    def test_generateSignature(self):
        acc = Account(
            "75de8489fcb2dcaf2ef3cd607feffde18789de7da129b5e97c81e001793cb7cf")
        sigaa = "01cf157a48216bfcd455a97a39c0ad65bd1b27d1da07965b19848146045c9f2e5a12f905a5ee0923412d589e615a5d6954c58cade367dce67fcf13eaa82c12e87a"
        byte_signature = acc.generate_signature(
            "sss".encode(), SignatureScheme.SHA256withECDSA)

        handler = SignatureHandler(KeyType.ECDSA,
                                   SignatureScheme.SHA256withECDSA)

        res = handler.verify_signature(acc.serialize_public_key(),
                                       "sss".encode(),
                                       bytearray.fromhex(sigaa))
        self.assertTrue(res)
Example #5
0
    def add_sign_transaction(tx: Transaction, signer: Account):
        """
        This interface is used to add signature into the transaction.

        :param tx: a Transaction object which will be signed.
        :param signer: an Account object which will sign the transaction.
        :return: a Transaction object which has been signed.
        """
        if tx.sigs is None or len(tx.sigs) == 0:
            tx.sigs = []
        elif len(tx.sigs) >= Common.TX_MAX_SIG_SIZE:
            raise SDKException(
                ErrorCode.param_err(
                    'the number of transaction signatures should not be over 16'
                ))
        tx_hash = tx.hash256_bytes()
        sig_data = signer.generate_signature(tx_hash,
                                             signer.get_signature_scheme())
        sig = Sig([signer.serialize_public_key()], 1, [sig_data])
        tx.sigs.append(sig)
        return tx
 def test_serialize_public_key(self):
     hex_private_key = '523c5fcf74823831756f0bcb3634234f10b3beb1c05595058534577752ad2d9f'
     hex_public_key = "03036c12be3726eb283d078dff481175e96224f0b0c632c7a37e10eb40fe6be889"
     account = Account(hex_private_key, SignatureScheme.SHA256withECDSA)
     self.assertEqual(hex_public_key, account.serialize_public_key().hex())
Example #7
0
import base64
import unittest

from ontology.utils import util
from ontology.account.account import Account
from ontology.wallet.wallet import WalletData
from ontology.wallet.account import AccountData
from ontology.crypto.signature_scheme import SignatureScheme
from ontology.common.address import Address

private_key = "8b6bb2bebb27f3e2c24cc8a3febb413c1ef98bd2481e2292ada6d90f9a5f5ec9"
if __name__ == '__main__':
    account = Account(private_key)
    publickey = account.serialize_public_key()
    wif = account.export_wif()
    privatekey = account.get_privatekey_from_wif(wif)
    print("public key is ", publickey, type(publickey))
    print("private key is ", privatekey)
    print("...", account.serialize_private_key())
    address = Address(publickey)
    addr = address.b58decode()
    print("address is ", addr)