Keyship SDK
π₯Read firstβ
Keyship protocol will protect the secret, which cannot be longer than 1024 characters, this can be an URL of a unencrypted data, an URL of a encrypted data(in this case the key used to encrypt the data need to be used to perform the secret encryption as well) or a key you used to encrypt some data as you want.
Installβ
yarn add @keyship/sdk
or
npm install @keyship/sdk
Usageβ
Import and instantiateβ
import Keyship, { INft, ISell, IToken } from "@keyship/sdk";
const kship = new Keyship();
Check if user have already fulfilled the Rule of the secretβ
// itemData is dataMetaDescription from the create secret section
// userWalletAddress is the user wallet address
// chain is the chain user is auth
const canAccess = await kship.canOpen(
itemData,
userWalletAddress,
kship.CHAINS.POLYGON
);
if (canAccess) {
// user can access the secret
// auth user and decrypt the secret
// check the secret section below
} else {
// user can't access the secret
// show to the user the Item Data
}
Generate random Keyβ
const someKey = kship.generateKey();
Local encryption (AES256)β
const encryptedText = kship.encrypt("My secret", someKey);
// or encrypt a file
const encryptedFile = kship.encrypt(myFile, someKey);
Local decryption (AES256)β
const decryptedText = kship.decrypt(encryptedText, someKey);
// or decrypt a file
const decryptedFile = kship.encrypt(encryptedFile, someKey);
Authβ
You need to auth the user before each action: creating a item, buying a item, opening a item.
const token = await kship.auth();
const signature = await web3.eth.personal.sign(token, accounts[0]);
// finalSignature need to be used on the interaction with Keyship netowork
const finalSignature = kship.formatSignature(token, accounts[0], signature);
Secretsβ
The 2 most important functions in Keyship SDK are NewSecret and OpenSecret. By passing your key or URL as the secret property(see example below) among the rule of decryption you can easly allow anyone to access the secret.
Secret using Paying/Sell Ruleβ
Createβ
const mySecret = "https://urltosecretdata"; // or a key
const itemSell: ISell = {
name: "Some name", // optional
description: "Some description", // optional
owner: accounts[0], // creator user wallet address
price: "0.01", // price in $
secret: mySecret,
signature: finalSignature,
rule: kship.RULES.SELL,
};
const dataMetaDescription = await kship.NewSecret(itemSell);
// you can store the dataMetaDescription anywhere.
// In this case we will upload it to IPFS
const ipfsCID = await kship.uploadToIPFS(dataMetaDescription);
Buy and open the secretβ
const ipfsURL = kship.getIPFSUrl(ipfsCID);
// getting the dataMetaDescription from IPFS
const dataMetaDescription = await kship.fetchItem(ipfsURL);
// getting the txData to buy the secret from the Keyship network
const txData = await kship.getSellTxData(
dataMetaDescription,
finalSignature,
kship.CHAINS.POLYGON // This rule is only available on Polygon, Ethereum and Binance Smart Chain
);
const gasPrice = await web3.eth.getGasPrice();
const networkId = await web3.eth.net.getId();
const contract = new web3.eth.Contract(
kship.contracts.proxy.abi,
kship.contracts.proxy.networks[networkId] && kship.contracts.proxy.address
);
const gas = await contract.methods
.buyProduct(txData.seller, txData.id, txData.signature, txData.mid)
.estimateGas({ from: accounts[0], value: txData.value });
// Make the payment
const execTx = await contract.methods
.buyProduct(txData.seller, txData.id, txData.signature, txData.mid)
.send({
from: accounts[0],
value: txData.value,
gasPrice: gasPrice,
gas: gas,
});
if (execTx.transactionHash) {
// After payment is done, decrypt the secret
const decryptedData = await kship.OpenSecret(
dataMetaDescription,
finalSignature,
kship.CHAINS.POLYGON // This rule is only available on Polygon, Ethereum and Binance Smart Chain, same as user auth chain.
);
console.log(decryptedData);
}
Secret using NFT Rule (Like a Paywall/Gate)β
Createβ
const mySecret = "https://urltosecretdata"; // or a key
const itemNft: INft = {
owner: accounts[0], // creator user wallet address
contract: "0x828383B51514873CC937ee83F57fBbff0221700C", // NFT contract address
chain: sdk.CHAINS.POLYGON, // !!! IMPORTANT this chain need to be the same as the NFT contract chain
secret: mySecret,
signature: finalSignature,
rule: kship.RULES.NFT,
};
const dataMetaDescription = await kship.NewSecret(itemNft);
// you can store the dataMetaDescription anywhere.
// In this case we will upload it to IPFS
const ipfsCID = await kship.uploadToIPFS(dataMetaDescription);
Open the secret with NFTβ
const ipfsURL = kship.getIPFSUrl(ipfsCID);
// getting the dataMetaDescription from IPFS
const dataMetaDescription = await kship.fetchItem(ipfsURL);
const decryptedData = await kship.OpenSecret(
dataMetaDescription,
finalSignature,
kship.CHAINS.POLYGON // is the chain user is auth
);
console.log(decryptedData);
Secret using Token Balance Ruleβ
Createβ
const mySecret = "https://urltosecretdata"; // or a key
const itemToken: IToken = {
owner: accounts[0], // creator user wallet address
contract: "0x828383B51514873CC937ee83F57fBbff0221700C", // Token/ERC20 contract address
minBalance: "100", // 100 tokens - minimum balance of the token to open the secret
chain: sdk.CHAINS.POLYGON, // !!! IMPORTANT this chain need to be the same as the Token/ERC20 contract chain
secret: mySecret,
signature: finalSignature,
rule: kship.RULES.TOKEN,
};
const dataMetaDescription = await kship.NewSecret(itemToken);
// you can store the dataMetaDescription anywhere.
// In this case we will upload it to IPFS
const ipfsCID = await kship.uploadToIPFS(dataMetaDescription);
Open the secret with Token Balanceβ
const ipfsURL = kship.getIPFSUrl(ipfsCID);
// getting the dataMetaDescription from IPFS
const dataMetaDescription = await kship.fetchItem(ipfsURL);
const decryptedData = await kship.OpenSecret(
dataMetaDescription,
finalSignature,
kship.CHAINS.POLYGON // is the chain user is auth
);
console.log(decryptedData);