UNPKG

@tokenlon/contracts-lib

Version:

Common utility functions (e.g., signing order, encoding data) to interact with [Tokenlon contracts](https://github.com/consenlabs/tokenlon-contracts).

65 lines 2.94 kB
"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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SigningHelper = void 0; const crypto_1 = __importDefault(require("crypto")); const ethers_1 = require("ethers"); const hash_1 = require("@ethersproject/hash"); class SigningHelper { constructor(options) { this.name = options.name; this.version = options.version; } generateRandomSalt() { const randomBytes = crypto_1.default.randomBytes(32); return ethers_1.BigNumber.from(randomBytes); } getEIP712Domain(options) { return __awaiter(this, void 0, void 0, function* () { return { name: this.name, version: this.version, chainId: options.chainId, verifyingContract: options.verifyingContract, }; }); } // Reference: https://eips.ethereum.org/EIPS/eip-712#specification // "\x19\x01" ‖ hashStruct(domain) ‖ hashStruct(types, value) getEIP712Digest(domain, types, value) { return hash_1._TypedDataEncoder.hash(domain, types, value); } // Reference: https://eips.ethereum.org/EIPS/eip-712#definition-of-hashstruct // keccak256(keccak256(encodeType(types[name]) ‖ encodeData(value)) getEIP712StructHash(name, types, value) { return hash_1._TypedDataEncoder.hashStruct(name, types, value); } signEIP712(types, value, options) { return __awaiter(this, void 0, void 0, function* () { const domain = yield this.getEIP712Domain({ chainId: yield options.signer.getChainId(), verifyingContract: options.verifyingContract, }); const signature = yield options.signer._signTypedData(domain, types, value); const signatureComposed = this.composeSignature(signature, options.type); return signatureComposed; }); } composeSignature(signature, type) { const paddedNonce = "00".repeat(32); return signature + paddedNonce + type; } } exports.SigningHelper = SigningHelper; //# sourceMappingURL=signing.js.map