예제 #1
0
        public static bool TryDecode(string encodedAddress, out Address address)
        {
            byte[] base58DecodedAddress;
            if (!Base58.TryDecode(encodedAddress, out base58DecodedAddress))
            {
                address = null;
                return(false);
            }

            // TODO: This has to be updated to parse pubkey addresses, which have different
            // base58 -decoded lengths than the p2pkh and p2sh addresses.

            if (base58DecodedAddress.Length != 2 + Ripemd160Hash.Length + Checksum.SumLength)
            {
                address = null;
                return(false);
            }

            if (!Checksum.Verify(base58DecodedAddress))
            {
                address = null;
                return(false);
            }

            // All p2pkh and p2sh addresses store the pubkey hash in the same location.
            var hash160 = new byte[Ripemd160Hash.Length];

            Array.Copy(base58DecodedAddress, 2, hash160, 0, Ripemd160Hash.Length);

            var prefix = new AddressPrefix(base58DecodedAddress[0], base58DecodedAddress[1]);
            BlockChainIdentity intendedBlockChain;

            if (AddressPrefix.IdentityForSecp256k1PubkeyHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToSecp256k1PubKeyHash(intendedBlockChain, hash160);
                return(true);
            }
            else if (AddressPrefix.IdentityForEd25519PubKeyHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToEd25519PubKeyHash(intendedBlockChain, hash160);
                return(true);
            }
            else if (AddressPrefix.IdentityForSecSchnorrPubKeyHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToSecSchnorrPubKeyHash(intendedBlockChain, hash160);
                return(true);
            }
            else if (AddressPrefix.IdentityForScriptHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToScriptHash(intendedBlockChain, hash160);
                return(true);
            }
            else
            {
                address = null;
                return(false);
            }
        }
예제 #2
0
            public override string Encode()
            {
                var buffer = new byte[2 + Ripemd160Hash.Length + Checksum.SumLength];
                var prefix = AddressPrefix.PayToEd25519PubKeyHashPrefix(IntendedBlockChain);

                buffer[0] = prefix.FirstByte;
                buffer[1] = prefix.SecondByte;
                Array.Copy(PubKeyHash, 0, buffer, 2, Ripemd160Hash.Length);
                Checksum.WriteSum(buffer);
                return(Base58.Encode(buffer));
            }
예제 #3
0
 public static bool IdentityForScriptHashPrefix(AddressPrefix prefix, out BlockChainIdentity identity)
 {
     if (prefix == MainNetScriptHashPrefix)
     {
         identity = BlockChainIdentity.MainNet;
         return(true);
     }
     else if (prefix == TestNetScriptHashPrefix)
     {
         identity = BlockChainIdentity.TestNet;
         return(true);
     }
     else if (prefix == SimNetScriptHashPrefix)
     {
         identity = BlockChainIdentity.SimNet;
         return(true);
     }
     else
     {
         identity = null;
         return(false);
     }
 }
예제 #4
0
 public static bool IdentityForScriptHashPrefix(AddressPrefix prefix, out BlockChainIdentity identity)
 {
     if (prefix == MainNetScriptHashPrefix)
     {
         identity = BlockChainIdentity.MainNet;
         return true;
     }
     else if (prefix == TestNetScriptHashPrefix)
     {
         identity = BlockChainIdentity.TestNet;
         return true;
     }
     else if (prefix == SimNetScriptHashPrefix)
     {
         identity = BlockChainIdentity.SimNet;
         return true;
     }
     else
     {
         identity = null;
         return false;
     }
 }
예제 #5
0
파일: Address.cs 프로젝트: jrick/Paymetheus
        public static bool TryDecode(string encodedAddress, out Address address)
        {
            byte[] base58DecodedAddress;
            if (!Base58.TryDecode(encodedAddress, out base58DecodedAddress))
            {
                address = null;
                return false;
            }

            // TODO: This has to be updated to parse pubkey addresses, which have different
            // base58 -decoded lengths than the p2pkh and p2sh addresses.

            if (base58DecodedAddress.Length != 2 + Ripemd160Hash.Length + Checksum.SumLength)
            {
                address = null;
                return false;
            }

            if (!Checksum.Verify(base58DecodedAddress))
            {
                address = null;
                return false;
            }

            // All p2pkh and p2sh addresses store the pubkey hash in the same location.
            var hash160 = new byte[Ripemd160Hash.Length];
            Array.Copy(base58DecodedAddress, 2, hash160, 0, Ripemd160Hash.Length);

            var prefix = new AddressPrefix(base58DecodedAddress[0], base58DecodedAddress[1]);
            BlockChainIdentity intendedBlockChain;
            if (AddressPrefix.IdentityForSecp256k1PubkeyHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToSecp256k1PubKeyHash(intendedBlockChain, hash160);
                return true;
            }
            else if (AddressPrefix.IdentityForEd25519PubKeyHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToEd25519PubKeyHash(intendedBlockChain, hash160);
                return true;
            }
            else if (AddressPrefix.IdentityForSecSchnorrPubKeyHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToSecSchnorrPubKeyHash(intendedBlockChain, hash160);
                return true;
            }
            else if (AddressPrefix.IdentityForScriptHashPrefix(prefix, out intendedBlockChain))
            {
                address = new PayToScriptHash(intendedBlockChain, hash160);
                return true;
            }
            else
            {
                address = null;
                return false;
            }
        }