From 44974aa5322524fb896e9daa988aa2a2a4436000 Mon Sep 17 00:00:00 2001 From: ueyamamasashi Date: Sat, 15 Jan 2022 13:56:53 +0900 Subject: [PATCH 1/7] add: error&frontend connect to json --- dfx.json | 15 +- package-lock.json | 85 +++------- package.json | 13 +- src/declarations/small_nft/index.js | 38 +++++ src/declarations/small_nft/small_nft.did | 40 +++++ src/declarations/small_nft/small_nft.did.d.ts | 21 +++ src/declarations/small_nft/small_nft.did.js | 26 +++ .../small_nft_assets/assetstorage.did | 140 ++++++++++++++++ src/declarations/small_nft_assets/index.js | 38 +++++ .../small_nft_assets/small_nft_assets.did | 140 ++++++++++++++++ .../small_nft_assets.did.d.ts | 117 +++++++++++++ .../small_nft_assets/small_nft_assets.did.js | 155 ++++++++++++++++++ src/small_nft/main.mo | 33 +++- 13 files changed, 784 insertions(+), 77 deletions(-) create mode 100644 src/declarations/small_nft/index.js create mode 100644 src/declarations/small_nft/small_nft.did create mode 100644 src/declarations/small_nft/small_nft.did.d.ts create mode 100644 src/declarations/small_nft/small_nft.did.js create mode 100644 src/declarations/small_nft_assets/assetstorage.did create mode 100644 src/declarations/small_nft_assets/index.js create mode 100644 src/declarations/small_nft_assets/small_nft_assets.did create mode 100644 src/declarations/small_nft_assets/small_nft_assets.did.d.ts create mode 100644 src/declarations/small_nft_assets/small_nft_assets.did.js diff --git a/dfx.json b/dfx.json index e86aa5b..04f7307 100644 --- a/dfx.json +++ b/dfx.json @@ -3,6 +3,19 @@ "small_nft": { "main": "src/small_nft/main.mo", "type": "motoko" + }, + "small_nft_assets": { + "dependencies": [ + "small_nft" + ], + "frontend": { + "entrypoint": "src/small_nft_assets/src/index.html" + }, + "source": [ + "src/small_nft_assets/assets", + "dist/small_nft_assets/" + ], + "type": "assets" } }, "defaults": { @@ -14,7 +27,7 @@ "dfx": "0.8.1", "networks": { "local": { - "bind": "127.0.0.1:8000", + "bind": "127.0.0.1:8020", "type": "ephemeral" } }, diff --git a/package-lock.json b/package-lock.json index ba8c5c2..8f81c19 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,8 +21,8 @@ "terser-webpack-plugin": "5.1.1", "util": "0.12.3", "webpack": "5.24.4", - "webpack-cli": "4.5.0", - "webpack-dev-server": "^3.11.2" + "webpack-cli": "^4.9.0", + "webpack-dev-server": "^3.11.3" } }, "node_modules/@dfinity/agent": { @@ -375,15 +375,6 @@ "ajv": "^6.9.1" } }, - "node_modules/ansi-colors": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.1.tgz", - "integrity": "sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==", - "dev": true, - "engines": { - "node": ">=6" - } - }, "node_modules/ansi-html-community": { "version": "0.0.8", "resolved": "https://registry.npmjs.org/ansi-html-community/-/ansi-html-community-0.0.8.tgz", @@ -1386,9 +1377,9 @@ "dev": true }, "node_modules/colorette": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/colorette/-/colorette-1.4.0.tgz", - "integrity": "sha512-Y2oEozpomLn7Q3HFP7dpww7AtMJplbM9lGZP6RDfHqmbeRjiwRg4n6VM6j4KLmRke85uWEI7JqF17f3pqdRA0g==", + "version": "2.0.16", + "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.16.tgz", + "integrity": "sha512-hUewv7oMjCp+wkBv5Rm0v87eJhq4woh5rSR+42YSQJKecCqgIqNkZ6lAlQms/BwHPJA5NKMRlpxPRv0n8HQW6g==", "dev": true }, "node_modules/commander": { @@ -2036,18 +2027,6 @@ "node": ">=10.13.0" } }, - "node_modules/enquirer": { - "version": "2.3.6", - "resolved": "https://registry.npmjs.org/enquirer/-/enquirer-2.3.6.tgz", - "integrity": "sha512-yjNnPr315/FjS4zIsUxYguYUPP2e1NK4d7E7ZOLiyYCcbFBiTMyID+2wvm2w6+pZ/odMA7cRkjhsPbltwBOrLg==", - "dev": true, - "dependencies": { - "ansi-colors": "^4.1.1" - }, - "engines": { - "node": ">=8.6" - } - }, "node_modules/entities": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/entities/-/entities-2.2.0.tgz", @@ -6752,18 +6731,17 @@ } }, "node_modules/webpack-cli": { - "version": "4.5.0", - "resolved": "https://registry.npmjs.org/webpack-cli/-/webpack-cli-4.5.0.tgz", - "integrity": "sha512-wXg/ef6Ibstl2f50mnkcHblRPN/P9J4Nlod5Hg9HGFgSeF8rsqDGHJeVe4aR26q9l62TUJi6vmvC2Qz96YJw1Q==", + "version": "4.9.0", + "resolved": "https://registry.npmjs.org/webpack-cli/-/webpack-cli-4.9.0.tgz", + "integrity": "sha512-n/jZZBMzVEl4PYIBs+auy2WI0WTQ74EnJDiyD98O2JZY6IVIHJNitkYp/uTXOviIOMfgzrNvC9foKv/8o8KSZw==", "dev": true, "dependencies": { "@discoveryjs/json-ext": "^0.5.0", - "@webpack-cli/configtest": "^1.0.1", - "@webpack-cli/info": "^1.2.2", - "@webpack-cli/serve": "^1.3.0", - "colorette": "^1.2.1", + "@webpack-cli/configtest": "^1.1.0", + "@webpack-cli/info": "^1.4.0", + "@webpack-cli/serve": "^1.6.0", + "colorette": "^2.0.14", "commander": "^7.0.0", - "enquirer": "^2.3.6", "execa": "^5.0.0", "fastest-levenshtein": "^1.0.12", "import-local": "^3.0.2", @@ -6785,9 +6763,6 @@ "@webpack-cli/generators": { "optional": true }, - "@webpack-cli/init": { - "optional": true - }, "@webpack-cli/migrate": { "optional": true }, @@ -7695,12 +7670,6 @@ "dev": true, "requires": {} }, - "ansi-colors": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.1.tgz", - "integrity": "sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==", - "dev": true - }, "ansi-html-community": { "version": "0.0.8", "resolved": "https://registry.npmjs.org/ansi-html-community/-/ansi-html-community-0.0.8.tgz", @@ -8490,9 +8459,9 @@ "dev": true }, "colorette": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/colorette/-/colorette-1.4.0.tgz", - "integrity": "sha512-Y2oEozpomLn7Q3HFP7dpww7AtMJplbM9lGZP6RDfHqmbeRjiwRg4n6VM6j4KLmRke85uWEI7JqF17f3pqdRA0g==", + "version": "2.0.16", + "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.16.tgz", + "integrity": "sha512-hUewv7oMjCp+wkBv5Rm0v87eJhq4woh5rSR+42YSQJKecCqgIqNkZ6lAlQms/BwHPJA5NKMRlpxPRv0n8HQW6g==", "dev": true }, "commander": { @@ -9007,15 +8976,6 @@ "tapable": "^2.2.0" } }, - "enquirer": { - "version": "2.3.6", - "resolved": "https://registry.npmjs.org/enquirer/-/enquirer-2.3.6.tgz", - "integrity": "sha512-yjNnPr315/FjS4zIsUxYguYUPP2e1NK4d7E7ZOLiyYCcbFBiTMyID+2wvm2w6+pZ/odMA7cRkjhsPbltwBOrLg==", - "dev": true, - "requires": { - "ansi-colors": "^4.1.1" - } - }, "entities": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/entities/-/entities-2.2.0.tgz", @@ -12704,18 +12664,17 @@ } }, "webpack-cli": { - "version": "4.5.0", - "resolved": "https://registry.npmjs.org/webpack-cli/-/webpack-cli-4.5.0.tgz", - "integrity": "sha512-wXg/ef6Ibstl2f50mnkcHblRPN/P9J4Nlod5Hg9HGFgSeF8rsqDGHJeVe4aR26q9l62TUJi6vmvC2Qz96YJw1Q==", + "version": "4.9.0", + "resolved": "https://registry.npmjs.org/webpack-cli/-/webpack-cli-4.9.0.tgz", + "integrity": "sha512-n/jZZBMzVEl4PYIBs+auy2WI0WTQ74EnJDiyD98O2JZY6IVIHJNitkYp/uTXOviIOMfgzrNvC9foKv/8o8KSZw==", "dev": true, "requires": { "@discoveryjs/json-ext": "^0.5.0", - "@webpack-cli/configtest": "^1.0.1", - "@webpack-cli/info": "^1.2.2", - "@webpack-cli/serve": "^1.3.0", - "colorette": "^1.2.1", + "@webpack-cli/configtest": "^1.1.0", + "@webpack-cli/info": "^1.4.0", + "@webpack-cli/serve": "^1.6.0", + "colorette": "^2.0.14", "commander": "^7.0.0", - "enquirer": "^2.3.6", "execa": "^5.0.0", "fastest-levenshtein": "^1.0.12", "import-local": "^3.0.2", diff --git a/package.json b/package.json index c47a47d..b4f8f05 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,12 @@ "name": "small_nft_assets", "version": "0.1.0", "description": "Internet Computer starter application", - "keywords": ["Internet Computer", "Motoko", "JavaScript", "Canister"], + "keywords": [ + "Internet Computer", + "Motoko", + "JavaScript", + "Canister" + ], "scripts": { "build": "webpack", "prebuild": "npm run copy:types", @@ -23,9 +28,9 @@ "stream-browserify": "3.0.0", "terser-webpack-plugin": "5.1.1", "util": "0.12.3", - "webpack-cli": "4.5.0", - "webpack-dev-server": "^3.11.2", - "webpack": "5.24.4" + "webpack": "5.24.4", + "webpack-cli": "^4.9.0", + "webpack-dev-server": "^3.11.3" }, "browserslist": [ "last 2 chrome version", diff --git a/src/declarations/small_nft/index.js b/src/declarations/small_nft/index.js new file mode 100644 index 0000000..84d7f45 --- /dev/null +++ b/src/declarations/small_nft/index.js @@ -0,0 +1,38 @@ +import { Actor, HttpAgent } from "@dfinity/agent"; + +// Imports and re-exports candid interface +import { idlFactory } from './small_nft.did.js'; +export { idlFactory } from './small_nft.did.js'; +// CANISTER_ID is replaced by webpack based on node environment +export const canisterId = process.env.SMALL_NFT_CANISTER_ID; + +/** + * + * @param {string | import("@dfinity/principal").Principal} canisterId Canister ID of Agent + * @param {{agentOptions?: import("@dfinity/agent").HttpAgentOptions; actorOptions?: import("@dfinity/agent").ActorConfig}} [options] + * @return {import("@dfinity/agent").ActorSubclass} + */ + export const createActor = (canisterId, options) => { + const agent = new HttpAgent({ ...options?.agentOptions }); + + // Fetch root key for certificate validation during development + if(process.env.NODE_ENV !== "production") { + agent.fetchRootKey().catch(err=>{ + console.warn("Unable to fetch root key. Check to ensure that your local replica is running"); + console.error(err); + }); + } + + // Creates an actor with using the candid interface and the HttpAgent + return Actor.createActor(idlFactory, { + agent, + canisterId, + ...options?.actorOptions, + }); +}; + +/** + * A ready-to-use agent for the small_nft canister + * @type {import("@dfinity/agent").ActorSubclass} + */ + export const small_nft = createActor(canisterId); diff --git a/src/declarations/small_nft/small_nft.did b/src/declarations/small_nft/small_nft.did new file mode 100644 index 0000000..03a71ed --- /dev/null +++ b/src/declarations/small_nft/small_nft.did @@ -0,0 +1,40 @@ +type TokenMetadata = record {attributes: opt vec Attribute;}; +type TokenID = nat; +type Small_NFT = + service { + latestTokenID: () -> (Result_2) query; + mint: (principal, opt TokenMetadata) -> (TokenID); + ownerOf: (TokenID) -> (Result_1) query; + transfer: (principal, TokenID) -> (Result); + }; +type Result_2 = + variant { + err: text; + ok: nat; + }; +type Result_1 = + variant { + err: Errors; + ok: text; + }; +type Result = + variant { + err: Errors; + ok: Okays; + }; +type Okays = + variant { + IsSuccess: nat; + canTransfer: text; + }; +type Errors = + variant { + alreadyExist; + notFoundTokenInfo; + }; +type Attribute = + record { + key: text; + value: text; + }; +service : () -> Small_NFT diff --git a/src/declarations/small_nft/small_nft.did.d.ts b/src/declarations/small_nft/small_nft.did.d.ts new file mode 100644 index 0000000..7b7403e --- /dev/null +++ b/src/declarations/small_nft/small_nft.did.d.ts @@ -0,0 +1,21 @@ +import type { Principal } from '@dfinity/principal'; +export interface Attribute { 'key' : string, 'value' : string } +export type Errors = { 'notFoundTokenInfo' : null } | + { 'alreadyExist' : null }; +export type Okays = { 'canTransfer' : string } | + { 'IsSuccess' : bigint }; +export type Result = { 'ok' : Okays } | + { 'err' : Errors }; +export type Result_1 = { 'ok' : string } | + { 'err' : Errors }; +export type Result_2 = { 'ok' : bigint } | + { 'err' : string }; +export interface Small_NFT { + 'latestTokenID' : () => Promise, + 'mint' : (arg_0: Principal, arg_1: [] | [TokenMetadata]) => Promise, + 'ownerOf' : (arg_0: TokenID) => Promise, + 'transfer' : (arg_0: Principal, arg_1: TokenID) => Promise, +} +export type TokenID = bigint; +export interface TokenMetadata { 'attributes' : [] | [Array] } +export interface _SERVICE extends Small_NFT {} diff --git a/src/declarations/small_nft/small_nft.did.js b/src/declarations/small_nft/small_nft.did.js new file mode 100644 index 0000000..2d24cdc --- /dev/null +++ b/src/declarations/small_nft/small_nft.did.js @@ -0,0 +1,26 @@ +export const idlFactory = ({ IDL }) => { + const Result_2 = IDL.Variant({ 'ok' : IDL.Nat, 'err' : IDL.Text }); + const Attribute = IDL.Record({ 'key' : IDL.Text, 'value' : IDL.Text }); + const TokenMetadata = IDL.Record({ + 'attributes' : IDL.Opt(IDL.Vec(Attribute)), + }); + const TokenID = IDL.Nat; + const Errors = IDL.Variant({ + 'notFoundTokenInfo' : IDL.Null, + 'alreadyExist' : IDL.Null, + }); + const Result_1 = IDL.Variant({ 'ok' : IDL.Text, 'err' : Errors }); + const Okays = IDL.Variant({ + 'canTransfer' : IDL.Text, + 'IsSuccess' : IDL.Nat, + }); + const Result = IDL.Variant({ 'ok' : Okays, 'err' : Errors }); + const Small_NFT = IDL.Service({ + 'latestTokenID' : IDL.Func([], [Result_2], ['query']), + 'mint' : IDL.Func([IDL.Principal, IDL.Opt(TokenMetadata)], [TokenID], []), + 'ownerOf' : IDL.Func([TokenID], [Result_1], ['query']), + 'transfer' : IDL.Func([IDL.Principal, TokenID], [Result], []), + }); + return Small_NFT; +}; +export const init = ({ IDL }) => { return []; }; diff --git a/src/declarations/small_nft_assets/assetstorage.did b/src/declarations/small_nft_assets/assetstorage.did new file mode 100644 index 0000000..d11ecd9 --- /dev/null +++ b/src/declarations/small_nft_assets/assetstorage.did @@ -0,0 +1,140 @@ +type BatchId = nat; +type ChunkId = nat; +type Key = text; +type Time = int; + +type CreateAssetArguments = record { + key: Key; + content_type: text; +}; + +// Add or change content for an asset, by content encoding +type SetAssetContentArguments = record { + key: Key; + content_encoding: text; + chunk_ids: vec ChunkId; + sha256: opt blob; +}; + +// Remove content for an asset, by content encoding +type UnsetAssetContentArguments = record { + key: Key; + content_encoding: text; +}; + +// Delete an asset +type DeleteAssetArguments = record { + key: Key; +}; + +// Reset everything +type ClearArguments = record {}; + +type BatchOperationKind = variant { + CreateAsset: CreateAssetArguments; + SetAssetContent: SetAssetContentArguments; + + UnsetAssetContent: UnsetAssetContentArguments; + DeleteAsset: DeleteAssetArguments; + + Clear: ClearArguments; +}; + +type HeaderField = record { text; text; }; + +type HttpRequest = record { + method: text; + url: text; + headers: vec HeaderField; + body: blob; +}; + +type HttpResponse = record { + status_code: nat16; + headers: vec HeaderField; + body: blob; + streaming_strategy: opt StreamingStrategy; +}; + +type StreamingCallbackHttpResponse = record { + body: blob; + token: opt StreamingCallbackToken; +}; + +type StreamingCallbackToken = record { + key: Key; + content_encoding: text; + index: nat; + sha256: opt blob; +}; + +type StreamingStrategy = variant { + Callback: record { + callback: func (StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; + token: StreamingCallbackToken; + }; +}; + +service: { + + get: (record { + key: Key; + accept_encodings: vec text; + }) -> (record { + content: blob; // may be the entirety of the content, or just chunk index 0 + content_type: text; + content_encoding: text; + sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments + total_length: nat; // all chunks except last have size == content.size() + }) query; + + // if get() returned chunks > 1, call this to retrieve them. + // chunks may or may not be split up at the same boundaries as presented to create_chunk(). + get_chunk: (record { + key: Key; + content_encoding: text; + index: nat; + sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments + }) -> (record { content: blob }) query; + + list : (record {}) -> (vec record { + key: Key; + content_type: text; + encodings: vec record { + content_encoding: text; + sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments + length: nat; // Size of this encoding's blob. Calculated when uploading assets. + modified: Time; + }; + }) query; + + create_batch : (record {}) -> (record { batch_id: BatchId }); + + create_chunk: (record { batch_id: BatchId; content: blob }) -> (record { chunk_id: ChunkId }); + + // Perform all operations successfully, or reject + commit_batch: (record { batch_id: BatchId; operations: vec BatchOperationKind }) -> (); + + create_asset: (CreateAssetArguments) -> (); + set_asset_content: (SetAssetContentArguments) -> (); + unset_asset_content: (UnsetAssetContentArguments) -> (); + + delete_asset: (DeleteAssetArguments) -> (); + + clear: (ClearArguments) -> (); + + // Single call to create an asset with content for a single content encoding that + // fits within the message ingress limit. + store: (record { + key: Key; + content_type: text; + content_encoding: text; + content: blob; + sha256: opt blob + }) -> (); + + http_request: (request: HttpRequest) -> (HttpResponse) query; + http_request_streaming_callback: (token: StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; + + authorize: (principal) -> (); +} diff --git a/src/declarations/small_nft_assets/index.js b/src/declarations/small_nft_assets/index.js new file mode 100644 index 0000000..6905dc8 --- /dev/null +++ b/src/declarations/small_nft_assets/index.js @@ -0,0 +1,38 @@ +import { Actor, HttpAgent } from "@dfinity/agent"; + +// Imports and re-exports candid interface +import { idlFactory } from './small_nft_assets.did.js'; +export { idlFactory } from './small_nft_assets.did.js'; +// CANISTER_ID is replaced by webpack based on node environment +export const canisterId = process.env.SMALL_NFT_ASSETS_CANISTER_ID; + +/** + * + * @param {string | import("@dfinity/principal").Principal} canisterId Canister ID of Agent + * @param {{agentOptions?: import("@dfinity/agent").HttpAgentOptions; actorOptions?: import("@dfinity/agent").ActorConfig}} [options] + * @return {import("@dfinity/agent").ActorSubclass} + */ + export const createActor = (canisterId, options) => { + const agent = new HttpAgent({ ...options?.agentOptions }); + + // Fetch root key for certificate validation during development + if(process.env.NODE_ENV !== "production") { + agent.fetchRootKey().catch(err=>{ + console.warn("Unable to fetch root key. Check to ensure that your local replica is running"); + console.error(err); + }); + } + + // Creates an actor with using the candid interface and the HttpAgent + return Actor.createActor(idlFactory, { + agent, + canisterId, + ...options?.actorOptions, + }); +}; + +/** + * A ready-to-use agent for the small_nft_assets canister + * @type {import("@dfinity/agent").ActorSubclass} + */ + export const small_nft_assets = createActor(canisterId); diff --git a/src/declarations/small_nft_assets/small_nft_assets.did b/src/declarations/small_nft_assets/small_nft_assets.did new file mode 100644 index 0000000..d11ecd9 --- /dev/null +++ b/src/declarations/small_nft_assets/small_nft_assets.did @@ -0,0 +1,140 @@ +type BatchId = nat; +type ChunkId = nat; +type Key = text; +type Time = int; + +type CreateAssetArguments = record { + key: Key; + content_type: text; +}; + +// Add or change content for an asset, by content encoding +type SetAssetContentArguments = record { + key: Key; + content_encoding: text; + chunk_ids: vec ChunkId; + sha256: opt blob; +}; + +// Remove content for an asset, by content encoding +type UnsetAssetContentArguments = record { + key: Key; + content_encoding: text; +}; + +// Delete an asset +type DeleteAssetArguments = record { + key: Key; +}; + +// Reset everything +type ClearArguments = record {}; + +type BatchOperationKind = variant { + CreateAsset: CreateAssetArguments; + SetAssetContent: SetAssetContentArguments; + + UnsetAssetContent: UnsetAssetContentArguments; + DeleteAsset: DeleteAssetArguments; + + Clear: ClearArguments; +}; + +type HeaderField = record { text; text; }; + +type HttpRequest = record { + method: text; + url: text; + headers: vec HeaderField; + body: blob; +}; + +type HttpResponse = record { + status_code: nat16; + headers: vec HeaderField; + body: blob; + streaming_strategy: opt StreamingStrategy; +}; + +type StreamingCallbackHttpResponse = record { + body: blob; + token: opt StreamingCallbackToken; +}; + +type StreamingCallbackToken = record { + key: Key; + content_encoding: text; + index: nat; + sha256: opt blob; +}; + +type StreamingStrategy = variant { + Callback: record { + callback: func (StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; + token: StreamingCallbackToken; + }; +}; + +service: { + + get: (record { + key: Key; + accept_encodings: vec text; + }) -> (record { + content: blob; // may be the entirety of the content, or just chunk index 0 + content_type: text; + content_encoding: text; + sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments + total_length: nat; // all chunks except last have size == content.size() + }) query; + + // if get() returned chunks > 1, call this to retrieve them. + // chunks may or may not be split up at the same boundaries as presented to create_chunk(). + get_chunk: (record { + key: Key; + content_encoding: text; + index: nat; + sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments + }) -> (record { content: blob }) query; + + list : (record {}) -> (vec record { + key: Key; + content_type: text; + encodings: vec record { + content_encoding: text; + sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments + length: nat; // Size of this encoding's blob. Calculated when uploading assets. + modified: Time; + }; + }) query; + + create_batch : (record {}) -> (record { batch_id: BatchId }); + + create_chunk: (record { batch_id: BatchId; content: blob }) -> (record { chunk_id: ChunkId }); + + // Perform all operations successfully, or reject + commit_batch: (record { batch_id: BatchId; operations: vec BatchOperationKind }) -> (); + + create_asset: (CreateAssetArguments) -> (); + set_asset_content: (SetAssetContentArguments) -> (); + unset_asset_content: (UnsetAssetContentArguments) -> (); + + delete_asset: (DeleteAssetArguments) -> (); + + clear: (ClearArguments) -> (); + + // Single call to create an asset with content for a single content encoding that + // fits within the message ingress limit. + store: (record { + key: Key; + content_type: text; + content_encoding: text; + content: blob; + sha256: opt blob + }) -> (); + + http_request: (request: HttpRequest) -> (HttpResponse) query; + http_request_streaming_callback: (token: StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; + + authorize: (principal) -> (); +} diff --git a/src/declarations/small_nft_assets/small_nft_assets.did.d.ts b/src/declarations/small_nft_assets/small_nft_assets.did.d.ts new file mode 100644 index 0000000..990bf87 --- /dev/null +++ b/src/declarations/small_nft_assets/small_nft_assets.did.d.ts @@ -0,0 +1,117 @@ +import type { Principal } from '@dfinity/principal'; +export type BatchId = bigint; +export type BatchOperationKind = { 'CreateAsset' : CreateAssetArguments } | + { 'UnsetAssetContent' : UnsetAssetContentArguments } | + { 'DeleteAsset' : DeleteAssetArguments } | + { 'SetAssetContent' : SetAssetContentArguments } | + { 'Clear' : ClearArguments }; +export type ChunkId = bigint; +export type ClearArguments = {}; +export interface CreateAssetArguments { 'key' : Key, 'content_type' : string } +export interface DeleteAssetArguments { 'key' : Key } +export type HeaderField = [string, string]; +export interface HttpRequest { + 'url' : string, + 'method' : string, + 'body' : Array, + 'headers' : Array, +} +export interface HttpResponse { + 'body' : Array, + 'headers' : Array, + 'streaming_strategy' : [] | [StreamingStrategy], + 'status_code' : number, +} +export type Key = string; +export interface SetAssetContentArguments { + 'key' : Key, + 'sha256' : [] | [Array], + 'chunk_ids' : Array, + 'content_encoding' : string, +} +export interface StreamingCallbackHttpResponse { + 'token' : [] | [StreamingCallbackToken], + 'body' : Array, +} +export interface StreamingCallbackToken { + 'key' : Key, + 'sha256' : [] | [Array], + 'index' : bigint, + 'content_encoding' : string, +} +export type StreamingStrategy = { + 'Callback' : { + 'token' : StreamingCallbackToken, + 'callback' : [Principal, string], + } + }; +export type Time = bigint; +export interface UnsetAssetContentArguments { + 'key' : Key, + 'content_encoding' : string, +} +export interface _SERVICE { + 'authorize' : (arg_0: Principal) => Promise, + 'clear' : (arg_0: ClearArguments) => Promise, + 'commit_batch' : ( + arg_0: { 'batch_id' : BatchId, 'operations' : Array }, + ) => Promise, + 'create_asset' : (arg_0: CreateAssetArguments) => Promise, + 'create_batch' : (arg_0: {}) => Promise<{ 'batch_id' : BatchId }>, + 'create_chunk' : ( + arg_0: { 'content' : Array, 'batch_id' : BatchId }, + ) => Promise<{ 'chunk_id' : ChunkId }>, + 'delete_asset' : (arg_0: DeleteAssetArguments) => Promise, + 'get' : ( + arg_0: { 'key' : Key, 'accept_encodings' : Array }, + ) => Promise< + { + 'content' : Array, + 'sha256' : [] | [Array], + 'content_type' : string, + 'content_encoding' : string, + 'total_length' : bigint, + } + >, + 'get_chunk' : ( + arg_0: { + 'key' : Key, + 'sha256' : [] | [Array], + 'index' : bigint, + 'content_encoding' : string, + }, + ) => Promise<{ 'content' : Array }>, + 'http_request' : (arg_0: HttpRequest) => Promise, + 'http_request_streaming_callback' : ( + arg_0: StreamingCallbackToken, + ) => Promise<[] | [StreamingCallbackHttpResponse]>, + 'list' : (arg_0: {}) => Promise< + Array< + { + 'key' : Key, + 'encodings' : Array< + { + 'modified' : Time, + 'sha256' : [] | [Array], + 'length' : bigint, + 'content_encoding' : string, + } + >, + 'content_type' : string, + } + > + >, + 'set_asset_content' : (arg_0: SetAssetContentArguments) => Promise, + 'store' : ( + arg_0: { + 'key' : Key, + 'content' : Array, + 'sha256' : [] | [Array], + 'content_type' : string, + 'content_encoding' : string, + }, + ) => Promise, + 'unset_asset_content' : (arg_0: UnsetAssetContentArguments) => Promise< + undefined + >, +} diff --git a/src/declarations/small_nft_assets/small_nft_assets.did.js b/src/declarations/small_nft_assets/small_nft_assets.did.js new file mode 100644 index 0000000..46f975e --- /dev/null +++ b/src/declarations/small_nft_assets/small_nft_assets.did.js @@ -0,0 +1,155 @@ +export const idlFactory = ({ IDL }) => { + const ClearArguments = IDL.Record({}); + const BatchId = IDL.Nat; + const Key = IDL.Text; + const CreateAssetArguments = IDL.Record({ + 'key' : Key, + 'content_type' : IDL.Text, + }); + const UnsetAssetContentArguments = IDL.Record({ + 'key' : Key, + 'content_encoding' : IDL.Text, + }); + const DeleteAssetArguments = IDL.Record({ 'key' : Key }); + const ChunkId = IDL.Nat; + const SetAssetContentArguments = IDL.Record({ + 'key' : Key, + 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), + 'chunk_ids' : IDL.Vec(ChunkId), + 'content_encoding' : IDL.Text, + }); + const BatchOperationKind = IDL.Variant({ + 'CreateAsset' : CreateAssetArguments, + 'UnsetAssetContent' : UnsetAssetContentArguments, + 'DeleteAsset' : DeleteAssetArguments, + 'SetAssetContent' : SetAssetContentArguments, + 'Clear' : ClearArguments, + }); + const HeaderField = IDL.Tuple(IDL.Text, IDL.Text); + const HttpRequest = IDL.Record({ + 'url' : IDL.Text, + 'method' : IDL.Text, + 'body' : IDL.Vec(IDL.Nat8), + 'headers' : IDL.Vec(HeaderField), + }); + const StreamingCallbackToken = IDL.Record({ + 'key' : Key, + 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), + 'index' : IDL.Nat, + 'content_encoding' : IDL.Text, + }); + const StreamingCallbackHttpResponse = IDL.Record({ + 'token' : IDL.Opt(StreamingCallbackToken), + 'body' : IDL.Vec(IDL.Nat8), + }); + const StreamingStrategy = IDL.Variant({ + 'Callback' : IDL.Record({ + 'token' : StreamingCallbackToken, + 'callback' : IDL.Func( + [StreamingCallbackToken], + [IDL.Opt(StreamingCallbackHttpResponse)], + ['query'], + ), + }), + }); + const HttpResponse = IDL.Record({ + 'body' : IDL.Vec(IDL.Nat8), + 'headers' : IDL.Vec(HeaderField), + 'streaming_strategy' : IDL.Opt(StreamingStrategy), + 'status_code' : IDL.Nat16, + }); + const Time = IDL.Int; + return IDL.Service({ + 'authorize' : IDL.Func([IDL.Principal], [], []), + 'clear' : IDL.Func([ClearArguments], [], []), + 'commit_batch' : IDL.Func( + [ + IDL.Record({ + 'batch_id' : BatchId, + 'operations' : IDL.Vec(BatchOperationKind), + }), + ], + [], + [], + ), + 'create_asset' : IDL.Func([CreateAssetArguments], [], []), + 'create_batch' : IDL.Func( + [IDL.Record({})], + [IDL.Record({ 'batch_id' : BatchId })], + [], + ), + 'create_chunk' : IDL.Func( + [IDL.Record({ 'content' : IDL.Vec(IDL.Nat8), 'batch_id' : BatchId })], + [IDL.Record({ 'chunk_id' : ChunkId })], + [], + ), + 'delete_asset' : IDL.Func([DeleteAssetArguments], [], []), + 'get' : IDL.Func( + [IDL.Record({ 'key' : Key, 'accept_encodings' : IDL.Vec(IDL.Text) })], + [ + IDL.Record({ + 'content' : IDL.Vec(IDL.Nat8), + 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), + 'content_type' : IDL.Text, + 'content_encoding' : IDL.Text, + 'total_length' : IDL.Nat, + }), + ], + ['query'], + ), + 'get_chunk' : IDL.Func( + [ + IDL.Record({ + 'key' : Key, + 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), + 'index' : IDL.Nat, + 'content_encoding' : IDL.Text, + }), + ], + [IDL.Record({ 'content' : IDL.Vec(IDL.Nat8) })], + ['query'], + ), + 'http_request' : IDL.Func([HttpRequest], [HttpResponse], ['query']), + 'http_request_streaming_callback' : IDL.Func( + [StreamingCallbackToken], + [IDL.Opt(StreamingCallbackHttpResponse)], + ['query'], + ), + 'list' : IDL.Func( + [IDL.Record({})], + [ + IDL.Vec( + IDL.Record({ + 'key' : Key, + 'encodings' : IDL.Vec( + IDL.Record({ + 'modified' : Time, + 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), + 'length' : IDL.Nat, + 'content_encoding' : IDL.Text, + }) + ), + 'content_type' : IDL.Text, + }) + ), + ], + ['query'], + ), + 'set_asset_content' : IDL.Func([SetAssetContentArguments], [], []), + 'store' : IDL.Func( + [ + IDL.Record({ + 'key' : Key, + 'content' : IDL.Vec(IDL.Nat8), + 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), + 'content_type' : IDL.Text, + 'content_encoding' : IDL.Text, + }), + ], + [], + [], + ), + 'unset_asset_content' : IDL.Func([UnsetAssetContentArguments], [], []), + }); +}; +export const init = ({ IDL }) => { return []; }; diff --git a/src/small_nft/main.mo b/src/small_nft/main.mo index 3782cdd..f290e9a 100644 --- a/src/small_nft/main.mo +++ b/src/small_nft/main.mo @@ -14,6 +14,8 @@ import Nat "mo:base/Nat"; // HashMapの第2引数で使用 import Hash "mo:base/Hash"; // HashMapの第3引数で使用 import Iter "mo:base/Iter"; // preupgrade,postupgradeで使う.HashMapのエントリを書き出す. +import Result "mo:base/Result"; // エラー処理用のモジュール(add:by hokosugi) + // `shared(<変数名>)`を加えることで,ICProtocolからこのトランザクションの呼び出し人を受け取ることができる. // `<変数名>.caller`でそのPrincipalを参照できる. @@ -57,6 +59,18 @@ shared(installer) actor class Small_NFT() { private var _tokenRegistry = HashMap.HashMap(1, Nat.equal, Hash.hash); private stable var _latestTokenID: Nat = 0; + //④ エラー処理(add: by hokosugi) + // Resultライブラリから型指定(https://smartcontracts.org/docs/base-libraries/Result.html) + public type Errors = { + #notFoundTokenInfo; + #alreadyExist; // 未使用 + }; + // okの時で関数内で別の型がある時に使う + public type Okays = { + #canTransfer : Text; + #IsSuccess : Nat; + }; + public shared(msg) func mint(to : Principal, metadata : ?TokenMetadata) : async TokenID{ // 最新のtokeIDの更新 @@ -75,7 +89,8 @@ shared(installer) actor class Small_NFT() { return _latestTokenID; }; - public query func ownerOf(tokenId : TokenID) : async Text { //public funcの戻り値はasync型として記述 + + public query func ownerOf(tokenId : TokenID) : async Result.Result { //public funcの戻り値はasync型として記述 // このswitch-case文がmotokoの鬼門 /* HashMapはnull許容型として帰ってくるため,型安全性のため一度Nullを場合分けしなくてはならない. @@ -86,33 +101,33 @@ shared(installer) actor class Small_NFT() { switch(_tokenRegistry.get(tokenId)) { case(?tokenInfo) { //owner - return Principal.toText(tokenInfo.owner); + return #ok(Principal.toText(tokenInfo.owner)); }; case(_){ - return "none"; + return #err(#notFoundTokenInfo); }; }; }; - public shared(msg) func transfer(to : Principal, tokenId : TokenID) : async TokenID { + public shared(msg) func transfer(to : Principal, tokenId : TokenID) : async Result.Result { switch(_tokenRegistry.get(tokenId)) { case(?tokenInfo) { if(tokenInfo.owner != msg.caller ) { - return 0; + return #ok(#canTransfer("yes")); }; //ownerの書き換え tokenInfo.owner := to; // switch-caseは参照渡しなので,これで直接代入できるはず. - return tokenId; + return #ok(#IsSuccess(tokenId)); }; case(_){ - return 0; + return #err(#notFoundTokenInfo); }; }; }; - public query func latestTokenID() : async TokenID { - return _latestTokenID; + public query func latestTokenID() : async Result.Result { + return #ok(_latestTokenID); }; //State functions From 4596ee3d198a31b7d1dbf1bc366de3cf0d67dff6 Mon Sep 17 00:00:00 2001 From: ueyamamasashi Date: Sat, 15 Jan 2022 14:06:00 +0900 Subject: [PATCH 2/7] change: bind 8020 to 8000 --- dfx.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dfx.json b/dfx.json index 04f7307..22dd581 100644 --- a/dfx.json +++ b/dfx.json @@ -27,7 +27,7 @@ "dfx": "0.8.1", "networks": { "local": { - "bind": "127.0.0.1:8020", + "bind": "127.0.0.1:8000", "type": "ephemeral" } }, From 6b019984edb2e7ca46466d9d4366b3f73483391a Mon Sep 17 00:00:00 2001 From: ueyamamasashi Date: Sat, 15 Jan 2022 14:51:37 +0900 Subject: [PATCH 3/7] delete: declarations --- src/declarations/small_nft/index.js | 38 ----- src/declarations/small_nft/small_nft.did | 40 ----- src/declarations/small_nft/small_nft.did.d.ts | 21 --- src/declarations/small_nft/small_nft.did.js | 26 --- .../small_nft_assets/assetstorage.did | 140 ---------------- src/declarations/small_nft_assets/index.js | 38 ----- .../small_nft_assets/small_nft_assets.did | 140 ---------------- .../small_nft_assets.did.d.ts | 117 ------------- .../small_nft_assets/small_nft_assets.did.js | 155 ------------------ 9 files changed, 715 deletions(-) delete mode 100644 src/declarations/small_nft/index.js delete mode 100644 src/declarations/small_nft/small_nft.did delete mode 100644 src/declarations/small_nft/small_nft.did.d.ts delete mode 100644 src/declarations/small_nft/small_nft.did.js delete mode 100644 src/declarations/small_nft_assets/assetstorage.did delete mode 100644 src/declarations/small_nft_assets/index.js delete mode 100644 src/declarations/small_nft_assets/small_nft_assets.did delete mode 100644 src/declarations/small_nft_assets/small_nft_assets.did.d.ts delete mode 100644 src/declarations/small_nft_assets/small_nft_assets.did.js diff --git a/src/declarations/small_nft/index.js b/src/declarations/small_nft/index.js deleted file mode 100644 index 84d7f45..0000000 --- a/src/declarations/small_nft/index.js +++ /dev/null @@ -1,38 +0,0 @@ -import { Actor, HttpAgent } from "@dfinity/agent"; - -// Imports and re-exports candid interface -import { idlFactory } from './small_nft.did.js'; -export { idlFactory } from './small_nft.did.js'; -// CANISTER_ID is replaced by webpack based on node environment -export const canisterId = process.env.SMALL_NFT_CANISTER_ID; - -/** - * - * @param {string | import("@dfinity/principal").Principal} canisterId Canister ID of Agent - * @param {{agentOptions?: import("@dfinity/agent").HttpAgentOptions; actorOptions?: import("@dfinity/agent").ActorConfig}} [options] - * @return {import("@dfinity/agent").ActorSubclass} - */ - export const createActor = (canisterId, options) => { - const agent = new HttpAgent({ ...options?.agentOptions }); - - // Fetch root key for certificate validation during development - if(process.env.NODE_ENV !== "production") { - agent.fetchRootKey().catch(err=>{ - console.warn("Unable to fetch root key. Check to ensure that your local replica is running"); - console.error(err); - }); - } - - // Creates an actor with using the candid interface and the HttpAgent - return Actor.createActor(idlFactory, { - agent, - canisterId, - ...options?.actorOptions, - }); -}; - -/** - * A ready-to-use agent for the small_nft canister - * @type {import("@dfinity/agent").ActorSubclass} - */ - export const small_nft = createActor(canisterId); diff --git a/src/declarations/small_nft/small_nft.did b/src/declarations/small_nft/small_nft.did deleted file mode 100644 index 03a71ed..0000000 --- a/src/declarations/small_nft/small_nft.did +++ /dev/null @@ -1,40 +0,0 @@ -type TokenMetadata = record {attributes: opt vec Attribute;}; -type TokenID = nat; -type Small_NFT = - service { - latestTokenID: () -> (Result_2) query; - mint: (principal, opt TokenMetadata) -> (TokenID); - ownerOf: (TokenID) -> (Result_1) query; - transfer: (principal, TokenID) -> (Result); - }; -type Result_2 = - variant { - err: text; - ok: nat; - }; -type Result_1 = - variant { - err: Errors; - ok: text; - }; -type Result = - variant { - err: Errors; - ok: Okays; - }; -type Okays = - variant { - IsSuccess: nat; - canTransfer: text; - }; -type Errors = - variant { - alreadyExist; - notFoundTokenInfo; - }; -type Attribute = - record { - key: text; - value: text; - }; -service : () -> Small_NFT diff --git a/src/declarations/small_nft/small_nft.did.d.ts b/src/declarations/small_nft/small_nft.did.d.ts deleted file mode 100644 index 7b7403e..0000000 --- a/src/declarations/small_nft/small_nft.did.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -import type { Principal } from '@dfinity/principal'; -export interface Attribute { 'key' : string, 'value' : string } -export type Errors = { 'notFoundTokenInfo' : null } | - { 'alreadyExist' : null }; -export type Okays = { 'canTransfer' : string } | - { 'IsSuccess' : bigint }; -export type Result = { 'ok' : Okays } | - { 'err' : Errors }; -export type Result_1 = { 'ok' : string } | - { 'err' : Errors }; -export type Result_2 = { 'ok' : bigint } | - { 'err' : string }; -export interface Small_NFT { - 'latestTokenID' : () => Promise, - 'mint' : (arg_0: Principal, arg_1: [] | [TokenMetadata]) => Promise, - 'ownerOf' : (arg_0: TokenID) => Promise, - 'transfer' : (arg_0: Principal, arg_1: TokenID) => Promise, -} -export type TokenID = bigint; -export interface TokenMetadata { 'attributes' : [] | [Array] } -export interface _SERVICE extends Small_NFT {} diff --git a/src/declarations/small_nft/small_nft.did.js b/src/declarations/small_nft/small_nft.did.js deleted file mode 100644 index 2d24cdc..0000000 --- a/src/declarations/small_nft/small_nft.did.js +++ /dev/null @@ -1,26 +0,0 @@ -export const idlFactory = ({ IDL }) => { - const Result_2 = IDL.Variant({ 'ok' : IDL.Nat, 'err' : IDL.Text }); - const Attribute = IDL.Record({ 'key' : IDL.Text, 'value' : IDL.Text }); - const TokenMetadata = IDL.Record({ - 'attributes' : IDL.Opt(IDL.Vec(Attribute)), - }); - const TokenID = IDL.Nat; - const Errors = IDL.Variant({ - 'notFoundTokenInfo' : IDL.Null, - 'alreadyExist' : IDL.Null, - }); - const Result_1 = IDL.Variant({ 'ok' : IDL.Text, 'err' : Errors }); - const Okays = IDL.Variant({ - 'canTransfer' : IDL.Text, - 'IsSuccess' : IDL.Nat, - }); - const Result = IDL.Variant({ 'ok' : Okays, 'err' : Errors }); - const Small_NFT = IDL.Service({ - 'latestTokenID' : IDL.Func([], [Result_2], ['query']), - 'mint' : IDL.Func([IDL.Principal, IDL.Opt(TokenMetadata)], [TokenID], []), - 'ownerOf' : IDL.Func([TokenID], [Result_1], ['query']), - 'transfer' : IDL.Func([IDL.Principal, TokenID], [Result], []), - }); - return Small_NFT; -}; -export const init = ({ IDL }) => { return []; }; diff --git a/src/declarations/small_nft_assets/assetstorage.did b/src/declarations/small_nft_assets/assetstorage.did deleted file mode 100644 index d11ecd9..0000000 --- a/src/declarations/small_nft_assets/assetstorage.did +++ /dev/null @@ -1,140 +0,0 @@ -type BatchId = nat; -type ChunkId = nat; -type Key = text; -type Time = int; - -type CreateAssetArguments = record { - key: Key; - content_type: text; -}; - -// Add or change content for an asset, by content encoding -type SetAssetContentArguments = record { - key: Key; - content_encoding: text; - chunk_ids: vec ChunkId; - sha256: opt blob; -}; - -// Remove content for an asset, by content encoding -type UnsetAssetContentArguments = record { - key: Key; - content_encoding: text; -}; - -// Delete an asset -type DeleteAssetArguments = record { - key: Key; -}; - -// Reset everything -type ClearArguments = record {}; - -type BatchOperationKind = variant { - CreateAsset: CreateAssetArguments; - SetAssetContent: SetAssetContentArguments; - - UnsetAssetContent: UnsetAssetContentArguments; - DeleteAsset: DeleteAssetArguments; - - Clear: ClearArguments; -}; - -type HeaderField = record { text; text; }; - -type HttpRequest = record { - method: text; - url: text; - headers: vec HeaderField; - body: blob; -}; - -type HttpResponse = record { - status_code: nat16; - headers: vec HeaderField; - body: blob; - streaming_strategy: opt StreamingStrategy; -}; - -type StreamingCallbackHttpResponse = record { - body: blob; - token: opt StreamingCallbackToken; -}; - -type StreamingCallbackToken = record { - key: Key; - content_encoding: text; - index: nat; - sha256: opt blob; -}; - -type StreamingStrategy = variant { - Callback: record { - callback: func (StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; - token: StreamingCallbackToken; - }; -}; - -service: { - - get: (record { - key: Key; - accept_encodings: vec text; - }) -> (record { - content: blob; // may be the entirety of the content, or just chunk index 0 - content_type: text; - content_encoding: text; - sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments - total_length: nat; // all chunks except last have size == content.size() - }) query; - - // if get() returned chunks > 1, call this to retrieve them. - // chunks may or may not be split up at the same boundaries as presented to create_chunk(). - get_chunk: (record { - key: Key; - content_encoding: text; - index: nat; - sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments - }) -> (record { content: blob }) query; - - list : (record {}) -> (vec record { - key: Key; - content_type: text; - encodings: vec record { - content_encoding: text; - sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments - length: nat; // Size of this encoding's blob. Calculated when uploading assets. - modified: Time; - }; - }) query; - - create_batch : (record {}) -> (record { batch_id: BatchId }); - - create_chunk: (record { batch_id: BatchId; content: blob }) -> (record { chunk_id: ChunkId }); - - // Perform all operations successfully, or reject - commit_batch: (record { batch_id: BatchId; operations: vec BatchOperationKind }) -> (); - - create_asset: (CreateAssetArguments) -> (); - set_asset_content: (SetAssetContentArguments) -> (); - unset_asset_content: (UnsetAssetContentArguments) -> (); - - delete_asset: (DeleteAssetArguments) -> (); - - clear: (ClearArguments) -> (); - - // Single call to create an asset with content for a single content encoding that - // fits within the message ingress limit. - store: (record { - key: Key; - content_type: text; - content_encoding: text; - content: blob; - sha256: opt blob - }) -> (); - - http_request: (request: HttpRequest) -> (HttpResponse) query; - http_request_streaming_callback: (token: StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; - - authorize: (principal) -> (); -} diff --git a/src/declarations/small_nft_assets/index.js b/src/declarations/small_nft_assets/index.js deleted file mode 100644 index 6905dc8..0000000 --- a/src/declarations/small_nft_assets/index.js +++ /dev/null @@ -1,38 +0,0 @@ -import { Actor, HttpAgent } from "@dfinity/agent"; - -// Imports and re-exports candid interface -import { idlFactory } from './small_nft_assets.did.js'; -export { idlFactory } from './small_nft_assets.did.js'; -// CANISTER_ID is replaced by webpack based on node environment -export const canisterId = process.env.SMALL_NFT_ASSETS_CANISTER_ID; - -/** - * - * @param {string | import("@dfinity/principal").Principal} canisterId Canister ID of Agent - * @param {{agentOptions?: import("@dfinity/agent").HttpAgentOptions; actorOptions?: import("@dfinity/agent").ActorConfig}} [options] - * @return {import("@dfinity/agent").ActorSubclass} - */ - export const createActor = (canisterId, options) => { - const agent = new HttpAgent({ ...options?.agentOptions }); - - // Fetch root key for certificate validation during development - if(process.env.NODE_ENV !== "production") { - agent.fetchRootKey().catch(err=>{ - console.warn("Unable to fetch root key. Check to ensure that your local replica is running"); - console.error(err); - }); - } - - // Creates an actor with using the candid interface and the HttpAgent - return Actor.createActor(idlFactory, { - agent, - canisterId, - ...options?.actorOptions, - }); -}; - -/** - * A ready-to-use agent for the small_nft_assets canister - * @type {import("@dfinity/agent").ActorSubclass} - */ - export const small_nft_assets = createActor(canisterId); diff --git a/src/declarations/small_nft_assets/small_nft_assets.did b/src/declarations/small_nft_assets/small_nft_assets.did deleted file mode 100644 index d11ecd9..0000000 --- a/src/declarations/small_nft_assets/small_nft_assets.did +++ /dev/null @@ -1,140 +0,0 @@ -type BatchId = nat; -type ChunkId = nat; -type Key = text; -type Time = int; - -type CreateAssetArguments = record { - key: Key; - content_type: text; -}; - -// Add or change content for an asset, by content encoding -type SetAssetContentArguments = record { - key: Key; - content_encoding: text; - chunk_ids: vec ChunkId; - sha256: opt blob; -}; - -// Remove content for an asset, by content encoding -type UnsetAssetContentArguments = record { - key: Key; - content_encoding: text; -}; - -// Delete an asset -type DeleteAssetArguments = record { - key: Key; -}; - -// Reset everything -type ClearArguments = record {}; - -type BatchOperationKind = variant { - CreateAsset: CreateAssetArguments; - SetAssetContent: SetAssetContentArguments; - - UnsetAssetContent: UnsetAssetContentArguments; - DeleteAsset: DeleteAssetArguments; - - Clear: ClearArguments; -}; - -type HeaderField = record { text; text; }; - -type HttpRequest = record { - method: text; - url: text; - headers: vec HeaderField; - body: blob; -}; - -type HttpResponse = record { - status_code: nat16; - headers: vec HeaderField; - body: blob; - streaming_strategy: opt StreamingStrategy; -}; - -type StreamingCallbackHttpResponse = record { - body: blob; - token: opt StreamingCallbackToken; -}; - -type StreamingCallbackToken = record { - key: Key; - content_encoding: text; - index: nat; - sha256: opt blob; -}; - -type StreamingStrategy = variant { - Callback: record { - callback: func (StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; - token: StreamingCallbackToken; - }; -}; - -service: { - - get: (record { - key: Key; - accept_encodings: vec text; - }) -> (record { - content: blob; // may be the entirety of the content, or just chunk index 0 - content_type: text; - content_encoding: text; - sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments - total_length: nat; // all chunks except last have size == content.size() - }) query; - - // if get() returned chunks > 1, call this to retrieve them. - // chunks may or may not be split up at the same boundaries as presented to create_chunk(). - get_chunk: (record { - key: Key; - content_encoding: text; - index: nat; - sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments - }) -> (record { content: blob }) query; - - list : (record {}) -> (vec record { - key: Key; - content_type: text; - encodings: vec record { - content_encoding: text; - sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments - length: nat; // Size of this encoding's blob. Calculated when uploading assets. - modified: Time; - }; - }) query; - - create_batch : (record {}) -> (record { batch_id: BatchId }); - - create_chunk: (record { batch_id: BatchId; content: blob }) -> (record { chunk_id: ChunkId }); - - // Perform all operations successfully, or reject - commit_batch: (record { batch_id: BatchId; operations: vec BatchOperationKind }) -> (); - - create_asset: (CreateAssetArguments) -> (); - set_asset_content: (SetAssetContentArguments) -> (); - unset_asset_content: (UnsetAssetContentArguments) -> (); - - delete_asset: (DeleteAssetArguments) -> (); - - clear: (ClearArguments) -> (); - - // Single call to create an asset with content for a single content encoding that - // fits within the message ingress limit. - store: (record { - key: Key; - content_type: text; - content_encoding: text; - content: blob; - sha256: opt blob - }) -> (); - - http_request: (request: HttpRequest) -> (HttpResponse) query; - http_request_streaming_callback: (token: StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; - - authorize: (principal) -> (); -} diff --git a/src/declarations/small_nft_assets/small_nft_assets.did.d.ts b/src/declarations/small_nft_assets/small_nft_assets.did.d.ts deleted file mode 100644 index 990bf87..0000000 --- a/src/declarations/small_nft_assets/small_nft_assets.did.d.ts +++ /dev/null @@ -1,117 +0,0 @@ -import type { Principal } from '@dfinity/principal'; -export type BatchId = bigint; -export type BatchOperationKind = { 'CreateAsset' : CreateAssetArguments } | - { 'UnsetAssetContent' : UnsetAssetContentArguments } | - { 'DeleteAsset' : DeleteAssetArguments } | - { 'SetAssetContent' : SetAssetContentArguments } | - { 'Clear' : ClearArguments }; -export type ChunkId = bigint; -export type ClearArguments = {}; -export interface CreateAssetArguments { 'key' : Key, 'content_type' : string } -export interface DeleteAssetArguments { 'key' : Key } -export type HeaderField = [string, string]; -export interface HttpRequest { - 'url' : string, - 'method' : string, - 'body' : Array, - 'headers' : Array, -} -export interface HttpResponse { - 'body' : Array, - 'headers' : Array, - 'streaming_strategy' : [] | [StreamingStrategy], - 'status_code' : number, -} -export type Key = string; -export interface SetAssetContentArguments { - 'key' : Key, - 'sha256' : [] | [Array], - 'chunk_ids' : Array, - 'content_encoding' : string, -} -export interface StreamingCallbackHttpResponse { - 'token' : [] | [StreamingCallbackToken], - 'body' : Array, -} -export interface StreamingCallbackToken { - 'key' : Key, - 'sha256' : [] | [Array], - 'index' : bigint, - 'content_encoding' : string, -} -export type StreamingStrategy = { - 'Callback' : { - 'token' : StreamingCallbackToken, - 'callback' : [Principal, string], - } - }; -export type Time = bigint; -export interface UnsetAssetContentArguments { - 'key' : Key, - 'content_encoding' : string, -} -export interface _SERVICE { - 'authorize' : (arg_0: Principal) => Promise, - 'clear' : (arg_0: ClearArguments) => Promise, - 'commit_batch' : ( - arg_0: { 'batch_id' : BatchId, 'operations' : Array }, - ) => Promise, - 'create_asset' : (arg_0: CreateAssetArguments) => Promise, - 'create_batch' : (arg_0: {}) => Promise<{ 'batch_id' : BatchId }>, - 'create_chunk' : ( - arg_0: { 'content' : Array, 'batch_id' : BatchId }, - ) => Promise<{ 'chunk_id' : ChunkId }>, - 'delete_asset' : (arg_0: DeleteAssetArguments) => Promise, - 'get' : ( - arg_0: { 'key' : Key, 'accept_encodings' : Array }, - ) => Promise< - { - 'content' : Array, - 'sha256' : [] | [Array], - 'content_type' : string, - 'content_encoding' : string, - 'total_length' : bigint, - } - >, - 'get_chunk' : ( - arg_0: { - 'key' : Key, - 'sha256' : [] | [Array], - 'index' : bigint, - 'content_encoding' : string, - }, - ) => Promise<{ 'content' : Array }>, - 'http_request' : (arg_0: HttpRequest) => Promise, - 'http_request_streaming_callback' : ( - arg_0: StreamingCallbackToken, - ) => Promise<[] | [StreamingCallbackHttpResponse]>, - 'list' : (arg_0: {}) => Promise< - Array< - { - 'key' : Key, - 'encodings' : Array< - { - 'modified' : Time, - 'sha256' : [] | [Array], - 'length' : bigint, - 'content_encoding' : string, - } - >, - 'content_type' : string, - } - > - >, - 'set_asset_content' : (arg_0: SetAssetContentArguments) => Promise, - 'store' : ( - arg_0: { - 'key' : Key, - 'content' : Array, - 'sha256' : [] | [Array], - 'content_type' : string, - 'content_encoding' : string, - }, - ) => Promise, - 'unset_asset_content' : (arg_0: UnsetAssetContentArguments) => Promise< - undefined - >, -} diff --git a/src/declarations/small_nft_assets/small_nft_assets.did.js b/src/declarations/small_nft_assets/small_nft_assets.did.js deleted file mode 100644 index 46f975e..0000000 --- a/src/declarations/small_nft_assets/small_nft_assets.did.js +++ /dev/null @@ -1,155 +0,0 @@ -export const idlFactory = ({ IDL }) => { - const ClearArguments = IDL.Record({}); - const BatchId = IDL.Nat; - const Key = IDL.Text; - const CreateAssetArguments = IDL.Record({ - 'key' : Key, - 'content_type' : IDL.Text, - }); - const UnsetAssetContentArguments = IDL.Record({ - 'key' : Key, - 'content_encoding' : IDL.Text, - }); - const DeleteAssetArguments = IDL.Record({ 'key' : Key }); - const ChunkId = IDL.Nat; - const SetAssetContentArguments = IDL.Record({ - 'key' : Key, - 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), - 'chunk_ids' : IDL.Vec(ChunkId), - 'content_encoding' : IDL.Text, - }); - const BatchOperationKind = IDL.Variant({ - 'CreateAsset' : CreateAssetArguments, - 'UnsetAssetContent' : UnsetAssetContentArguments, - 'DeleteAsset' : DeleteAssetArguments, - 'SetAssetContent' : SetAssetContentArguments, - 'Clear' : ClearArguments, - }); - const HeaderField = IDL.Tuple(IDL.Text, IDL.Text); - const HttpRequest = IDL.Record({ - 'url' : IDL.Text, - 'method' : IDL.Text, - 'body' : IDL.Vec(IDL.Nat8), - 'headers' : IDL.Vec(HeaderField), - }); - const StreamingCallbackToken = IDL.Record({ - 'key' : Key, - 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), - 'index' : IDL.Nat, - 'content_encoding' : IDL.Text, - }); - const StreamingCallbackHttpResponse = IDL.Record({ - 'token' : IDL.Opt(StreamingCallbackToken), - 'body' : IDL.Vec(IDL.Nat8), - }); - const StreamingStrategy = IDL.Variant({ - 'Callback' : IDL.Record({ - 'token' : StreamingCallbackToken, - 'callback' : IDL.Func( - [StreamingCallbackToken], - [IDL.Opt(StreamingCallbackHttpResponse)], - ['query'], - ), - }), - }); - const HttpResponse = IDL.Record({ - 'body' : IDL.Vec(IDL.Nat8), - 'headers' : IDL.Vec(HeaderField), - 'streaming_strategy' : IDL.Opt(StreamingStrategy), - 'status_code' : IDL.Nat16, - }); - const Time = IDL.Int; - return IDL.Service({ - 'authorize' : IDL.Func([IDL.Principal], [], []), - 'clear' : IDL.Func([ClearArguments], [], []), - 'commit_batch' : IDL.Func( - [ - IDL.Record({ - 'batch_id' : BatchId, - 'operations' : IDL.Vec(BatchOperationKind), - }), - ], - [], - [], - ), - 'create_asset' : IDL.Func([CreateAssetArguments], [], []), - 'create_batch' : IDL.Func( - [IDL.Record({})], - [IDL.Record({ 'batch_id' : BatchId })], - [], - ), - 'create_chunk' : IDL.Func( - [IDL.Record({ 'content' : IDL.Vec(IDL.Nat8), 'batch_id' : BatchId })], - [IDL.Record({ 'chunk_id' : ChunkId })], - [], - ), - 'delete_asset' : IDL.Func([DeleteAssetArguments], [], []), - 'get' : IDL.Func( - [IDL.Record({ 'key' : Key, 'accept_encodings' : IDL.Vec(IDL.Text) })], - [ - IDL.Record({ - 'content' : IDL.Vec(IDL.Nat8), - 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), - 'content_type' : IDL.Text, - 'content_encoding' : IDL.Text, - 'total_length' : IDL.Nat, - }), - ], - ['query'], - ), - 'get_chunk' : IDL.Func( - [ - IDL.Record({ - 'key' : Key, - 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), - 'index' : IDL.Nat, - 'content_encoding' : IDL.Text, - }), - ], - [IDL.Record({ 'content' : IDL.Vec(IDL.Nat8) })], - ['query'], - ), - 'http_request' : IDL.Func([HttpRequest], [HttpResponse], ['query']), - 'http_request_streaming_callback' : IDL.Func( - [StreamingCallbackToken], - [IDL.Opt(StreamingCallbackHttpResponse)], - ['query'], - ), - 'list' : IDL.Func( - [IDL.Record({})], - [ - IDL.Vec( - IDL.Record({ - 'key' : Key, - 'encodings' : IDL.Vec( - IDL.Record({ - 'modified' : Time, - 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), - 'length' : IDL.Nat, - 'content_encoding' : IDL.Text, - }) - ), - 'content_type' : IDL.Text, - }) - ), - ], - ['query'], - ), - 'set_asset_content' : IDL.Func([SetAssetContentArguments], [], []), - 'store' : IDL.Func( - [ - IDL.Record({ - 'key' : Key, - 'content' : IDL.Vec(IDL.Nat8), - 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), - 'content_type' : IDL.Text, - 'content_encoding' : IDL.Text, - }), - ], - [], - [], - ), - 'unset_asset_content' : IDL.Func([UnsetAssetContentArguments], [], []), - }); -}; -export const init = ({ IDL }) => { return []; }; From 18267f9081c928975aba7d2c14f3d67a0523b293 Mon Sep 17 00:00:00 2001 From: ueyamamasashi Date: Sat, 15 Jan 2022 15:56:24 +0900 Subject: [PATCH 4/7] pull request form hoko --- src/small_nft/main.mo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/small_nft/main.mo b/src/small_nft/main.mo index f290e9a..d210f0c 100644 --- a/src/small_nft/main.mo +++ b/src/small_nft/main.mo @@ -65,7 +65,7 @@ shared(installer) actor class Small_NFT() { #notFoundTokenInfo; #alreadyExist; // 未使用 }; - // okの時で関数内で別の型がある時に使う + // okの時で関数内で別の型がある時に使う. public type Okays = { #canTransfer : Text; #IsSuccess : Nat; From 07a884a6f05f2e06ed6f382c190ebd676c04090e Mon Sep 17 00:00:00 2001 From: ueyamamasashi Date: Sun, 16 Jan 2022 13:27:59 +0900 Subject: [PATCH 5/7] changed --- src/small_nft/main.mo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/small_nft/main.mo b/src/small_nft/main.mo index d210f0c..0903eaf 100644 --- a/src/small_nft/main.mo +++ b/src/small_nft/main.mo @@ -14,7 +14,7 @@ import Nat "mo:base/Nat"; // HashMapの第2引数で使用 import Hash "mo:base/Hash"; // HashMapの第3引数で使用 import Iter "mo:base/Iter"; // preupgrade,postupgradeで使う.HashMapのエントリを書き出す. -import Result "mo:base/Result"; // エラー処理用のモジュール(add:by hokosugi) + // エラー処理用のモジュール(add:by hokosugi) // `shared(<変数名>)`を加えることで,ICProtocolからこのトランザクションの呼び出し人を受け取ることができる. From 82eeb9b8690861288eb4a93ddcdc68fd4ec09f61 Mon Sep 17 00:00:00 2001 From: ueyamamasashi Date: Sun, 16 Jan 2022 13:47:09 +0900 Subject: [PATCH 6/7] changed: bind 8000 to 8030 --- README.md | 2 +- dfx.json | 2 +- src/declarations/small_nft/index.js | 38 +++++ src/declarations/small_nft/small_nft.did | 40 +++++ src/declarations/small_nft/small_nft.did.d.ts | 21 +++ src/declarations/small_nft/small_nft.did.js | 26 +++ .../small_nft_assets/assetstorage.did | 140 ++++++++++++++++ src/declarations/small_nft_assets/index.js | 38 +++++ .../small_nft_assets/small_nft_assets.did | 140 ++++++++++++++++ .../small_nft_assets.did.d.ts | 117 +++++++++++++ .../small_nft_assets/small_nft_assets.did.js | 155 ++++++++++++++++++ src/small_nft/main.mo | 4 +- 12 files changed, 719 insertions(+), 4 deletions(-) create mode 100644 src/declarations/small_nft/index.js create mode 100644 src/declarations/small_nft/small_nft.did create mode 100644 src/declarations/small_nft/small_nft.did.d.ts create mode 100644 src/declarations/small_nft/small_nft.did.js create mode 100644 src/declarations/small_nft_assets/assetstorage.did create mode 100644 src/declarations/small_nft_assets/index.js create mode 100644 src/declarations/small_nft_assets/small_nft_assets.did create mode 100644 src/declarations/small_nft_assets/small_nft_assets.did.d.ts create mode 100644 src/declarations/small_nft_assets/small_nft_assets.did.js diff --git a/README.md b/README.md index ed4ca39..90c9651 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ dfx start --background dfx deploy ``` -Once the job completes, your application will be available at `http://localhost:8000?canisterId={asset_canister_id}`. +Once the job completes, your application will be available at `http://localhost:8030?canisterId={asset_canister_id}`. Additionally, if you are making frontend changes, you can start a development server with diff --git a/dfx.json b/dfx.json index 22dd581..bc052ba 100644 --- a/dfx.json +++ b/dfx.json @@ -27,7 +27,7 @@ "dfx": "0.8.1", "networks": { "local": { - "bind": "127.0.0.1:8000", + "bind": "127.0.0.1:8030", "type": "ephemeral" } }, diff --git a/src/declarations/small_nft/index.js b/src/declarations/small_nft/index.js new file mode 100644 index 0000000..84d7f45 --- /dev/null +++ b/src/declarations/small_nft/index.js @@ -0,0 +1,38 @@ +import { Actor, HttpAgent } from "@dfinity/agent"; + +// Imports and re-exports candid interface +import { idlFactory } from './small_nft.did.js'; +export { idlFactory } from './small_nft.did.js'; +// CANISTER_ID is replaced by webpack based on node environment +export const canisterId = process.env.SMALL_NFT_CANISTER_ID; + +/** + * + * @param {string | import("@dfinity/principal").Principal} canisterId Canister ID of Agent + * @param {{agentOptions?: import("@dfinity/agent").HttpAgentOptions; actorOptions?: import("@dfinity/agent").ActorConfig}} [options] + * @return {import("@dfinity/agent").ActorSubclass} + */ + export const createActor = (canisterId, options) => { + const agent = new HttpAgent({ ...options?.agentOptions }); + + // Fetch root key for certificate validation during development + if(process.env.NODE_ENV !== "production") { + agent.fetchRootKey().catch(err=>{ + console.warn("Unable to fetch root key. Check to ensure that your local replica is running"); + console.error(err); + }); + } + + // Creates an actor with using the candid interface and the HttpAgent + return Actor.createActor(idlFactory, { + agent, + canisterId, + ...options?.actorOptions, + }); +}; + +/** + * A ready-to-use agent for the small_nft canister + * @type {import("@dfinity/agent").ActorSubclass} + */ + export const small_nft = createActor(canisterId); diff --git a/src/declarations/small_nft/small_nft.did b/src/declarations/small_nft/small_nft.did new file mode 100644 index 0000000..03a71ed --- /dev/null +++ b/src/declarations/small_nft/small_nft.did @@ -0,0 +1,40 @@ +type TokenMetadata = record {attributes: opt vec Attribute;}; +type TokenID = nat; +type Small_NFT = + service { + latestTokenID: () -> (Result_2) query; + mint: (principal, opt TokenMetadata) -> (TokenID); + ownerOf: (TokenID) -> (Result_1) query; + transfer: (principal, TokenID) -> (Result); + }; +type Result_2 = + variant { + err: text; + ok: nat; + }; +type Result_1 = + variant { + err: Errors; + ok: text; + }; +type Result = + variant { + err: Errors; + ok: Okays; + }; +type Okays = + variant { + IsSuccess: nat; + canTransfer: text; + }; +type Errors = + variant { + alreadyExist; + notFoundTokenInfo; + }; +type Attribute = + record { + key: text; + value: text; + }; +service : () -> Small_NFT diff --git a/src/declarations/small_nft/small_nft.did.d.ts b/src/declarations/small_nft/small_nft.did.d.ts new file mode 100644 index 0000000..7b7403e --- /dev/null +++ b/src/declarations/small_nft/small_nft.did.d.ts @@ -0,0 +1,21 @@ +import type { Principal } from '@dfinity/principal'; +export interface Attribute { 'key' : string, 'value' : string } +export type Errors = { 'notFoundTokenInfo' : null } | + { 'alreadyExist' : null }; +export type Okays = { 'canTransfer' : string } | + { 'IsSuccess' : bigint }; +export type Result = { 'ok' : Okays } | + { 'err' : Errors }; +export type Result_1 = { 'ok' : string } | + { 'err' : Errors }; +export type Result_2 = { 'ok' : bigint } | + { 'err' : string }; +export interface Small_NFT { + 'latestTokenID' : () => Promise, + 'mint' : (arg_0: Principal, arg_1: [] | [TokenMetadata]) => Promise, + 'ownerOf' : (arg_0: TokenID) => Promise, + 'transfer' : (arg_0: Principal, arg_1: TokenID) => Promise, +} +export type TokenID = bigint; +export interface TokenMetadata { 'attributes' : [] | [Array] } +export interface _SERVICE extends Small_NFT {} diff --git a/src/declarations/small_nft/small_nft.did.js b/src/declarations/small_nft/small_nft.did.js new file mode 100644 index 0000000..2d24cdc --- /dev/null +++ b/src/declarations/small_nft/small_nft.did.js @@ -0,0 +1,26 @@ +export const idlFactory = ({ IDL }) => { + const Result_2 = IDL.Variant({ 'ok' : IDL.Nat, 'err' : IDL.Text }); + const Attribute = IDL.Record({ 'key' : IDL.Text, 'value' : IDL.Text }); + const TokenMetadata = IDL.Record({ + 'attributes' : IDL.Opt(IDL.Vec(Attribute)), + }); + const TokenID = IDL.Nat; + const Errors = IDL.Variant({ + 'notFoundTokenInfo' : IDL.Null, + 'alreadyExist' : IDL.Null, + }); + const Result_1 = IDL.Variant({ 'ok' : IDL.Text, 'err' : Errors }); + const Okays = IDL.Variant({ + 'canTransfer' : IDL.Text, + 'IsSuccess' : IDL.Nat, + }); + const Result = IDL.Variant({ 'ok' : Okays, 'err' : Errors }); + const Small_NFT = IDL.Service({ + 'latestTokenID' : IDL.Func([], [Result_2], ['query']), + 'mint' : IDL.Func([IDL.Principal, IDL.Opt(TokenMetadata)], [TokenID], []), + 'ownerOf' : IDL.Func([TokenID], [Result_1], ['query']), + 'transfer' : IDL.Func([IDL.Principal, TokenID], [Result], []), + }); + return Small_NFT; +}; +export const init = ({ IDL }) => { return []; }; diff --git a/src/declarations/small_nft_assets/assetstorage.did b/src/declarations/small_nft_assets/assetstorage.did new file mode 100644 index 0000000..d11ecd9 --- /dev/null +++ b/src/declarations/small_nft_assets/assetstorage.did @@ -0,0 +1,140 @@ +type BatchId = nat; +type ChunkId = nat; +type Key = text; +type Time = int; + +type CreateAssetArguments = record { + key: Key; + content_type: text; +}; + +// Add or change content for an asset, by content encoding +type SetAssetContentArguments = record { + key: Key; + content_encoding: text; + chunk_ids: vec ChunkId; + sha256: opt blob; +}; + +// Remove content for an asset, by content encoding +type UnsetAssetContentArguments = record { + key: Key; + content_encoding: text; +}; + +// Delete an asset +type DeleteAssetArguments = record { + key: Key; +}; + +// Reset everything +type ClearArguments = record {}; + +type BatchOperationKind = variant { + CreateAsset: CreateAssetArguments; + SetAssetContent: SetAssetContentArguments; + + UnsetAssetContent: UnsetAssetContentArguments; + DeleteAsset: DeleteAssetArguments; + + Clear: ClearArguments; +}; + +type HeaderField = record { text; text; }; + +type HttpRequest = record { + method: text; + url: text; + headers: vec HeaderField; + body: blob; +}; + +type HttpResponse = record { + status_code: nat16; + headers: vec HeaderField; + body: blob; + streaming_strategy: opt StreamingStrategy; +}; + +type StreamingCallbackHttpResponse = record { + body: blob; + token: opt StreamingCallbackToken; +}; + +type StreamingCallbackToken = record { + key: Key; + content_encoding: text; + index: nat; + sha256: opt blob; +}; + +type StreamingStrategy = variant { + Callback: record { + callback: func (StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; + token: StreamingCallbackToken; + }; +}; + +service: { + + get: (record { + key: Key; + accept_encodings: vec text; + }) -> (record { + content: blob; // may be the entirety of the content, or just chunk index 0 + content_type: text; + content_encoding: text; + sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments + total_length: nat; // all chunks except last have size == content.size() + }) query; + + // if get() returned chunks > 1, call this to retrieve them. + // chunks may or may not be split up at the same boundaries as presented to create_chunk(). + get_chunk: (record { + key: Key; + content_encoding: text; + index: nat; + sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments + }) -> (record { content: blob }) query; + + list : (record {}) -> (vec record { + key: Key; + content_type: text; + encodings: vec record { + content_encoding: text; + sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments + length: nat; // Size of this encoding's blob. Calculated when uploading assets. + modified: Time; + }; + }) query; + + create_batch : (record {}) -> (record { batch_id: BatchId }); + + create_chunk: (record { batch_id: BatchId; content: blob }) -> (record { chunk_id: ChunkId }); + + // Perform all operations successfully, or reject + commit_batch: (record { batch_id: BatchId; operations: vec BatchOperationKind }) -> (); + + create_asset: (CreateAssetArguments) -> (); + set_asset_content: (SetAssetContentArguments) -> (); + unset_asset_content: (UnsetAssetContentArguments) -> (); + + delete_asset: (DeleteAssetArguments) -> (); + + clear: (ClearArguments) -> (); + + // Single call to create an asset with content for a single content encoding that + // fits within the message ingress limit. + store: (record { + key: Key; + content_type: text; + content_encoding: text; + content: blob; + sha256: opt blob + }) -> (); + + http_request: (request: HttpRequest) -> (HttpResponse) query; + http_request_streaming_callback: (token: StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; + + authorize: (principal) -> (); +} diff --git a/src/declarations/small_nft_assets/index.js b/src/declarations/small_nft_assets/index.js new file mode 100644 index 0000000..6905dc8 --- /dev/null +++ b/src/declarations/small_nft_assets/index.js @@ -0,0 +1,38 @@ +import { Actor, HttpAgent } from "@dfinity/agent"; + +// Imports and re-exports candid interface +import { idlFactory } from './small_nft_assets.did.js'; +export { idlFactory } from './small_nft_assets.did.js'; +// CANISTER_ID is replaced by webpack based on node environment +export const canisterId = process.env.SMALL_NFT_ASSETS_CANISTER_ID; + +/** + * + * @param {string | import("@dfinity/principal").Principal} canisterId Canister ID of Agent + * @param {{agentOptions?: import("@dfinity/agent").HttpAgentOptions; actorOptions?: import("@dfinity/agent").ActorConfig}} [options] + * @return {import("@dfinity/agent").ActorSubclass} + */ + export const createActor = (canisterId, options) => { + const agent = new HttpAgent({ ...options?.agentOptions }); + + // Fetch root key for certificate validation during development + if(process.env.NODE_ENV !== "production") { + agent.fetchRootKey().catch(err=>{ + console.warn("Unable to fetch root key. Check to ensure that your local replica is running"); + console.error(err); + }); + } + + // Creates an actor with using the candid interface and the HttpAgent + return Actor.createActor(idlFactory, { + agent, + canisterId, + ...options?.actorOptions, + }); +}; + +/** + * A ready-to-use agent for the small_nft_assets canister + * @type {import("@dfinity/agent").ActorSubclass} + */ + export const small_nft_assets = createActor(canisterId); diff --git a/src/declarations/small_nft_assets/small_nft_assets.did b/src/declarations/small_nft_assets/small_nft_assets.did new file mode 100644 index 0000000..d11ecd9 --- /dev/null +++ b/src/declarations/small_nft_assets/small_nft_assets.did @@ -0,0 +1,140 @@ +type BatchId = nat; +type ChunkId = nat; +type Key = text; +type Time = int; + +type CreateAssetArguments = record { + key: Key; + content_type: text; +}; + +// Add or change content for an asset, by content encoding +type SetAssetContentArguments = record { + key: Key; + content_encoding: text; + chunk_ids: vec ChunkId; + sha256: opt blob; +}; + +// Remove content for an asset, by content encoding +type UnsetAssetContentArguments = record { + key: Key; + content_encoding: text; +}; + +// Delete an asset +type DeleteAssetArguments = record { + key: Key; +}; + +// Reset everything +type ClearArguments = record {}; + +type BatchOperationKind = variant { + CreateAsset: CreateAssetArguments; + SetAssetContent: SetAssetContentArguments; + + UnsetAssetContent: UnsetAssetContentArguments; + DeleteAsset: DeleteAssetArguments; + + Clear: ClearArguments; +}; + +type HeaderField = record { text; text; }; + +type HttpRequest = record { + method: text; + url: text; + headers: vec HeaderField; + body: blob; +}; + +type HttpResponse = record { + status_code: nat16; + headers: vec HeaderField; + body: blob; + streaming_strategy: opt StreamingStrategy; +}; + +type StreamingCallbackHttpResponse = record { + body: blob; + token: opt StreamingCallbackToken; +}; + +type StreamingCallbackToken = record { + key: Key; + content_encoding: text; + index: nat; + sha256: opt blob; +}; + +type StreamingStrategy = variant { + Callback: record { + callback: func (StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; + token: StreamingCallbackToken; + }; +}; + +service: { + + get: (record { + key: Key; + accept_encodings: vec text; + }) -> (record { + content: blob; // may be the entirety of the content, or just chunk index 0 + content_type: text; + content_encoding: text; + sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments + total_length: nat; // all chunks except last have size == content.size() + }) query; + + // if get() returned chunks > 1, call this to retrieve them. + // chunks may or may not be split up at the same boundaries as presented to create_chunk(). + get_chunk: (record { + key: Key; + content_encoding: text; + index: nat; + sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments + }) -> (record { content: blob }) query; + + list : (record {}) -> (vec record { + key: Key; + content_type: text; + encodings: vec record { + content_encoding: text; + sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments + length: nat; // Size of this encoding's blob. Calculated when uploading assets. + modified: Time; + }; + }) query; + + create_batch : (record {}) -> (record { batch_id: BatchId }); + + create_chunk: (record { batch_id: BatchId; content: blob }) -> (record { chunk_id: ChunkId }); + + // Perform all operations successfully, or reject + commit_batch: (record { batch_id: BatchId; operations: vec BatchOperationKind }) -> (); + + create_asset: (CreateAssetArguments) -> (); + set_asset_content: (SetAssetContentArguments) -> (); + unset_asset_content: (UnsetAssetContentArguments) -> (); + + delete_asset: (DeleteAssetArguments) -> (); + + clear: (ClearArguments) -> (); + + // Single call to create an asset with content for a single content encoding that + // fits within the message ingress limit. + store: (record { + key: Key; + content_type: text; + content_encoding: text; + content: blob; + sha256: opt blob + }) -> (); + + http_request: (request: HttpRequest) -> (HttpResponse) query; + http_request_streaming_callback: (token: StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; + + authorize: (principal) -> (); +} diff --git a/src/declarations/small_nft_assets/small_nft_assets.did.d.ts b/src/declarations/small_nft_assets/small_nft_assets.did.d.ts new file mode 100644 index 0000000..990bf87 --- /dev/null +++ b/src/declarations/small_nft_assets/small_nft_assets.did.d.ts @@ -0,0 +1,117 @@ +import type { Principal } from '@dfinity/principal'; +export type BatchId = bigint; +export type BatchOperationKind = { 'CreateAsset' : CreateAssetArguments } | + { 'UnsetAssetContent' : UnsetAssetContentArguments } | + { 'DeleteAsset' : DeleteAssetArguments } | + { 'SetAssetContent' : SetAssetContentArguments } | + { 'Clear' : ClearArguments }; +export type ChunkId = bigint; +export type ClearArguments = {}; +export interface CreateAssetArguments { 'key' : Key, 'content_type' : string } +export interface DeleteAssetArguments { 'key' : Key } +export type HeaderField = [string, string]; +export interface HttpRequest { + 'url' : string, + 'method' : string, + 'body' : Array, + 'headers' : Array, +} +export interface HttpResponse { + 'body' : Array, + 'headers' : Array, + 'streaming_strategy' : [] | [StreamingStrategy], + 'status_code' : number, +} +export type Key = string; +export interface SetAssetContentArguments { + 'key' : Key, + 'sha256' : [] | [Array], + 'chunk_ids' : Array, + 'content_encoding' : string, +} +export interface StreamingCallbackHttpResponse { + 'token' : [] | [StreamingCallbackToken], + 'body' : Array, +} +export interface StreamingCallbackToken { + 'key' : Key, + 'sha256' : [] | [Array], + 'index' : bigint, + 'content_encoding' : string, +} +export type StreamingStrategy = { + 'Callback' : { + 'token' : StreamingCallbackToken, + 'callback' : [Principal, string], + } + }; +export type Time = bigint; +export interface UnsetAssetContentArguments { + 'key' : Key, + 'content_encoding' : string, +} +export interface _SERVICE { + 'authorize' : (arg_0: Principal) => Promise, + 'clear' : (arg_0: ClearArguments) => Promise, + 'commit_batch' : ( + arg_0: { 'batch_id' : BatchId, 'operations' : Array }, + ) => Promise, + 'create_asset' : (arg_0: CreateAssetArguments) => Promise, + 'create_batch' : (arg_0: {}) => Promise<{ 'batch_id' : BatchId }>, + 'create_chunk' : ( + arg_0: { 'content' : Array, 'batch_id' : BatchId }, + ) => Promise<{ 'chunk_id' : ChunkId }>, + 'delete_asset' : (arg_0: DeleteAssetArguments) => Promise, + 'get' : ( + arg_0: { 'key' : Key, 'accept_encodings' : Array }, + ) => Promise< + { + 'content' : Array, + 'sha256' : [] | [Array], + 'content_type' : string, + 'content_encoding' : string, + 'total_length' : bigint, + } + >, + 'get_chunk' : ( + arg_0: { + 'key' : Key, + 'sha256' : [] | [Array], + 'index' : bigint, + 'content_encoding' : string, + }, + ) => Promise<{ 'content' : Array }>, + 'http_request' : (arg_0: HttpRequest) => Promise, + 'http_request_streaming_callback' : ( + arg_0: StreamingCallbackToken, + ) => Promise<[] | [StreamingCallbackHttpResponse]>, + 'list' : (arg_0: {}) => Promise< + Array< + { + 'key' : Key, + 'encodings' : Array< + { + 'modified' : Time, + 'sha256' : [] | [Array], + 'length' : bigint, + 'content_encoding' : string, + } + >, + 'content_type' : string, + } + > + >, + 'set_asset_content' : (arg_0: SetAssetContentArguments) => Promise, + 'store' : ( + arg_0: { + 'key' : Key, + 'content' : Array, + 'sha256' : [] | [Array], + 'content_type' : string, + 'content_encoding' : string, + }, + ) => Promise, + 'unset_asset_content' : (arg_0: UnsetAssetContentArguments) => Promise< + undefined + >, +} diff --git a/src/declarations/small_nft_assets/small_nft_assets.did.js b/src/declarations/small_nft_assets/small_nft_assets.did.js new file mode 100644 index 0000000..46f975e --- /dev/null +++ b/src/declarations/small_nft_assets/small_nft_assets.did.js @@ -0,0 +1,155 @@ +export const idlFactory = ({ IDL }) => { + const ClearArguments = IDL.Record({}); + const BatchId = IDL.Nat; + const Key = IDL.Text; + const CreateAssetArguments = IDL.Record({ + 'key' : Key, + 'content_type' : IDL.Text, + }); + const UnsetAssetContentArguments = IDL.Record({ + 'key' : Key, + 'content_encoding' : IDL.Text, + }); + const DeleteAssetArguments = IDL.Record({ 'key' : Key }); + const ChunkId = IDL.Nat; + const SetAssetContentArguments = IDL.Record({ + 'key' : Key, + 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), + 'chunk_ids' : IDL.Vec(ChunkId), + 'content_encoding' : IDL.Text, + }); + const BatchOperationKind = IDL.Variant({ + 'CreateAsset' : CreateAssetArguments, + 'UnsetAssetContent' : UnsetAssetContentArguments, + 'DeleteAsset' : DeleteAssetArguments, + 'SetAssetContent' : SetAssetContentArguments, + 'Clear' : ClearArguments, + }); + const HeaderField = IDL.Tuple(IDL.Text, IDL.Text); + const HttpRequest = IDL.Record({ + 'url' : IDL.Text, + 'method' : IDL.Text, + 'body' : IDL.Vec(IDL.Nat8), + 'headers' : IDL.Vec(HeaderField), + }); + const StreamingCallbackToken = IDL.Record({ + 'key' : Key, + 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), + 'index' : IDL.Nat, + 'content_encoding' : IDL.Text, + }); + const StreamingCallbackHttpResponse = IDL.Record({ + 'token' : IDL.Opt(StreamingCallbackToken), + 'body' : IDL.Vec(IDL.Nat8), + }); + const StreamingStrategy = IDL.Variant({ + 'Callback' : IDL.Record({ + 'token' : StreamingCallbackToken, + 'callback' : IDL.Func( + [StreamingCallbackToken], + [IDL.Opt(StreamingCallbackHttpResponse)], + ['query'], + ), + }), + }); + const HttpResponse = IDL.Record({ + 'body' : IDL.Vec(IDL.Nat8), + 'headers' : IDL.Vec(HeaderField), + 'streaming_strategy' : IDL.Opt(StreamingStrategy), + 'status_code' : IDL.Nat16, + }); + const Time = IDL.Int; + return IDL.Service({ + 'authorize' : IDL.Func([IDL.Principal], [], []), + 'clear' : IDL.Func([ClearArguments], [], []), + 'commit_batch' : IDL.Func( + [ + IDL.Record({ + 'batch_id' : BatchId, + 'operations' : IDL.Vec(BatchOperationKind), + }), + ], + [], + [], + ), + 'create_asset' : IDL.Func([CreateAssetArguments], [], []), + 'create_batch' : IDL.Func( + [IDL.Record({})], + [IDL.Record({ 'batch_id' : BatchId })], + [], + ), + 'create_chunk' : IDL.Func( + [IDL.Record({ 'content' : IDL.Vec(IDL.Nat8), 'batch_id' : BatchId })], + [IDL.Record({ 'chunk_id' : ChunkId })], + [], + ), + 'delete_asset' : IDL.Func([DeleteAssetArguments], [], []), + 'get' : IDL.Func( + [IDL.Record({ 'key' : Key, 'accept_encodings' : IDL.Vec(IDL.Text) })], + [ + IDL.Record({ + 'content' : IDL.Vec(IDL.Nat8), + 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), + 'content_type' : IDL.Text, + 'content_encoding' : IDL.Text, + 'total_length' : IDL.Nat, + }), + ], + ['query'], + ), + 'get_chunk' : IDL.Func( + [ + IDL.Record({ + 'key' : Key, + 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), + 'index' : IDL.Nat, + 'content_encoding' : IDL.Text, + }), + ], + [IDL.Record({ 'content' : IDL.Vec(IDL.Nat8) })], + ['query'], + ), + 'http_request' : IDL.Func([HttpRequest], [HttpResponse], ['query']), + 'http_request_streaming_callback' : IDL.Func( + [StreamingCallbackToken], + [IDL.Opt(StreamingCallbackHttpResponse)], + ['query'], + ), + 'list' : IDL.Func( + [IDL.Record({})], + [ + IDL.Vec( + IDL.Record({ + 'key' : Key, + 'encodings' : IDL.Vec( + IDL.Record({ + 'modified' : Time, + 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), + 'length' : IDL.Nat, + 'content_encoding' : IDL.Text, + }) + ), + 'content_type' : IDL.Text, + }) + ), + ], + ['query'], + ), + 'set_asset_content' : IDL.Func([SetAssetContentArguments], [], []), + 'store' : IDL.Func( + [ + IDL.Record({ + 'key' : Key, + 'content' : IDL.Vec(IDL.Nat8), + 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), + 'content_type' : IDL.Text, + 'content_encoding' : IDL.Text, + }), + ], + [], + [], + ), + 'unset_asset_content' : IDL.Func([UnsetAssetContentArguments], [], []), + }); +}; +export const init = ({ IDL }) => { return []; }; diff --git a/src/small_nft/main.mo b/src/small_nft/main.mo index 0903eaf..e5ab86b 100644 --- a/src/small_nft/main.mo +++ b/src/small_nft/main.mo @@ -14,8 +14,8 @@ import Nat "mo:base/Nat"; // HashMapの第2引数で使用 import Hash "mo:base/Hash"; // HashMapの第3引数で使用 import Iter "mo:base/Iter"; // preupgrade,postupgradeで使う.HashMapのエントリを書き出す. - // エラー処理用のモジュール(add:by hokosugi) - + // エラー処理用のモジュール(add:by hokosugi) +import Result "mo:base/Result"; // `shared(<変数名>)`を加えることで,ICProtocolからこのトランザクションの呼び出し人を受け取ることができる. // `<変数名>.caller`でそのPrincipalを参照できる. From 9910becbdfcdac1118160c387149cde3510a9a34 Mon Sep 17 00:00:00 2001 From: ueyamamasashi Date: Sun, 16 Jan 2022 13:51:07 +0900 Subject: [PATCH 7/7] deleted: declarations --- src/declarations/small_nft/index.js | 38 ----- src/declarations/small_nft/small_nft.did | 40 ----- src/declarations/small_nft/small_nft.did.d.ts | 21 --- src/declarations/small_nft/small_nft.did.js | 26 --- .../small_nft_assets/assetstorage.did | 140 ---------------- src/declarations/small_nft_assets/index.js | 38 ----- .../small_nft_assets/small_nft_assets.did | 140 ---------------- .../small_nft_assets.did.d.ts | 117 ------------- .../small_nft_assets/small_nft_assets.did.js | 155 ------------------ 9 files changed, 715 deletions(-) delete mode 100644 src/declarations/small_nft/index.js delete mode 100644 src/declarations/small_nft/small_nft.did delete mode 100644 src/declarations/small_nft/small_nft.did.d.ts delete mode 100644 src/declarations/small_nft/small_nft.did.js delete mode 100644 src/declarations/small_nft_assets/assetstorage.did delete mode 100644 src/declarations/small_nft_assets/index.js delete mode 100644 src/declarations/small_nft_assets/small_nft_assets.did delete mode 100644 src/declarations/small_nft_assets/small_nft_assets.did.d.ts delete mode 100644 src/declarations/small_nft_assets/small_nft_assets.did.js diff --git a/src/declarations/small_nft/index.js b/src/declarations/small_nft/index.js deleted file mode 100644 index 84d7f45..0000000 --- a/src/declarations/small_nft/index.js +++ /dev/null @@ -1,38 +0,0 @@ -import { Actor, HttpAgent } from "@dfinity/agent"; - -// Imports and re-exports candid interface -import { idlFactory } from './small_nft.did.js'; -export { idlFactory } from './small_nft.did.js'; -// CANISTER_ID is replaced by webpack based on node environment -export const canisterId = process.env.SMALL_NFT_CANISTER_ID; - -/** - * - * @param {string | import("@dfinity/principal").Principal} canisterId Canister ID of Agent - * @param {{agentOptions?: import("@dfinity/agent").HttpAgentOptions; actorOptions?: import("@dfinity/agent").ActorConfig}} [options] - * @return {import("@dfinity/agent").ActorSubclass} - */ - export const createActor = (canisterId, options) => { - const agent = new HttpAgent({ ...options?.agentOptions }); - - // Fetch root key for certificate validation during development - if(process.env.NODE_ENV !== "production") { - agent.fetchRootKey().catch(err=>{ - console.warn("Unable to fetch root key. Check to ensure that your local replica is running"); - console.error(err); - }); - } - - // Creates an actor with using the candid interface and the HttpAgent - return Actor.createActor(idlFactory, { - agent, - canisterId, - ...options?.actorOptions, - }); -}; - -/** - * A ready-to-use agent for the small_nft canister - * @type {import("@dfinity/agent").ActorSubclass} - */ - export const small_nft = createActor(canisterId); diff --git a/src/declarations/small_nft/small_nft.did b/src/declarations/small_nft/small_nft.did deleted file mode 100644 index 03a71ed..0000000 --- a/src/declarations/small_nft/small_nft.did +++ /dev/null @@ -1,40 +0,0 @@ -type TokenMetadata = record {attributes: opt vec Attribute;}; -type TokenID = nat; -type Small_NFT = - service { - latestTokenID: () -> (Result_2) query; - mint: (principal, opt TokenMetadata) -> (TokenID); - ownerOf: (TokenID) -> (Result_1) query; - transfer: (principal, TokenID) -> (Result); - }; -type Result_2 = - variant { - err: text; - ok: nat; - }; -type Result_1 = - variant { - err: Errors; - ok: text; - }; -type Result = - variant { - err: Errors; - ok: Okays; - }; -type Okays = - variant { - IsSuccess: nat; - canTransfer: text; - }; -type Errors = - variant { - alreadyExist; - notFoundTokenInfo; - }; -type Attribute = - record { - key: text; - value: text; - }; -service : () -> Small_NFT diff --git a/src/declarations/small_nft/small_nft.did.d.ts b/src/declarations/small_nft/small_nft.did.d.ts deleted file mode 100644 index 7b7403e..0000000 --- a/src/declarations/small_nft/small_nft.did.d.ts +++ /dev/null @@ -1,21 +0,0 @@ -import type { Principal } from '@dfinity/principal'; -export interface Attribute { 'key' : string, 'value' : string } -export type Errors = { 'notFoundTokenInfo' : null } | - { 'alreadyExist' : null }; -export type Okays = { 'canTransfer' : string } | - { 'IsSuccess' : bigint }; -export type Result = { 'ok' : Okays } | - { 'err' : Errors }; -export type Result_1 = { 'ok' : string } | - { 'err' : Errors }; -export type Result_2 = { 'ok' : bigint } | - { 'err' : string }; -export interface Small_NFT { - 'latestTokenID' : () => Promise, - 'mint' : (arg_0: Principal, arg_1: [] | [TokenMetadata]) => Promise, - 'ownerOf' : (arg_0: TokenID) => Promise, - 'transfer' : (arg_0: Principal, arg_1: TokenID) => Promise, -} -export type TokenID = bigint; -export interface TokenMetadata { 'attributes' : [] | [Array] } -export interface _SERVICE extends Small_NFT {} diff --git a/src/declarations/small_nft/small_nft.did.js b/src/declarations/small_nft/small_nft.did.js deleted file mode 100644 index 2d24cdc..0000000 --- a/src/declarations/small_nft/small_nft.did.js +++ /dev/null @@ -1,26 +0,0 @@ -export const idlFactory = ({ IDL }) => { - const Result_2 = IDL.Variant({ 'ok' : IDL.Nat, 'err' : IDL.Text }); - const Attribute = IDL.Record({ 'key' : IDL.Text, 'value' : IDL.Text }); - const TokenMetadata = IDL.Record({ - 'attributes' : IDL.Opt(IDL.Vec(Attribute)), - }); - const TokenID = IDL.Nat; - const Errors = IDL.Variant({ - 'notFoundTokenInfo' : IDL.Null, - 'alreadyExist' : IDL.Null, - }); - const Result_1 = IDL.Variant({ 'ok' : IDL.Text, 'err' : Errors }); - const Okays = IDL.Variant({ - 'canTransfer' : IDL.Text, - 'IsSuccess' : IDL.Nat, - }); - const Result = IDL.Variant({ 'ok' : Okays, 'err' : Errors }); - const Small_NFT = IDL.Service({ - 'latestTokenID' : IDL.Func([], [Result_2], ['query']), - 'mint' : IDL.Func([IDL.Principal, IDL.Opt(TokenMetadata)], [TokenID], []), - 'ownerOf' : IDL.Func([TokenID], [Result_1], ['query']), - 'transfer' : IDL.Func([IDL.Principal, TokenID], [Result], []), - }); - return Small_NFT; -}; -export const init = ({ IDL }) => { return []; }; diff --git a/src/declarations/small_nft_assets/assetstorage.did b/src/declarations/small_nft_assets/assetstorage.did deleted file mode 100644 index d11ecd9..0000000 --- a/src/declarations/small_nft_assets/assetstorage.did +++ /dev/null @@ -1,140 +0,0 @@ -type BatchId = nat; -type ChunkId = nat; -type Key = text; -type Time = int; - -type CreateAssetArguments = record { - key: Key; - content_type: text; -}; - -// Add or change content for an asset, by content encoding -type SetAssetContentArguments = record { - key: Key; - content_encoding: text; - chunk_ids: vec ChunkId; - sha256: opt blob; -}; - -// Remove content for an asset, by content encoding -type UnsetAssetContentArguments = record { - key: Key; - content_encoding: text; -}; - -// Delete an asset -type DeleteAssetArguments = record { - key: Key; -}; - -// Reset everything -type ClearArguments = record {}; - -type BatchOperationKind = variant { - CreateAsset: CreateAssetArguments; - SetAssetContent: SetAssetContentArguments; - - UnsetAssetContent: UnsetAssetContentArguments; - DeleteAsset: DeleteAssetArguments; - - Clear: ClearArguments; -}; - -type HeaderField = record { text; text; }; - -type HttpRequest = record { - method: text; - url: text; - headers: vec HeaderField; - body: blob; -}; - -type HttpResponse = record { - status_code: nat16; - headers: vec HeaderField; - body: blob; - streaming_strategy: opt StreamingStrategy; -}; - -type StreamingCallbackHttpResponse = record { - body: blob; - token: opt StreamingCallbackToken; -}; - -type StreamingCallbackToken = record { - key: Key; - content_encoding: text; - index: nat; - sha256: opt blob; -}; - -type StreamingStrategy = variant { - Callback: record { - callback: func (StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; - token: StreamingCallbackToken; - }; -}; - -service: { - - get: (record { - key: Key; - accept_encodings: vec text; - }) -> (record { - content: blob; // may be the entirety of the content, or just chunk index 0 - content_type: text; - content_encoding: text; - sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments - total_length: nat; // all chunks except last have size == content.size() - }) query; - - // if get() returned chunks > 1, call this to retrieve them. - // chunks may or may not be split up at the same boundaries as presented to create_chunk(). - get_chunk: (record { - key: Key; - content_encoding: text; - index: nat; - sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments - }) -> (record { content: blob }) query; - - list : (record {}) -> (vec record { - key: Key; - content_type: text; - encodings: vec record { - content_encoding: text; - sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments - length: nat; // Size of this encoding's blob. Calculated when uploading assets. - modified: Time; - }; - }) query; - - create_batch : (record {}) -> (record { batch_id: BatchId }); - - create_chunk: (record { batch_id: BatchId; content: blob }) -> (record { chunk_id: ChunkId }); - - // Perform all operations successfully, or reject - commit_batch: (record { batch_id: BatchId; operations: vec BatchOperationKind }) -> (); - - create_asset: (CreateAssetArguments) -> (); - set_asset_content: (SetAssetContentArguments) -> (); - unset_asset_content: (UnsetAssetContentArguments) -> (); - - delete_asset: (DeleteAssetArguments) -> (); - - clear: (ClearArguments) -> (); - - // Single call to create an asset with content for a single content encoding that - // fits within the message ingress limit. - store: (record { - key: Key; - content_type: text; - content_encoding: text; - content: blob; - sha256: opt blob - }) -> (); - - http_request: (request: HttpRequest) -> (HttpResponse) query; - http_request_streaming_callback: (token: StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; - - authorize: (principal) -> (); -} diff --git a/src/declarations/small_nft_assets/index.js b/src/declarations/small_nft_assets/index.js deleted file mode 100644 index 6905dc8..0000000 --- a/src/declarations/small_nft_assets/index.js +++ /dev/null @@ -1,38 +0,0 @@ -import { Actor, HttpAgent } from "@dfinity/agent"; - -// Imports and re-exports candid interface -import { idlFactory } from './small_nft_assets.did.js'; -export { idlFactory } from './small_nft_assets.did.js'; -// CANISTER_ID is replaced by webpack based on node environment -export const canisterId = process.env.SMALL_NFT_ASSETS_CANISTER_ID; - -/** - * - * @param {string | import("@dfinity/principal").Principal} canisterId Canister ID of Agent - * @param {{agentOptions?: import("@dfinity/agent").HttpAgentOptions; actorOptions?: import("@dfinity/agent").ActorConfig}} [options] - * @return {import("@dfinity/agent").ActorSubclass} - */ - export const createActor = (canisterId, options) => { - const agent = new HttpAgent({ ...options?.agentOptions }); - - // Fetch root key for certificate validation during development - if(process.env.NODE_ENV !== "production") { - agent.fetchRootKey().catch(err=>{ - console.warn("Unable to fetch root key. Check to ensure that your local replica is running"); - console.error(err); - }); - } - - // Creates an actor with using the candid interface and the HttpAgent - return Actor.createActor(idlFactory, { - agent, - canisterId, - ...options?.actorOptions, - }); -}; - -/** - * A ready-to-use agent for the small_nft_assets canister - * @type {import("@dfinity/agent").ActorSubclass} - */ - export const small_nft_assets = createActor(canisterId); diff --git a/src/declarations/small_nft_assets/small_nft_assets.did b/src/declarations/small_nft_assets/small_nft_assets.did deleted file mode 100644 index d11ecd9..0000000 --- a/src/declarations/small_nft_assets/small_nft_assets.did +++ /dev/null @@ -1,140 +0,0 @@ -type BatchId = nat; -type ChunkId = nat; -type Key = text; -type Time = int; - -type CreateAssetArguments = record { - key: Key; - content_type: text; -}; - -// Add or change content for an asset, by content encoding -type SetAssetContentArguments = record { - key: Key; - content_encoding: text; - chunk_ids: vec ChunkId; - sha256: opt blob; -}; - -// Remove content for an asset, by content encoding -type UnsetAssetContentArguments = record { - key: Key; - content_encoding: text; -}; - -// Delete an asset -type DeleteAssetArguments = record { - key: Key; -}; - -// Reset everything -type ClearArguments = record {}; - -type BatchOperationKind = variant { - CreateAsset: CreateAssetArguments; - SetAssetContent: SetAssetContentArguments; - - UnsetAssetContent: UnsetAssetContentArguments; - DeleteAsset: DeleteAssetArguments; - - Clear: ClearArguments; -}; - -type HeaderField = record { text; text; }; - -type HttpRequest = record { - method: text; - url: text; - headers: vec HeaderField; - body: blob; -}; - -type HttpResponse = record { - status_code: nat16; - headers: vec HeaderField; - body: blob; - streaming_strategy: opt StreamingStrategy; -}; - -type StreamingCallbackHttpResponse = record { - body: blob; - token: opt StreamingCallbackToken; -}; - -type StreamingCallbackToken = record { - key: Key; - content_encoding: text; - index: nat; - sha256: opt blob; -}; - -type StreamingStrategy = variant { - Callback: record { - callback: func (StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; - token: StreamingCallbackToken; - }; -}; - -service: { - - get: (record { - key: Key; - accept_encodings: vec text; - }) -> (record { - content: blob; // may be the entirety of the content, or just chunk index 0 - content_type: text; - content_encoding: text; - sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments - total_length: nat; // all chunks except last have size == content.size() - }) query; - - // if get() returned chunks > 1, call this to retrieve them. - // chunks may or may not be split up at the same boundaries as presented to create_chunk(). - get_chunk: (record { - key: Key; - content_encoding: text; - index: nat; - sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments - }) -> (record { content: blob }) query; - - list : (record {}) -> (vec record { - key: Key; - content_type: text; - encodings: vec record { - content_encoding: text; - sha256: opt blob; // sha256 of entire asset encoding, calculated by dfx and passed in SetAssetContentArguments - length: nat; // Size of this encoding's blob. Calculated when uploading assets. - modified: Time; - }; - }) query; - - create_batch : (record {}) -> (record { batch_id: BatchId }); - - create_chunk: (record { batch_id: BatchId; content: blob }) -> (record { chunk_id: ChunkId }); - - // Perform all operations successfully, or reject - commit_batch: (record { batch_id: BatchId; operations: vec BatchOperationKind }) -> (); - - create_asset: (CreateAssetArguments) -> (); - set_asset_content: (SetAssetContentArguments) -> (); - unset_asset_content: (UnsetAssetContentArguments) -> (); - - delete_asset: (DeleteAssetArguments) -> (); - - clear: (ClearArguments) -> (); - - // Single call to create an asset with content for a single content encoding that - // fits within the message ingress limit. - store: (record { - key: Key; - content_type: text; - content_encoding: text; - content: blob; - sha256: opt blob - }) -> (); - - http_request: (request: HttpRequest) -> (HttpResponse) query; - http_request_streaming_callback: (token: StreamingCallbackToken) -> (opt StreamingCallbackHttpResponse) query; - - authorize: (principal) -> (); -} diff --git a/src/declarations/small_nft_assets/small_nft_assets.did.d.ts b/src/declarations/small_nft_assets/small_nft_assets.did.d.ts deleted file mode 100644 index 990bf87..0000000 --- a/src/declarations/small_nft_assets/small_nft_assets.did.d.ts +++ /dev/null @@ -1,117 +0,0 @@ -import type { Principal } from '@dfinity/principal'; -export type BatchId = bigint; -export type BatchOperationKind = { 'CreateAsset' : CreateAssetArguments } | - { 'UnsetAssetContent' : UnsetAssetContentArguments } | - { 'DeleteAsset' : DeleteAssetArguments } | - { 'SetAssetContent' : SetAssetContentArguments } | - { 'Clear' : ClearArguments }; -export type ChunkId = bigint; -export type ClearArguments = {}; -export interface CreateAssetArguments { 'key' : Key, 'content_type' : string } -export interface DeleteAssetArguments { 'key' : Key } -export type HeaderField = [string, string]; -export interface HttpRequest { - 'url' : string, - 'method' : string, - 'body' : Array, - 'headers' : Array, -} -export interface HttpResponse { - 'body' : Array, - 'headers' : Array, - 'streaming_strategy' : [] | [StreamingStrategy], - 'status_code' : number, -} -export type Key = string; -export interface SetAssetContentArguments { - 'key' : Key, - 'sha256' : [] | [Array], - 'chunk_ids' : Array, - 'content_encoding' : string, -} -export interface StreamingCallbackHttpResponse { - 'token' : [] | [StreamingCallbackToken], - 'body' : Array, -} -export interface StreamingCallbackToken { - 'key' : Key, - 'sha256' : [] | [Array], - 'index' : bigint, - 'content_encoding' : string, -} -export type StreamingStrategy = { - 'Callback' : { - 'token' : StreamingCallbackToken, - 'callback' : [Principal, string], - } - }; -export type Time = bigint; -export interface UnsetAssetContentArguments { - 'key' : Key, - 'content_encoding' : string, -} -export interface _SERVICE { - 'authorize' : (arg_0: Principal) => Promise, - 'clear' : (arg_0: ClearArguments) => Promise, - 'commit_batch' : ( - arg_0: { 'batch_id' : BatchId, 'operations' : Array }, - ) => Promise, - 'create_asset' : (arg_0: CreateAssetArguments) => Promise, - 'create_batch' : (arg_0: {}) => Promise<{ 'batch_id' : BatchId }>, - 'create_chunk' : ( - arg_0: { 'content' : Array, 'batch_id' : BatchId }, - ) => Promise<{ 'chunk_id' : ChunkId }>, - 'delete_asset' : (arg_0: DeleteAssetArguments) => Promise, - 'get' : ( - arg_0: { 'key' : Key, 'accept_encodings' : Array }, - ) => Promise< - { - 'content' : Array, - 'sha256' : [] | [Array], - 'content_type' : string, - 'content_encoding' : string, - 'total_length' : bigint, - } - >, - 'get_chunk' : ( - arg_0: { - 'key' : Key, - 'sha256' : [] | [Array], - 'index' : bigint, - 'content_encoding' : string, - }, - ) => Promise<{ 'content' : Array }>, - 'http_request' : (arg_0: HttpRequest) => Promise, - 'http_request_streaming_callback' : ( - arg_0: StreamingCallbackToken, - ) => Promise<[] | [StreamingCallbackHttpResponse]>, - 'list' : (arg_0: {}) => Promise< - Array< - { - 'key' : Key, - 'encodings' : Array< - { - 'modified' : Time, - 'sha256' : [] | [Array], - 'length' : bigint, - 'content_encoding' : string, - } - >, - 'content_type' : string, - } - > - >, - 'set_asset_content' : (arg_0: SetAssetContentArguments) => Promise, - 'store' : ( - arg_0: { - 'key' : Key, - 'content' : Array, - 'sha256' : [] | [Array], - 'content_type' : string, - 'content_encoding' : string, - }, - ) => Promise, - 'unset_asset_content' : (arg_0: UnsetAssetContentArguments) => Promise< - undefined - >, -} diff --git a/src/declarations/small_nft_assets/small_nft_assets.did.js b/src/declarations/small_nft_assets/small_nft_assets.did.js deleted file mode 100644 index 46f975e..0000000 --- a/src/declarations/small_nft_assets/small_nft_assets.did.js +++ /dev/null @@ -1,155 +0,0 @@ -export const idlFactory = ({ IDL }) => { - const ClearArguments = IDL.Record({}); - const BatchId = IDL.Nat; - const Key = IDL.Text; - const CreateAssetArguments = IDL.Record({ - 'key' : Key, - 'content_type' : IDL.Text, - }); - const UnsetAssetContentArguments = IDL.Record({ - 'key' : Key, - 'content_encoding' : IDL.Text, - }); - const DeleteAssetArguments = IDL.Record({ 'key' : Key }); - const ChunkId = IDL.Nat; - const SetAssetContentArguments = IDL.Record({ - 'key' : Key, - 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), - 'chunk_ids' : IDL.Vec(ChunkId), - 'content_encoding' : IDL.Text, - }); - const BatchOperationKind = IDL.Variant({ - 'CreateAsset' : CreateAssetArguments, - 'UnsetAssetContent' : UnsetAssetContentArguments, - 'DeleteAsset' : DeleteAssetArguments, - 'SetAssetContent' : SetAssetContentArguments, - 'Clear' : ClearArguments, - }); - const HeaderField = IDL.Tuple(IDL.Text, IDL.Text); - const HttpRequest = IDL.Record({ - 'url' : IDL.Text, - 'method' : IDL.Text, - 'body' : IDL.Vec(IDL.Nat8), - 'headers' : IDL.Vec(HeaderField), - }); - const StreamingCallbackToken = IDL.Record({ - 'key' : Key, - 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), - 'index' : IDL.Nat, - 'content_encoding' : IDL.Text, - }); - const StreamingCallbackHttpResponse = IDL.Record({ - 'token' : IDL.Opt(StreamingCallbackToken), - 'body' : IDL.Vec(IDL.Nat8), - }); - const StreamingStrategy = IDL.Variant({ - 'Callback' : IDL.Record({ - 'token' : StreamingCallbackToken, - 'callback' : IDL.Func( - [StreamingCallbackToken], - [IDL.Opt(StreamingCallbackHttpResponse)], - ['query'], - ), - }), - }); - const HttpResponse = IDL.Record({ - 'body' : IDL.Vec(IDL.Nat8), - 'headers' : IDL.Vec(HeaderField), - 'streaming_strategy' : IDL.Opt(StreamingStrategy), - 'status_code' : IDL.Nat16, - }); - const Time = IDL.Int; - return IDL.Service({ - 'authorize' : IDL.Func([IDL.Principal], [], []), - 'clear' : IDL.Func([ClearArguments], [], []), - 'commit_batch' : IDL.Func( - [ - IDL.Record({ - 'batch_id' : BatchId, - 'operations' : IDL.Vec(BatchOperationKind), - }), - ], - [], - [], - ), - 'create_asset' : IDL.Func([CreateAssetArguments], [], []), - 'create_batch' : IDL.Func( - [IDL.Record({})], - [IDL.Record({ 'batch_id' : BatchId })], - [], - ), - 'create_chunk' : IDL.Func( - [IDL.Record({ 'content' : IDL.Vec(IDL.Nat8), 'batch_id' : BatchId })], - [IDL.Record({ 'chunk_id' : ChunkId })], - [], - ), - 'delete_asset' : IDL.Func([DeleteAssetArguments], [], []), - 'get' : IDL.Func( - [IDL.Record({ 'key' : Key, 'accept_encodings' : IDL.Vec(IDL.Text) })], - [ - IDL.Record({ - 'content' : IDL.Vec(IDL.Nat8), - 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), - 'content_type' : IDL.Text, - 'content_encoding' : IDL.Text, - 'total_length' : IDL.Nat, - }), - ], - ['query'], - ), - 'get_chunk' : IDL.Func( - [ - IDL.Record({ - 'key' : Key, - 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), - 'index' : IDL.Nat, - 'content_encoding' : IDL.Text, - }), - ], - [IDL.Record({ 'content' : IDL.Vec(IDL.Nat8) })], - ['query'], - ), - 'http_request' : IDL.Func([HttpRequest], [HttpResponse], ['query']), - 'http_request_streaming_callback' : IDL.Func( - [StreamingCallbackToken], - [IDL.Opt(StreamingCallbackHttpResponse)], - ['query'], - ), - 'list' : IDL.Func( - [IDL.Record({})], - [ - IDL.Vec( - IDL.Record({ - 'key' : Key, - 'encodings' : IDL.Vec( - IDL.Record({ - 'modified' : Time, - 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), - 'length' : IDL.Nat, - 'content_encoding' : IDL.Text, - }) - ), - 'content_type' : IDL.Text, - }) - ), - ], - ['query'], - ), - 'set_asset_content' : IDL.Func([SetAssetContentArguments], [], []), - 'store' : IDL.Func( - [ - IDL.Record({ - 'key' : Key, - 'content' : IDL.Vec(IDL.Nat8), - 'sha256' : IDL.Opt(IDL.Vec(IDL.Nat8)), - 'content_type' : IDL.Text, - 'content_encoding' : IDL.Text, - }), - ], - [], - [], - ), - 'unset_asset_content' : IDL.Func([UnsetAssetContentArguments], [], []), - }); -}; -export const init = ({ IDL }) => { return []; };