Skip to content

Commit

Permalink
Merge pull request #16 from keypom/ben/error-checks
Browse files Browse the repository at this point in the history
Error Checks, Usability Updates, Bug Fixes
  • Loading branch information
mattlockyer authored Jan 17, 2023
2 parents cfe9084 + 55e3184 commit adf4862
Show file tree
Hide file tree
Showing 21 changed files with 907 additions and 289 deletions.
3 changes: 2 additions & 1 deletion lib/components/KeypomContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ var __importStar = (this && this.__importStar) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useKeypom = exports.KeypomContextProvider = void 0;
var react_1 = __importStar(require("../../../keypom-app/node_modules/react/index"));
var react_1 = __importStar(require("react"));
var keypom_1 = require("../lib/keypom");
var KeypomContext = react_1.default.createContext(null);
var KeypomContextProvider = function (_a) {
var children = _a.children;
var _b = (0, react_1.useState)(null), env = _b[0], setEnv = _b[1];
(0, react_1.useEffect)(function () {
// try to call getEnv of Keypom SDK which will throw if initKeypom isn't called somewhere in client codebase
var tried = 0;
var attempts = 10, timeout = 1000;
var lazyCheck = function () {
Expand Down
25 changes: 25 additions & 0 deletions lib/lib/checks.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Account, Near } from "near-api-js";
import { FCData } from "./types/fc";
import { Funder } from "./types/general";
export declare function isValidAccountObj(o: Account | undefined): o is Account;
export declare function isValidNearObject(o: Near): o is Near;
export declare function isValidFunderObject(o: Funder): o is Funder;
export declare const assert: (exp: any, m: any) => void;
export declare const assertValidDropConfig: (config: {
uses_per_key?: number;
time?: {
start?: number;
end?: number;
throttle?: number;
interval?: number;
};
usage?: {
permission?: string;
refund_deposit?: boolean;
auto_delete_drop?: boolean;
auto_withdraw?: boolean;
};
root_account_id?: string;
}) => void;
export declare const assertValidFCData: (fcData: FCData | undefined, depositPerUse: string, usesPerKey: number) => void;
export declare const assertDropIdUnique: (dropId: string) => Promise<void>;
130 changes: 130 additions & 0 deletions lib/lib/checks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertDropIdUnique = exports.assertValidFCData = exports.assertValidDropConfig = exports.assert = exports.isValidFunderObject = exports.isValidNearObject = exports.isValidAccountObj = void 0;
var bn_js_1 = __importDefault(require("bn.js"));
var keypom_1 = require("./keypom");
function isValidAccountObj(o) {
if (o) {
return o.connection !== undefined && o.accountId !== undefined;
}
return true;
}
exports.isValidAccountObj = isValidAccountObj;
function isValidNearObject(o) {
return o.connection !== undefined && o.config !== undefined && o.accountCreator !== undefined;
}
exports.isValidNearObject = isValidNearObject;
function isValidFunderObject(o) {
return o.accountId !== undefined && o.secretKey !== undefined;
}
exports.isValidFunderObject = isValidFunderObject;
var assert = function (exp, m) {
if (!exp) {
throw new Error(m);
}
};
exports.assert = assert;
var assertValidDropConfig = function (config) {
var _a;
(0, exports.assert)(((config === null || config === void 0 ? void 0 : config.uses_per_key) || 1) != 0, "Cannot have 0 uses per key for a drop config");
if ((_a = config === null || config === void 0 ? void 0 : config.usage) === null || _a === void 0 ? void 0 : _a.permission) {
(0, exports.assert)(config.usage.permission == "create_account_and_claim" || config.usage.permission == "claim", "Invalid permission type for usage. Must be 'create_account_and_claim' or 'claim'");
}
if (config === null || config === void 0 ? void 0 : config.time) {
var currentBlockTimestamp = Date.now() * 1e6;
(0, exports.assert)((config.time.interval != undefined && config.time.start != undefined) == false, "If you want to set a claim interval, you must also set a start timestamp");
(0, exports.assert)((config.time.start || currentBlockTimestamp) >= currentBlockTimestamp, "The start timestamp must be greater than the current block timestamp");
exports.assert((config.time.end || currentBlockTimestamp) >= currentBlockTimestamp, "The end timestamp must be greater than the current block timestamp");
if (config.time.start != undefined && config.time.end != undefined) {
(0, exports.assert)(config.time.start < config.time.end, "The start timestamp must be less than the end timestamp");
}
}
};
exports.assertValidDropConfig = assertValidDropConfig;
var assertValidFCData = function (fcData, depositPerUse, usesPerKey) {
var _a;
if ((_a = fcData === null || fcData === void 0 ? void 0 : fcData.config) === null || _a === void 0 ? void 0 : _a.attachedGas) {
(0, exports.assert)(depositPerUse == "0", "Cannot specify gas to attach and have a balance in the linkdrop");
(0, exports.assert)(new bn_js_1.default(fcData.config.attachedGas).lte(new bn_js_1.default("80000000000000")), "Cannot have 0 attached gas");
}
if (fcData === null || fcData === void 0 ? void 0 : fcData.methods) {
var numMethodData = fcData.methods.length;
if (usesPerKey == 1) {
(0, exports.assert)(numMethodData == 1, "Cannot have more Method Data than the number of uses per key");
}
else if (usesPerKey > 1) {
(0, exports.assert)(numMethodData == usesPerKey, "Number of FCs must match number of uses per key if more than 1 is specified");
}
if (usesPerKey > 1 && numMethodData == 1) {
(0, exports.assert)(fcData.methods[0] != undefined, "cannot have a single none function call");
}
}
};
exports.assertValidFCData = assertValidFCData;
var assertDropIdUnique = function (dropId) { return __awaiter(void 0, void 0, void 0, function () {
var _a, viewAccount, contractId, dropInfo, _1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = (0, keypom_1.getEnv)(), viewAccount = _a.viewAccount, contractId = _a.contractId;
_b.label = 1;
case 1:
_b.trys.push([1, 3, , 4]);
return [4 /*yield*/, viewAccount.viewFunction2({
contractId: contractId,
methodName: 'get_drop_information',
args: {
drop_id: dropId
}
})];
case 2:
dropInfo = _b.sent();
(0, exports.assert)(!dropInfo, "Drop with ID ".concat(dropId, " already exists. Please use a different drop ID."));
return [3 /*break*/, 4];
case 3:
_1 = _b.sent();
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
}); };
exports.assertDropIdUnique = assertDropIdUnique;
17 changes: 6 additions & 11 deletions lib/lib/claims.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.claim = void 0;
var nearAPI = __importStar(require("near-api-js"));
var checks_1 = require("./checks");
var KeyPair = nearAPI.KeyPair;
var keypom_1 = require("./keypom");
/**
Expand Down Expand Up @@ -164,24 +165,18 @@ var keypom_1 = require("./keypom");
var claim = function (_a) {
var secretKey = _a.secretKey, accountId = _a.accountId, newAccountId = _a.newAccountId, newPublicKey = _a.newPublicKey, password = _a.password;
return __awaiter(void 0, void 0, void 0, function () {
var _b, networkId, keyStore, attachedGas, contractId, contractAccount, receiverId, execute, fundingAccountDetails, keyPair, transactions, result;
var _b, networkId, keyStore, attachedGas, contractId, contractAccount, receiverId, execute, fundingAccountDetails, near, keyPair, transactions, result;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_b = (0, keypom_1.getEnv)(), networkId = _b.networkId, keyStore = _b.keyStore, attachedGas = _b.attachedGas, contractId = _b.contractId, contractAccount = _b.contractAccount, receiverId = _b.receiverId, execute = _b.execute, fundingAccountDetails = _b.fundingAccountDetails;
if (!keyStore) {
throw new Error('Keypom SDK is not initialized. Please call `initKeypom`.');
}
_b = (0, keypom_1.getEnv)(), networkId = _b.networkId, keyStore = _b.keyStore, attachedGas = _b.attachedGas, contractId = _b.contractId, contractAccount = _b.contractAccount, receiverId = _b.receiverId, execute = _b.execute, fundingAccountDetails = _b.fundingAccountDetails, near = _b.near;
(0, checks_1.assert)(near && networkId && keyStore, 'Keypom SDK is not initialized. Please call `initKeypom`.');
keyPair = KeyPair.fromString(secretKey);
return [4 /*yield*/, keyStore.setKey(networkId, contractId, keyPair)];
case 1:
_c.sent();
if (newAccountId && !newPublicKey) {
throw new Error('If creating a new account, a newPublicKey must be passed in.');
}
if (!newAccountId && !accountId) {
throw new Error('Either an accountId or newAccountId must be passed in.');
}
(0, checks_1.assert)(!newAccountId || newPublicKey, 'If creating a new account, a newPublicKey must be passed in.');
(0, checks_1.assert)(newAccountId || accountId, 'Either an accountId or newAccountId must be passed in.');
transactions = [{
receiverId: receiverId,
actions: [{
Expand Down
Loading

0 comments on commit adf4862

Please sign in to comment.