All files / src/serializer builtins.ts

87.27% Statements 48/55
81.57% Branches 31/38
94.11% Functions 16/17
88.46% Lines 46/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300                                                                      1x   6x   29x   153x   89x       1x   4x   10x   21x   14x                                                                                                                                                                                                                                                                       503x                                                                               503x 503x 503x 15593x   503x 3443x     3443x   503x       44x 34x   10x               10x   3x   7x         121x 105x   16x   3x 6x   3x 3x 1x   4x 1x   1x   13x 13x 3x 6x   6x 1x   2x         10x   3x   3x      
import {ABIDecoder} from './decoder'
import {ABIEncoder} from './encoder'
import {ABIField, ABISerializableConstructor} from './serializable'
 
import {
    Asset,
    BlockTimestamp,
    Bytes,
    Checksum160,
    Checksum256,
    Checksum512,
    ExtendedAsset,
    Float128,
    Float32,
    Float64,
    Int128,
    Int16,
    Int32,
    Int64,
    Int8,
    Name,
    PublicKey,
    Signature,
    Struct,
    TimePoint,
    TimePointSec,
    UInt128,
    UInt16,
    UInt32,
    UInt64,
    UInt8,
    VarInt,
    VarUInt,
} from '../chain'
 
const StringType = {
    abiName: 'string',
    abiDefault: () => '',
    fromABI: (decoder: ABIDecoder) => {
        return decoder.readString()
    },
    from: (string: string): string => string,
    toABI: (string: string, encoder: ABIEncoder) => {
        encoder.writeString(string)
    },
}
 
const BoolType = {
    abiName: 'bool',
    abiDefault: () => false,
    fromABI: (decoder: ABIDecoder) => {
        return decoder.readByte() === 1
    },
    from: (value: boolean): boolean => value,
    toABI: (value: boolean, encoder: ABIEncoder) => {
        encoder.writeByte(value === true ? 1 : 0)
    },
}
 
export interface BuiltinTypes {
    string: string
    'string?'?: string
    'string[]': string[]
    'string[]?'?: string[]
    bool: boolean
    'bool?'?: boolean
    'bool[]': boolean[]
    'bool[]?'?: boolean[]
    asset: Asset
    'asset?'?: Asset
    'asset[]': Asset[]
    'asset[]?'?: Asset[]
    extended_asset: ExtendedAsset
    'extended_asset?'?: ExtendedAsset
    'extended_asset[]': ExtendedAsset[]
    'extended_asset[]?'?: ExtendedAsset[]
    bytes: Bytes
    'bytes?'?: Bytes
    'bytes[]': Bytes[]
    'bytes[]?'?: Bytes[]
    checksum160: Checksum160
    'checksum160?'?: Checksum160
    'checksum160[]': Checksum160[]
    'checksum160[]?'?: Checksum160[]
    checksum256: Checksum256
    'checksum256?'?: Checksum256
    'checksum256[]': Checksum256[]
    'checksum256[]?'?: Checksum256[]
    checksum512: Checksum512
    'checksum512?'?: Checksum512
    'checksum512[]': Checksum512[]
    'checksum512[]?'?: Checksum512[]
    name: Name
    'name?'?: Name
    'name[]': Name[]
    'name[]?'?: Name[]
    publickey: PublicKey
    'publickey?'?: PublicKey
    'publickey[]': PublicKey[]
    'publickey[]?'?: PublicKey[]
    signature: Signature
    'signature?'?: Signature
    'signature[]': Signature[]
    'signature[]?'?: Signature[]
    symbol: Asset.Symbol
    'symbol?'?: Asset.Symbol
    'symbol[]': Asset.Symbol[]
    'symbol[]?'?: Asset.Symbol[]
    symbol_code: Asset.SymbolCode
    'symbol_code?'?: Asset.SymbolCode
    'symbol_code[]': Asset.SymbolCode[]
    'symbol_code[]?'?: Asset.SymbolCode[]
    time_point: TimePoint
    'time_point?'?: TimePoint
    'time_point[]': TimePoint[]
    'time_point[]?'?: TimePoint[]
    time_point_sec: TimePointSec
    'time_point_sec?'?: TimePointSec
    'time_point_sec[]': TimePointSec[]
    'time_point_sec[]?'?: TimePointSec[]
    block_timestamp_type: BlockTimestamp
    'block_timestamp_type?'?: BlockTimestamp
    'block_timestamp_type[]': BlockTimestamp[]
    'block_timestamp_type[]?'?: BlockTimestamp[]
    int8: Int8
    'int8?'?: Int8
    'int8[]': Int8[]
    'int8[]?'?: Int8[]
    int16: Int16
    'int16?'?: Int16
    'int16[]': Int16[]
    'int16[]?'?: Int16[]
    int32: Int32
    'int32?'?: Int32
    'int32[]': Int32[]
    'int32[]?'?: Int32[]
    int64: Int64
    'int64?'?: Int64
    'int64[]': Int64[]
    'int64[]?'?: Int64[]
    int128: Int128
    'int128?'?: Int128
    'int128[]': Int128[]
    'int128[]?'?: Int128[]
    uint8: UInt8
    'uint8?'?: UInt8
    'uint8[]': UInt8[]
    'uint8[]?'?: UInt8[]
    uint16: UInt16
    'uint16?'?: UInt16
    'uint16[]': UInt16[]
    'uint16[]?'?: UInt16[]
    uint32: UInt32
    'uint32?'?: UInt32
    'uint32[]': UInt32[]
    'uint32[]?'?: UInt32[]
    uint64: UInt64
    'uint64?'?: UInt64
    'uint64[]': UInt64[]
    'uint64[]?'?: UInt64[]
    uint128: UInt128
    'uint128?'?: UInt128
    'uint128[]': UInt128[]
    'uint128[]?'?: UInt128[]
    varint: VarInt
    'varint?'?: VarInt
    'varint[]': VarInt[]
    'varint[]?'?: VarInt[]
    varuint: VarUInt
    'varuint?'?: VarUInt
    'varuint[]': VarUInt[]
    'varuint[]?'?: VarUInt[]
    float32: Float32
    'float32?'?: Float32
    'float32[]': Float32[]
    'float32[]?'?: Float32[]
    float64: Float64
    'float64?'?: Float64
    'float64[]': Float64[]
    'float64[]?'?: Float64[]
    float128: Float128
    'float128?'?: Float128
    'float128[]': Float128[]
    'float128[]?'?: Float128[]
}
 
function getBuiltins(): ABISerializableConstructor[] {
    return [
        // types represented by JavaScript builtins
        BoolType as ABISerializableConstructor,
        StringType as ABISerializableConstructor,
        // types represented by Classes
        Asset,
        Asset.Symbol,
        Asset.SymbolCode,
        BlockTimestamp,
        Bytes,
        Checksum160,
        Checksum256,
        Checksum512,
        ExtendedAsset,
        Float128,
        Float32,
        Float64,
        Int128,
        Int16,
        Int32,
        Int64,
        Int8,
        Name,
        PublicKey,
        Signature,
        TimePoint,
        TimePointSec,
        UInt128,
        UInt16,
        UInt32,
        UInt64,
        UInt8,
        VarInt,
        VarUInt,
    ]
}
 
export type TypeLookup = {[name: string]: ABISerializableConstructor}
 
export function buildTypeLookup(additional: ABISerializableConstructor[] = []): TypeLookup {
    const rv: TypeLookup = {}
    const builtins = getBuiltins()
    for (const type of builtins) {
        rv[type.abiName] = type
    }
    for (const type of additional) {
        Iif (!type.abiName) {
            throw new Error('Invalid type')
        }
        rv[type.abiName] = type
    }
    return rv
}
 
export function getTypeName(object: any): string | undefined {
    if (object.constructor && object.constructor.abiName !== undefined) {
        return object.constructor.abiName
    }
    Iif (Array.isArray(object)) {
        const types = object.map(getTypeName)
        const type = types[0]
        if (!type || !types.every((t) => t === type)) {
            return
        }
        return type + '[]'
    }
    switch (typeof object) {
        case 'boolean':
            return 'bool'
        case 'string':
            return 'string'
    }
}
 
export function getType(object: any, name = 'jsobj'): ABISerializableConstructor | undefined {
    if (object.constructor && object.constructor.abiName !== undefined) {
        return object.constructor
    }
    if (Array.isArray(object)) {
        // check for array of all ABISerializableType with same type name
        const types = object.map((v) => {
            return getType(v, name)
        })
        const type = types[0]
        if (!type) {
            return // some type not known
        }
        if (!types.every((t) => t && t.abiName === type.abiName)) {
            return // not all types are the same
        }
        return type
    }
    const objectType = typeof object
    if (objectType === 'object' && object !== null) {
        const fields: ABIField[] = Object.keys(object).map((key) => {
            return {name: key, type: getType(object[key], name + '_nested')!}
        })
        if (fields.find((field) => !field.type)) {
            return // encountered unknown type
        }
        return class extends Struct {
            static abiName = name
            static abiFields = fields
        }
    }
    switch (objectType) {
        case 'boolean':
            return BoolType as ABISerializableConstructor
        case 'string':
            return StringType as ABISerializableConstructor
    }
}