src/TAO/API/types/assets/schema.cpp
json::json Assets::GetSchema(const json::json& params, bool fHelp)
        {
            /* The response JSON array */
            json::json ret = json::json::array();

            /* Get the Register ID. */
            TAO::Register::Address hashRegister ;

            /* Check whether the caller has provided the asset name parameter. */
            if(params.find("name") != params.end())
            {
                /* If name is provided then use this to deduce the register address */
                hashRegister = Names::ResolveAddress(params, params["name"].get<std::string>());
            }

            /* Otherwise try to find the raw hex encoded address. */
            else if(params.find("address") != params.end())
                hashRegister.SetBase58(params["address"].get<std::string>());

            /* Fail if no required parameters supplied. */
            else
                throw APIException(-33, "Missing name / address");


            /* Get the asset from the register DB.  We can read it as an Object.
               If this fails then we try to read it as a base State type and assume it was
               created as a raw format asset */
            TAO::Register::Object object;
            if(!LLD::Register->ReadState(hashRegister, object, TAO::Ledger::FLAGS::MEMPOOL))
                throw APIException(-34, "Asset not found");

            /* Only include non-standard object types */
            if(object.nType == TAO::Register::REGISTER::OBJECT)
            {
                /* parse object so that the data fields can be accessed */
                if(!object.Parse())
                    throw APIException(-36, "Failed to parse object register");

                /* Only include non standard object registers (assets) */
                if(object.Standard() != TAO::Register::OBJECTS::NONSTANDARD)
                    throw APIException(-35, "Specified name/address is not an asset.");
            }
            else
                throw APIException(-35, "Specified name/address is not an asset.");

            /* Populate the response JSON */
            /* Get List of field names in this asset object */
            std::vector<std::string> vFieldNames = object.GetFieldNames();

            /* Declare type and data variables for unpacking the Object fields */
            for(const auto& strName : vFieldNames)
            {
                /* JSON for this field */
                json::json field;

                /* max field size (only applicable to mutable fields) */
                std::size_t nMaxSize = 0;

                /* Add the name */
                field["name"] = strName;

                /* Get the type */
                uint8_t nType = 0;
                object.Type(strName, nType);

                /* Switch based on type. */
                switch(nType)
                {
                    /* Check for uint8_t type. */
                    case TAO::Register::TYPES::UINT8_T:
                    {
                        /* Add the type */
                        field["type"] = "uint8";

                        /* Set the return value from object register data. */
                        field["value"] = object.get<uint8_t>(strName);

                        break;
                    }

                    /* Check for uint16_t type. */
                    case TAO::Register::TYPES::UINT16_T:
                    {
                        /* Add the type */
                        field["type"] = "uint16";

                        /* Set the return value from object register data. */
                        field["value"] = object.get<uint16_t>(strName);

                        break;
                    }

                    /* Check for uint32_t type. */
                    case TAO::Register::TYPES::UINT32_T:
                    {
                        /* Add the type */
                        field["type"] = "uint32";

                        /* Set the return value from object register data. */
                        field["value"] = object.get<uint32_t>(strName);

                        break;
                    }

                    /* Check for uint64_t type. */
                    case TAO::Register::TYPES::UINT64_T:
                    {
                        /* Add the type */
                        field["type"] = "uint64";

                        /* Set the return value from object register data. */
                        field["value"] = object.get<uint64_t>(strName);

                        break;
                    }

                    /* Check for uint256_t type. */
                    case TAO::Register::TYPES::UINT256_T:
                    {
                        /* Add the type */
                        field["type"] = "uint256";

                        /* Set the return value from object register data. */
                        field["value"] = object.get<uint256_t>(strName).GetHex();

                        break;
                    }

                    /* Check for uint512_t type. */
                    case TAO::Register::TYPES::UINT512_T:
                    {
                        /* Add the type */
                        field["type"] = "uint512";

                        /* Set the return value from object register data. */
                        field["value"] = object.get<uint512_t>(strName).GetHex();

                        break;
                    }

                    /* Check for uint1024_t type. */
                    case TAO::Register::TYPES::UINT1024_T:
                    {
                        /* Add the type */
                        field["type"] = "uint1024";

                        /* Set the return value from object register data. */
                        field["value"] = object.get<uint1024_t>(strName).GetHex();

                        break;
                    }

                    /* Check for string type. */
                    case TAO::Register::TYPES::STRING:
                    {
                        /* Add the type */
                        field["type"] = "string";

                        /* Set the return value from object register data. */
                        std::string strRet = object.get<std::string>(strName);

                        /* get the size */
                        nMaxSize = strRet.length();

                        /* Remove trailing nulls from the data, which are padding to maxlength on mutable fields */
                        field["value"] = strRet.substr(0, strRet.find_last_not_of('\0') + 1);


                        break;
                    }

                    /* Check for bytes type. */
                    case TAO::Register::TYPES::BYTES:
                    {
                        /* Add the type */
                        field["type"] = "bytes";

                        /* Set the return value from object register data. */
                        std::vector<uint8_t> vRet = object.get<std::vector<uint8_t>>(strName);

                        /* get the size */
                        nMaxSize = vRet.size();

                        /* Remove trailing nulls from the data, which are padding to maxlength on mutable fields */
                        vRet.erase(std::find(vRet.begin(), vRet.end(), '\0'), vRet.end());

                        /* Add to return value in base64 encoding. */
                        field["value"] = encoding::EncodeBase64(&vRet[0], vRet.size()) ;

                        break;
                    }
                }

                /* Add mutable flag */
                field["mutable"] = object.mapData[strName].second;

                /* If mutable, add the max size */
                if(object.mapData[strName].second && nMaxSize > 0)
                    field["maxlength"] = nMaxSize;

                /* Add the field to the response array */
                ret.push_back(field);
            }

            return ret;
        }
/* Get the schema for an asset */
Get the schema for an asset
Get the schema for an asset
json::json Assets::GetSchema(const json::json& params, bool fHelp)
        {
            json::json ret = json::json::array();
            TAO::Register::Address hashRegister ;
            if(params.find("name") != params.end())
            {
                hashRegister = Names::ResolveAddress(params, params["name"].get<std::string>());
            }
            else if(params.find("address") != params.end())
                hashRegister.SetBase58(params["address"].get<std::string>());
            else
                throw APIException(-33, "Missing name / address");
            TAO::Register::Object object;
            if(!LLD::Register->ReadState(hashRegister, object, TAO::Ledger::FLAGS::MEMPOOL))
                throw APIException(-34, "Asset not found");
            if(object.nType == TAO::Register::REGISTER::OBJECT)
            {
                if(!object.Parse())
                    throw APIException(-36, "Failed to parse object register");
                if(object.Standard() != TAO::Register::OBJECTS::NONSTANDARD)
                    throw APIException(-35, "Specified name/address is not an asset.");
            }
            else
                throw APIException(-35, "Specified name/address is not an asset.");
            std::vector<std::string> vFieldNames = object.GetFieldNames();
            for(const auto& strName : vFieldNames)
            {
                json::json field;
                std::size_t nMaxSize = 0;
                field["name"] = strName;
                uint8_t nType = 0;
                object.Type(strName, nType);
                switch(nType)
                {
                    case TAO::Register::TYPES::UINT8_T:
                    {
                        field["type"] = "uint8";
                        field["value"] = object.get<uint8_t>(strName);
                        break;
                    }
                    case TAO::Register::TYPES::UINT16_T:
                    {
                        field["type"] = "uint16";
                        field["value"] = object.get<uint16_t>(strName);
                        break;
                    }
                    case TAO::Register::TYPES::UINT32_T:
                    {
                        field["type"] = "uint32";
                        field["value"] = object.get<uint32_t>(strName);
                        break;
                    }
                    case TAO::Register::TYPES::UINT64_T:
                    {
                        field["type"] = "uint64";
                        field["value"] = object.get<uint64_t>(strName);
                        break;
                    }
                    case TAO::Register::TYPES::UINT256_T:
                    {
                        field["type"] = "uint256";
                        field["value"] = object.get<uint256_t>(strName).GetHex();
                        break;
                    }
                    case TAO::Register::TYPES::UINT512_T:
                    {
                        field["type"] = "uint512";
                        field["value"] = object.get<uint512_t>(strName).GetHex();
                        break;
                    }
                    case TAO::Register::TYPES::UINT1024_T:
                    {
                        field["type"] = "uint1024";
                        field["value"] = object.get<uint1024_t>(strName).GetHex();
                        break;
                    }
                    case TAO::Register::TYPES::STRING:
                    {
                        field["type"] = "string";
                        std::string strRet = object.get<std::string>(strName);
                        nMaxSize = strRet.length();
                        field["value"] = strRet.substr(0, strRet.find_last_not_of('\0') + 1);
                        break;
                    }
                    case TAO::Register::TYPES::BYTES:
                    {
                        field["type"] = "bytes";
                        std::vector<uint8_t> vRet = object.get<std::vector<uint8_t>>(strName);
                        nMaxSize = vRet.size();
                        vRet.erase(std::find(vRet.begin(), vRet.end(), '\0'), vRet.end());
                        field["value"] = encoding::EncodeBase64(&vRet[0], vRet.size()) ;
                        break;
                    }
                }
                field["mutable"] = object.mapData[strName].second;
                if(object.mapData[strName].second && nMaxSize > 0)
                    field["maxlength"] = nMaxSize;
                ret.push_back(field);
            }
            return ret;
        }
json :: json Assets :: GetSchema ( const json :: json & params , bool fHelp ) { json :: json ret = json :: json :: array ( ) ; TAO :: Register :: Address hashRegister ; if ( params . find ( " " ) != params . end ( ) ) { hashRegister = Names :: ResolveAddress ( params , params [ " " ] . get < std :: string > ( ) ) ; } else if ( params . find ( " " ) != params . end ( ) ) hashRegister . SetBase58 ( params [ " " ] . get < std :: string > ( ) ) ; else throw APIException ( -33 , " " ) ; TAO :: Register :: Object object ; if ( ! LLD :: Register -> ReadState ( hashRegister , object , TAO :: Ledger :: FLAGS :: MEMPOOL ) ) throw APIException ( -34 , " " ) ; if ( object . nType == TAO :: Register :: REGISTER :: OBJECT ) { if ( ! object . Parse ( ) ) throw APIException ( -36 , " " ) ; if ( object . Standard ( ) != TAO :: Register :: OBJECTS :: NONSTANDARD ) throw APIException ( -35 , " " ) ; } else throw APIException ( -35 , " " ) ; std :: vector < std :: string > vFieldNames = object . GetFieldNames ( ) ; for ( const auto & strName : vFieldNames ) { json :: json field ; std :: size_t nMaxSize = 0 ; field [ " " ] = strName ; uint8_t nType = 0 ; object . Type ( strName , nType ) ; switch ( nType ) { case TAO :: Register :: TYPES :: UINT8_T : { field [ " " ] = " " ; field [ " " ] = object . get < uint8_t > ( strName ) ; break ; } case TAO :: Register :: TYPES :: UINT16_T : { field [ " " ] = " " ; field [ " " ] = object . get < uint16_t > ( strName ) ; break ; } case TAO :: Register :: TYPES :: UINT32_T : { field [ " " ] = " " ; field [ " " ] = object . get < uint32_t > ( strName ) ; break ; } case TAO :: Register :: TYPES :: UINT64_T : { field [ " " ] = " " ; field [ " " ] = object . get < uint64_t > ( strName ) ; break ; } case TAO :: Register :: TYPES :: UINT256_T : { field [ " " ] = " " ; field [ " " ] = object . get < uint256_t > ( strName ) . GetHex ( ) ; break ; } case TAO :: Register :: TYPES :: UINT512_T : { field [ " " ] = " " ; field [ " " ] = object . get < uint512_t > ( strName ) . GetHex ( ) ; break ; } case TAO :: Register :: TYPES :: UINT1024_T : { field [ " " ] = " " ; field [ " " ] = object . get < uint1024_t > ( strName ) . GetHex ( ) ; break ; } case TAO :: Register :: TYPES :: STRING : { field [ " " ] = " " ; std :: string strRet = object . get < std :: string > ( strName ) ; nMaxSize = strRet . length ( ) ; field [ " " ] = strRet . substr ( 0 , strRet . find_last_not_of ( ' \0 ' ) + 1 ) ; break ; } case TAO :: Register :: TYPES :: BYTES : { field [ " " ] = " " ; std :: vector < uint8_t > vRet = object . get < std :: vector < uint8_t > > ( strName ) ; nMaxSize = vRet . size ( ) ; vRet . erase ( std :: find ( vRet . begin ( ) , vRet . end ( ) , ' \0 ' ) , vRet . end ( ) ) ; field [ " " ] = encoding :: EncodeBase64 ( & vRet [ 0 ] , vRet . size ( ) ) ; break ; } } field [ " " ] = object . mapData [ strName ] . second ; if ( object . mapData [ strName ] . second && nMaxSize > 0 ) field [ " " ] = nMaxSize ; ret . push_back ( field ) ; } return ret ; }
Get the schema for an asset
Get the schema for an asset
/* The response JSON array */ /* Get the Register ID. */ /* Check whether the caller has provided the asset name parameter. */ /* If name is provided then use this to deduce the register address */ /* Otherwise try to find the raw hex encoded address. */ /* Fail if no required parameters supplied. */ /* Get the asset from the register DB.  We can read it as an Object.
               If this fails then we try to read it as a base State type and assume it was
               created as a raw format asset */ /* Only include non-standard object types */ /* parse object so that the data fields can be accessed */ /* Only include non standard object registers (assets) */ /* Populate the response JSON */ /* Get List of field names in this asset object */ /* Declare type and data variables for unpacking the Object fields */ /* JSON for this field */ /* max field size (only applicable to mutable fields) */ /* Add the name */ /* Get the type */ /* Switch based on type. */ /* Check for uint8_t type. */ /* Add the type */ /* Set the return value from object register data. */ /* Check for uint16_t type. */ /* Add the type */ /* Set the return value from object register data. */ /* Check for uint32_t type. */ /* Add the type */ /* Set the return value from object register data. */ /* Check for uint64_t type. */ /* Add the type */ /* Set the return value from object register data. */ /* Check for uint256_t type. */ /* Add the type */ /* Set the return value from object register data. */ /* Check for uint512_t type. */ /* Add the type */ /* Set the return value from object register data. */ /* Check for uint1024_t type. */ /* Add the type */ /* Set the return value from object register data. */ /* Check for string type. */ /* Add the type */ /* Set the return value from object register data. */ /* get the size */ /* Remove trailing nulls from the data, which are padding to maxlength on mutable fields */ /* Check for bytes type. */ /* Add the type */ /* Set the return value from object register data. */ /* get the size */ /* Remove trailing nulls from the data, which are padding to maxlength on mutable fields */ /* Add to return value in base64 encoding. */ /* Add mutable flag */ /* If mutable, add the max size */ /* Add the field to the response array */