Skip to content

Commit

Permalink
Add json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel committed Jun 22, 2022
1 parent 2601305 commit dacb2cd
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/archethic/mining/pending_transaction_validation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,29 @@ defmodule Archethic.Mining.PendingTransactionValidation do
type: :nft,
data: %TransactionData{content: content}
}) do
if Regex.match?(~r/(?<=initial supply:).*\d/mi, content) do
schema =
:archethic
|> Application.app_dir("priv/json-schemas/token-core.json")
|> File.read!()
|> Jason.decode!()
|> ExJsonSchema.Schema.resolve()

with {:ok, json_token} <- Jason.decode(content),
:ok <- ExJsonSchema.Validator.validate(schema, json_token),
%{"type" => "non-fungible", "supply" => supply, "properties" => properties}
when length(properties) == supply <- json_token do
:ok
else
{:error, "Invalid NFT content"}
{:error, reason} ->
Logger.debug("Invalid NFT token specification: #{inspect(reason)}")
{:error, "Invalid NFT transaction - Invalid specification"}

%{"type" => "fungible"} ->
:ok

%{"type" => "non-fungible"} ->
{:error,
"Invalid NFT transaction - Supply should match properties for non-fungible tokens"}
end
end

Expand Down
51 changes: 51 additions & 0 deletions priv/json-schemas/token-core.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"supply": {
"type": "integer",
"description": "Number of tokens to create",
"exclusiveMinimum": 0
},
"type": {
"type": "string",
"pattern": "fungible|non-fungible",
"description": "Type of token to create"
},
"name": {
"type": "string",
"description": "Name of the token"
},
"symbol": {
"type": "string",
"description": "Symbol of the token"
},
"properties": {
"description": "List of all the token properties (each one being for a token item)",
"type": "array",
"items": { "$ref": "#/$defs/property-list" }
}
},
"required": [ "supply", "type" ],
"$defs": {
"property-list": {
"type": "array",
"description": "List of the properties for a given token item",
"items": { "$ref": "#/$defs/property" }
},
"property": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Name of the property"
},
"value": {
"type": ["number", "string"],
"description": "Value of the property"
}
},
"required": ["name", "value"]
}
}
}
33 changes: 33 additions & 0 deletions test/archethic/mining/pending_transaction_validation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -314,5 +314,38 @@ defmodule Archethic.Mining.PendingTransactionValidationTest do

assert :ok = PendingTransactionValidation.validate(tx)
end

test "should return :ok when a transaction contains valid fields for NFT creation" do
tx_seed = :crypto.strong_rand_bytes(32)

tx =
Transaction.new(
:nft,
%TransactionData{
content:
Jason.encode!(%{
supply: 3,
name: "MyToken",
type: "fungible",
symbol: "MTK",
properties: [
[
%{name: "image", value: "link"}
],
[
%{name: "image", value: "link"}
],
[
%{name: "image", value: "link"}
]
]
})
},
tx_seed,
0
)

assert :ok = PendingTransactionValidation.validate(tx)
end
end
end

0 comments on commit dacb2cd

Please sign in to comment.