scryptlib
Advanced tools
Comparing version 0.2.18-1 to 0.2.18-beta
@@ -20,3 +20,4 @@ import { AbstractContract, TxContext, VerifyResult, AsmVarValues } from './contract'; | ||
} | ||
export declare type SupportedParamType = ScryptType | boolean | number | bigint; | ||
export declare type SingletonParamType = ScryptType | boolean | number | bigint; | ||
export declare type SupportedParamType = SingletonParamType | SingletonParamType[]; | ||
export declare class FunctionCall { | ||
@@ -48,4 +49,4 @@ methodName: string; | ||
encodeParams(args: SupportedParamType[], scryptTypeNames: string[]): string; | ||
encodeParamArray(args: unknown[], scryptTypeName: string): string; | ||
encodeParamArray(args: SingletonParamType[], arrayTypeName: string): string; | ||
encodeParam(arg: SupportedParamType, scryptTypeName: string): string; | ||
} |
@@ -12,2 +12,11 @@ "use strict"; | ||
})(ABIEntityType = exports.ABIEntityType || (exports.ABIEntityType = {})); | ||
function escapeRegExp(stringToGoIntoTheRegex) { | ||
return stringToGoIntoTheRegex.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&'); | ||
} | ||
function arrayTypeAndSize(arrayTypeName) { | ||
const group = arrayTypeName.split('['); | ||
const elemTypeName = group[0]; | ||
const arraySize = parseInt(group[1].slice(0, -1)); | ||
return [elemTypeName, arraySize]; | ||
} | ||
class FunctionCall { | ||
@@ -79,10 +88,30 @@ constructor(methodName, params, binding) { | ||
} | ||
// handle array type | ||
const cParams_ = []; | ||
const args_ = []; | ||
cParams.forEach((param, index) => { | ||
const arg = args[index]; | ||
if (Array.isArray(arg)) { | ||
const [elemTypeName, arraySize] = arrayTypeAndSize(param.type); | ||
if (arraySize !== arg.length) { | ||
throw new Error(`Array arguments wrong size for '${param.name}' in constructor, expected [${arraySize}] but got [${arg.length}]`); | ||
} | ||
// flattern array | ||
arg.forEach((e, idx) => { | ||
cParams_.push({ name: `${param.name}[${idx}]`, type: elemTypeName }); | ||
args_.push(e); | ||
}); | ||
} | ||
else { | ||
cParams_.push(param); | ||
args_.push(arg); | ||
} | ||
}); | ||
let lsASM = asmTemplate; | ||
cParams.forEach((param, index) => { | ||
cParams_.forEach((param, index) => { | ||
if (!asmTemplate.includes(`$${param.name}`)) { | ||
throw new Error(`abi constructor params mismatch with args provided: missing ${param.name} in ASM tempalte`); | ||
} | ||
// '$' needs doulbe '\\' to escape | ||
const re = new RegExp(`\\$${param.name}`, 'g'); | ||
lsASM = lsASM.replace(re, this.encodeParam(args[index], param.type)); | ||
const re = new RegExp(escapeRegExp(`$${param.name}`), 'g'); | ||
lsASM = lsASM.replace(re, this.encodeParam(args_[index], param.type)); | ||
}); | ||
@@ -111,6 +140,5 @@ return new FunctionCall('constructor', args, { contract, lockingScriptASM: lsASM }); | ||
} | ||
encodeParamArray(args, scryptTypeName) { | ||
encodeParamArray(args, arrayTypeName) { | ||
if (args.length === 0) { | ||
// empty | ||
return 'OP_0'; | ||
throw new Error('Empty array not allowed'); | ||
} | ||
@@ -121,16 +149,7 @@ const t = typeof args[0]; | ||
} | ||
// serialize array | ||
if (t === 'number') { | ||
if ('int[]' !== scryptTypeName) { | ||
throw new Error(`wrong argument type, expected ${scryptTypeName} but got int[]`); | ||
} | ||
return args.map(n => utils_1.num2bin(n, utils_1.IntElemLen)).join(''); | ||
const [elemTypeName, arraySize] = arrayTypeAndSize(arrayTypeName); | ||
if (arraySize !== args.length) { | ||
throw new Error(`Array arguments wrong size, expected [${arraySize}] but got [${args.length}]`); | ||
} | ||
if (t === 'boolean') { | ||
if ('bool[]' !== scryptTypeName) { | ||
throw new Error(`wrong argument type, expected ${scryptTypeName} but got bool[]`); | ||
} | ||
return args.map(flag => flag ? '01' : '00').join(''); | ||
} | ||
throw new Error(`array of ${t} is not supported`); | ||
return args.map(arg => this.encodeParam(arg, elemTypeName)).join(' '); | ||
} | ||
@@ -137,0 +156,0 @@ encodeParam(arg, scryptTypeName) { |
@@ -0,0 +0,0 @@ import { ABIEntity } from './abi'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { ABICoder, ABIEntity, FunctionCall, Script } from "./abi"; |
@@ -0,0 +0,0 @@ "use strict"; |
export { buildContractClass, VerifyResult } from './contract'; | ||
export { compile } from './compilerWrapper'; | ||
export { bsv, signTx, toHex, getPreimage, num2bin, bin2num, bool2Asm, int2Asm, literal2Asm, bytes2Literal, bytesToHexString, getValidatedHexString } from './utils'; | ||
export { bsv, signTx, toHex, getPreimage, num2bin, bin2num } from './utils'; | ||
export { serializeState, deserializeState, State, STATE_LEN_2BYTES, STATE_LEN_4BYTES } from './serializer'; | ||
export { Int, Bool, Byte, Bytes, PrivKey, PubKey, Sig, Ripemd160, Sha1, Sha256, SigHashType, SigHashPreimage, OpCodeType } from './scryptTypes'; | ||
export { Int, Bool, Bytes, PrivKey, PubKey, Sig, Ripemd160, Sha1, Sha256, SigHashType, SigHashPreimage, OpCodeType } from './scryptTypes'; |
@@ -14,8 +14,2 @@ "use strict"; | ||
Object.defineProperty(exports, "bin2num", { enumerable: true, get: function () { return utils_1.bin2num; } }); | ||
Object.defineProperty(exports, "bool2Asm", { enumerable: true, get: function () { return utils_1.bool2Asm; } }); | ||
Object.defineProperty(exports, "int2Asm", { enumerable: true, get: function () { return utils_1.int2Asm; } }); | ||
Object.defineProperty(exports, "literal2Asm", { enumerable: true, get: function () { return utils_1.literal2Asm; } }); | ||
Object.defineProperty(exports, "bytes2Literal", { enumerable: true, get: function () { return utils_1.bytes2Literal; } }); | ||
Object.defineProperty(exports, "bytesToHexString", { enumerable: true, get: function () { return utils_1.bytesToHexString; } }); | ||
Object.defineProperty(exports, "getValidatedHexString", { enumerable: true, get: function () { return utils_1.getValidatedHexString; } }); | ||
var serializer_1 = require("./serializer"); | ||
@@ -29,3 +23,2 @@ Object.defineProperty(exports, "serializeState", { enumerable: true, get: function () { return serializer_1.serializeState; } }); | ||
Object.defineProperty(exports, "Bool", { enumerable: true, get: function () { return scryptTypes_1.Bool; } }); | ||
Object.defineProperty(exports, "Byte", { enumerable: true, get: function () { return scryptTypes_1.Byte; } }); | ||
Object.defineProperty(exports, "Bytes", { enumerable: true, get: function () { return scryptTypes_1.Bytes; } }); | ||
@@ -32,0 +25,0 @@ Object.defineProperty(exports, "PrivKey", { enumerable: true, get: function () { return scryptTypes_1.PrivKey; } }); |
@@ -21,6 +21,2 @@ export declare abstract class ScryptType { | ||
} | ||
export declare class Byte extends ScryptType { | ||
constructor(bytesVal: string); | ||
toLiteral(): string; | ||
} | ||
export declare class Bytes extends ScryptType { | ||
@@ -27,0 +23,0 @@ constructor(bytesVal: string); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.OpCodeType = exports.SigHashPreimage = exports.SigHashType = exports.SigHash = exports.Sha256 = exports.Sha1 = exports.Ripemd160 = exports.Sig = exports.PubKey = exports.PrivKey = exports.Bytes = exports.Byte = exports.Bool = exports.Int = exports.ScryptType = void 0; | ||
exports.OpCodeType = exports.SigHashPreimage = exports.SigHashType = exports.SigHash = exports.Sha256 = exports.Sha1 = exports.Ripemd160 = exports.Sig = exports.PubKey = exports.PrivKey = exports.Bytes = exports.Bool = exports.Int = exports.ScryptType = void 0; | ||
const utils_1 = require("./utils"); | ||
@@ -50,11 +50,2 @@ class ScryptType { | ||
exports.Bool = Bool; | ||
class Byte extends ScryptType { | ||
constructor(bytesVal) { | ||
super(bytesVal); | ||
} | ||
toLiteral() { | ||
return `'${utils_1.getValidatedHexString(this._value.toString())}'`; | ||
} | ||
} | ||
exports.Byte = Byte; | ||
class Bytes extends ScryptType { | ||
@@ -61,0 +52,0 @@ constructor(bytesVal) { |
@@ -0,0 +0,0 @@ import { bsv } from './utils'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -5,3 +5,2 @@ /// <reference types="node" /> | ||
export { bsv }; | ||
export declare const IntElemLen = 4; | ||
export declare const DEFAULT_FLAGS: number; | ||
@@ -8,0 +7,0 @@ export declare const DEFAULT_SIGHASH_TYPE: number; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.uri2path = exports.path2uri = exports.bin2num = exports.num2bin = exports.getPreimage = exports.toHex = exports.signTx = exports.getValidatedHexString = exports.hexStringToBytes = exports.bytesToHexString = exports.bytes2Literal = exports.literal2Asm = exports.int2Asm = exports.bool2Asm = exports.DEFAULT_SIGHASH_TYPE = exports.DEFAULT_FLAGS = exports.IntElemLen = exports.bsv = void 0; | ||
exports.uri2path = exports.path2uri = exports.bin2num = exports.num2bin = exports.getPreimage = exports.toHex = exports.signTx = exports.getValidatedHexString = exports.hexStringToBytes = exports.bytesToHexString = exports.bytes2Literal = exports.literal2Asm = exports.int2Asm = exports.bool2Asm = exports.DEFAULT_SIGHASH_TYPE = exports.DEFAULT_FLAGS = exports.bsv = void 0; | ||
const url_1 = require("url"); | ||
@@ -8,4 +8,2 @@ const scryptTypes_1 = require("./scryptTypes"); | ||
exports.bsv = bsv; | ||
// each element's byte length in an int array | ||
exports.IntElemLen = 4; | ||
const BN = bsv.crypto.BN; | ||
@@ -74,3 +72,3 @@ const Interp = bsv.Script.Interpreter; | ||
if (m) { | ||
return [m[1].length > 0 ? getValidatedHexString(m[1]) : 'OP_0', 'byte[]']; | ||
return [m[1].length > 0 ? getValidatedHexString(m[1]) : 'OP_0', 'bytes']; | ||
} | ||
@@ -77,0 +75,0 @@ // byte |
{ | ||
"name": "scryptlib", | ||
"version": "0.2.18-1", | ||
"version": "0.2.18-beta", | ||
"description": "Javascript SDK for integration of Bitcoin SV Smart Contracts written in sCrypt language.", | ||
@@ -54,5 +54,5 @@ "engines": { | ||
"md5": "^2.2.1", | ||
"scryptc": "0.2.7", | ||
"scryptc": "^0.2.8", | ||
"ts-optchain": "^0.1.8" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
873189
73
2085
1
+ Addedscryptc@0.2.9(transitive)
- Removedscryptc@0.2.7(transitive)
Updatedscryptc@^0.2.8