Class: TridentAssistant::Utils::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/trident_assistant/utils/metadata.rb

Overview

build metadata of NFT

Defined Under Namespace

Classes: InvalidFormatError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ Metadata

Returns a new instance of Metadata.



11
12
13
14
15
16
# File 'lib/trident_assistant/utils/metadata.rb', line 11

def initialize(**kwargs)
  @creator = kwargs[:creator] || {}
  @collection = kwargs[:collection] || {}
  @token = kwargs[:token] || {}
  @checksum = kwargs[:checksum] || {}
end

Instance Attribute Details

#checksumObject (readonly)

Returns the value of attribute checksum.



9
10
11
# File 'lib/trident_assistant/utils/metadata.rb', line 9

def checksum
  @checksum
end

#collectionObject (readonly)

Returns the value of attribute collection.



9
10
11
# File 'lib/trident_assistant/utils/metadata.rb', line 9

def collection
  @collection
end

#creatorObject (readonly)

Returns the value of attribute creator.



9
10
11
# File 'lib/trident_assistant/utils/metadata.rb', line 9

def creator
  @creator
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/trident_assistant/utils/metadata.rb', line 9

def token
  @token
end

Instance Method Details

#all_hash_valid?Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/trident_assistant/utils/metadata.rb', line 76

def all_hash_valid?
  checksum["fields"].each do |field|
    value = json

    field.split(".").each do |key|
      next unless value.is_a?(Hash)

      return false if key == "hash" && value["hash"] != TridentAssistant::Utils.hash_from_url(value["url"])

      value = value[key]
    end
  end

  true
end

#checksum_contentObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/trident_assistant/utils/metadata.rb', line 92

def checksum_content
  return unless checksum_valid?

  checksum["fields"].map do |field|
    value = json
    field.split(".").each do |key|
      value = (value[key] if value.is_a?(Hash))
    end
    value.to_s
  end.join
end

#checksum_valid?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
# File 'lib/trident_assistant/utils/metadata.rb', line 69

def checksum_valid?
  return false unless checksum.is_a?(Hash)

  @checksum = checksum.with_indifferent_access
  checksum["algorithm"] && checksum["fields"].is_a?(Array)
end

#collection_valid?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
# File 'lib/trident_assistant/utils/metadata.rb', line 48

def collection_valid?
  return false unless collection.is_a?(Hash)

  @collection = collection.with_indifferent_access
  return false unless collection["id"].match?(/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/)
  return false if collection["name"].blank?

  true
end

#creator_valid?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/trident_assistant/utils/metadata.rb', line 41

def creator_valid?
  return false unless creator.is_a?(Hash)

  @creator = creator.with_indifferent_access
  creator["id"].present? && creator["name"].present?
end

#jsonObject



18
19
20
21
22
23
24
25
# File 'lib/trident_assistant/utils/metadata.rb', line 18

def json
  {
    creator: creator,
    collection: collection,
    token: token,
    checksum: checksum
  }.compact.with_indifferent_access
end

#metahashObject



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/trident_assistant/utils/metadata.rb', line 104

def metahash
  return unless valid?

  alg = checksum["algorithm"]
  case alg
  when "sha256", "sha3-256"
    SHA3::Digest::SHA256.hexdigest checksum_content
  else
    raise "algorithm #{alg} not supported!"
  end
end

#token_valid?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
# File 'lib/trident_assistant/utils/metadata.rb', line 58

def token_valid?
  return false unless token.is_a?(Hash)

  @token = token.with_indifferent_access
  return false unless token["id"].match?(/\A(?!0)\d+\z/)
  return false if token["id"].to_i > 8**64
  return false if token["name"].blank?

  true
end

#valid?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/trident_assistant/utils/metadata.rb', line 37

def valid?
  creator_valid? && collection_valid? && token_valid? && checksum_valid? && all_hash_valid?
end

#validate!Object

Raises:



27
28
29
30
31
32
33
34
35
# File 'lib/trident_assistant/utils/metadata.rb', line 27

def validate!
  raise InvalidFormatError, creator unless creator_valid?
  raise InvalidFormatError, collection unless collection_valid?
  raise InvalidFormatError, token unless token_valid?
  raise InvalidFormatError, checksum unless checksum_valid?
  raise InvalidFormatError, "failed to validate hash" unless all_hash_valid?

  true
end