def encPrivateKeyValid(self, encPrivateKey): valid = False try: if not encPrivateKey: raise EncException("Invalid privateKey") base58.alphabet = b'123456789AbCDEFGHJKLMNPQRSTuVWXYZaBcdefghijkmnopqrstUvwxyz' priKeyTemp = base58.b58decode(encPrivateKey) condition1 = len(priKeyTemp) != 41 condition2 = priKeyTemp[0] != chr(218) condition3 = priKeyTemp[1] != chr(55) condition4 = priKeyTemp[2] != chr(159) condition5 = priKeyTemp[3] != chr(1) if condition1 or condition2 or condition3 or condition4 or condition5: raise EncException("Invalid privateKey") lenth = len(priKeyTemp) checkSum = priKeyTemp[lenth - 4:lenth] buff = priKeyTemp[0:lenth - 4] hash1 = CheckKey().CalHash(KeyType.ED25519, buff) hash2 = CheckKey().CalHash(KeyType.ED25519, hash1) checkSumCol = hash2[0:4] if checkSum != checkSumCol: raise EncException("Invalid privateKey") valid = True except Exception: valid = False return valid
def encPublicKey(self, keyType, raw_pkey): if not raw_pkey: raise EncException("Public key is null") buff = "" buff = buff + chr(176) buff = buff + chr(1) buff = buff + raw_pkey hash1 = CheckKey().CalHash(keyType, buff) hash2 = CheckKey().CalHash(keyType, hash1) temp = buff + hash2[0:4] return temp
def encAddress(self, keyType, raw_pkey): buff = "" buff = buff + chr(1) buff = buff + chr(86) buff = buff + chr(1) hashPkey = CheckKey().CalHash(keyType, raw_pkey) buff = buff + hashPkey[12:32] hash1 = CheckKey().CalHash(keyType, buff) hash2 = CheckKey().CalHash(keyType, hash1) temp = buff + hash2[0:4] base58.alphabet = b'123456789AbCDEFGHJKLMNPQRSTuVWXYZaBcdefghijkmnopqrstUvwxyz' return base58.b58encode(temp)
def encPublicKeyValid(self, encPublicKey): valid = False try: if not encPublicKey: raise EncException("Invalid publicKey") buffPKey = encPublicKey if len(buffPKey) < 6 or buffPKey[0] != chr(176) or buffPKey[1] != chr(1): raise EncException("Invalid publicKey") lenth = len(buffPKey) checkSum = buffPKey[lenth - 4:lenth] buff = buffPKey[0:lenth - 4] hash1 = CheckKey().CalHash(KeyType.ED25519, buff) hash2 = CheckKey().CalHash(KeyType.ED25519, hash1) checkSumCol = hash2[0:4] if checkSum != checkSumCol: raise EncException("Invalid publicKey") valid = True except EncException: valid = False return valid
def encAddressValid(self, encAddress): valid = False try: if not encAddress: raise EncException("Invalid address") base58.alphabet = b'123456789AbCDEFGHJKLMNPQRSTuVWXYZaBcdefghijkmnopqrstUvwxyz' adTemp = base58.b58decode(encAddress) if len(adTemp) != 27 or adTemp[0] != chr(1) or adTemp[1] != chr(86) or adTemp[2] != chr(1): raise EncException("Invalid address") lenth = len(adTemp) checkSum = adTemp[lenth - 4:lenth] buff = adTemp[0:lenth - 4] hash1 = CheckKey().CalHash(KeyType.ED25519, buff) hash2 = CheckKey().CalHash(KeyType.ED25519, hash1) checkSumCol = hash2[0:4] if checkSum != checkSumCol: raise EncException("Invalid address") valid = True except EncException: valid = False return valid
def EncPrivateKey(self, keytype, raw_skey): ''' 生成私钥 :param keytype: KeyType 用于存储算法类型(Ed25519)和版本号默认1 :param raw_skey: :return: ''' if not raw_skey: raise EncException("Private key is null") buff = "" buff = buff + chr(int(0xDA)) buff = buff + chr(int(0x37)) buff = buff + chr(int(0x9F)) buff = buff + chr(1) buff = buff + str(raw_skey) buff = buff + chr(0) hash1 = CheckKey().CalHash(keytype, buff) hash2 = CheckKey().CalHash(keytype, hash1) temp = buff + hash2[0:4] base58.alphabet = b'123456789AbCDEFGHJKLMNPQRSTuVWXYZaBcdefghijkmnopqrstUvwxyz' print "this is privateKey :", base58.b58encode(temp) return base58.b58encode(temp)
def getPrivateKey(self, bSkey, member): if not bSkey: raise EncException("Private key cannot be null") base58.alphabet = b'123456789AbCDEFGHJKLMNPQRSTuVWXYZaBcdefghijkmnopqrstUvwxyz' sKeyTemp = base58.b58decode(bSkey) if len(sKeyTemp) <= 9: raise EncException("Private key (" + bSkey + ") is invalid") if ord(sKeyTemp[3]) != 1: raise EncException("Private key (" + bSkey + ") is invalid") type = KeyType.ED25519 if not CheckKey().CheckSum(type, sKeyTemp): raise EncException("Private key (" + bSkey + ") is invalid") rawSkey = sKeyTemp[4:len(sKeyTemp) - 5] member.setKeyType(type) member.setRawSKey(rawSkey)
def getPublicKey(self, bPkey=None, member=None): if not bPkey: raise EncException("public key cannot be null") buffPkey = bPkey if len(buffPkey) < 6: raise EncException("public key (" + bPkey + ") is invalid, please check") if buffPkey[0] != chr(0xB0): raise EncException("public key (" + bPkey + ") is invalid, please check") if buffPkey[1] != chr(1): raise EncException("public key (" + bPkey + ") is invalid, please check") keyType = KeyType.ED25519 if not CheckKey().CheckSum(keyType, buffPkey): raise EncException("public key (" + bPkey + ") is invalid, please check") rawPKey = buffPkey[2:len(buffPkey) - 4] member.setRawPKey(rawPKey) member.setKeyType(keyType)