SChain Javascript SDK
透過 SChain Javascript SDK,開發者可撰寫應用程式來存取區塊鍊中的 Chaincode 代碼。
前置作業
開始使用 SDK 之前,你需要先在開發者中心取得 API KEY 及 APP ID。
若你尚未閱讀 Get started,我們強烈建議你立即前往。
安裝套件
npm i schain_sdk
引入套件
const schain = ;
初始化
// 請貼上你的 API KEY 及 APP IDconst API_KEY = '5d5b9cbd55cc6725f82dabba0632fe6e';const APP_ID = 'app-38b30623-c207-4025-8c80-69df51f822c2'; schain
開啟/關閉Debug Mode
開啟Debug mode, 將提供SDK運行的Log
//開啟Debug modeschain //關閉Debug modeschain
資料儲存樣板
SDK 特別提供了存取 資料儲存樣板 的函數接口。
寫入一個 key-value 鍵值
let key = 'key1';let value = '100'; await schain;
取得一個鍵值
let key = 'key1'; await schain;
刪除一個鍵值
let key = 'key1'; await schain;
存取自行撰寫的 Chaincode 代碼
SDK 也提供了方法來存取你自行撰寫的 Chaincode 代碼函數。
註冊使用者身份
若你的應用程式需要區隔不同的使用者身份,可以使用 register 函數來創建使用者身份。
let username = 'user01'; await schain;
更新分散式帳本
透過調用 Chaincode 函數來更新分散式帳本。
// 指名要用來調用此函數的使用者身份// 可設定為 null,表示要調用的函數無需區分使用者身份let username = 'user01'; // 要調用的函數名稱let func = 'invoke'; // 函數參數let args = 'a' 'b' 'c'; await schain;
查詢分散式帳本
透過調用 Chaincode 函數來查詢分散式帳本。
// 指名要用來調用此函數的使用者身份// 可設定為 null,表示要調用的函數無需區分使用者身份let username = 'user01'; // 要調用的函數名稱let func = 'query'; // 函數參數let args = 'a'; await schain;
檔案儲存管理
SChain 為每個開發者帳號提供了 100MB 的檔案儲存空間。
所有儲存在 SChain 的檔案皆受到區塊鏈的嚴格保護,你可以透過 SDK 來管理你的檔案,也可以檢驗檔案的不可竄改性。
上傳檔案
// file key為檔案的id, 由開發者自行定義let file_key = "file.jpg"; // 上傳的檔案路徑let file_path = "./file.jpg"; await schain;
刪除檔案
// 檔案的key值let file_key = "file.jpg"; await schain;
取得檔案 Hash 值
// 檔案的key值let file_key = "file.jpg"; await schain;
下載檔案
// 檔案的key值let file_key = "file.jpg"; // 存放下載檔案的目錄, 本範例會在根目錄下建立 download 資料夾存放檔案let path = "./download"; await schain;
驗證檔案完整性
// 檔案的key值var file_key = "file.jpg"; // 要驗證檔案的路徑var file_path = "./test/file.jpg"; await schain;
取得檔案清單
// options 為取得檔案清單規則的條件設定var options = maxKeys: 1000 prefix: "prefix" startAfter: "file_key" continuationToken: "token" delimiter: "group_key" encodingType: "url"; // 設定檔案清單規則await schain; // 不設定檔案清單規則await schain;
關於 options 的詳細設定可參考 AWS S3說明文件
License
Copyright 2019 S-Chain Technologies Limited
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.