Ejemplo n.º 1
0
    def tx(self, txid):
        """
        Get the MsgTx. Retreive it from the blockchain if necessary. 

        Args:
            txid (str): A hex encoded transaction ID to fetch. 

        Returns:
            MsgTx: The transaction.
        """
        hashKey = hashFromHex(txid).bytes()
        with self.txDB as txDB:
            try:
                encoded = ByteArray(txDB[hashKey])
                return msgtx.MsgTx.deserialize(encoded)
            except database.NoValue:
                try:
                    # Grab the hex encoded transaction
                    txHex = self.dcrdata.tx.hex(txid)
                    if not txHex:
                        raise Exception(
                            "failed to retrieve tx hex from dcrdata")
                    encoded = ByteArray(txHex)
                    txDB[hashKey] = encoded.bytes()
                    return msgtx.MsgTx.deserialize(encoded)
                except:
                    log.warning(
                        "unable to retrieve tx data from dcrdata at %s" %
                        self.dcrdata.baseUri)
        raise Exception("failed to reteive transaction")
Ejemplo n.º 2
0
 def __init__(self, pw):
     """
     Args:
         pw (byte-like): A password that deterministically generates the key. 
     """
     super().__init__()
     salt = ByteArray(generateSeed(KEY_SIZE))
     b = lambda v: ByteArray(v).bytes()
     func, hashName, iterations = defaultKDFParams()
     self.key = ByteArray(
         hashlib.pbkdf2_hmac(hashName, b(pw), salt.bytes(), iterations))
     digest = ByteArray(hashlib.sha256(self.key.b).digest())
     self.keyParams = KDFParams(salt, digest)
Ejemplo n.º 3
0
    def string(self):
        """
        string returns the extended key as a base58-encoded string. See
        `decodeExtendedKey` for decoding.

        Returns:
            str: The encoded extended key.
        """
        if self.key.iszero():
            raise ZeroBytesError("unexpected zero key")

        childNumBytes = ByteArray(self.childNum, length=4)
        depthByte = ByteArray(self.depth % 256, length=1)

        # The serialized format is:
        #   version (4) || depth (1) || parent fingerprint (4)) ||
        #   child num (4) || chain code (32) || key data (33) || checksum (4)
        serializedBytes = ByteArray(
            bytearray(0))  # length serializedKeyLen + 4 after appending
        if self.isPrivate:
            serializedBytes += self.privVer
        else:
            serializedBytes += self.pubVer

        serializedBytes += depthByte
        serializedBytes += self.parentFP
        serializedBytes += childNumBytes
        serializedBytes += self.chainCode
        if self.isPrivate:
            serializedBytes += bytearray(1)
            serializedBytes += self.key
        else:
            serializedBytes += self.pubKey

        checkSum = checksum(serializedBytes.b)[:4]
        serializedBytes += checkSum
        return b58encode(serializedBytes.bytes()).decode()