scryptlib
Advanced tools
Comparing version 0.2.34 to 0.3.1-beta
# CHANGELOG | ||
## 0.3.1 | ||
* support mixed struct and array | ||
* CompileResult structs properties contains all dependencyAsts structs | ||
* Does not force an error when the DESC file version is too old | ||
## 0.2.34 | ||
@@ -4,0 +11,0 @@ |
@@ -116,14 +116,21 @@ "use strict"; | ||
const arg = args[index]; | ||
if (Array.isArray(arg)) { | ||
const [elemTypeName, arraySize] = utils_1.arrayTypeAndSize(param.finalType); | ||
if (arraySize !== arg.length) { | ||
throw new Error(`Array arguments wrong size for '${param.name}' in constructor, expected [${arraySize}] but got [${arg.length}]`); | ||
if (utils_1.isArrayType(param.finalType)) { | ||
const [elemTypeName, arraySizes] = utils_1.arrayTypeAndSize(param.finalType); | ||
if (Array.isArray(arg)) { | ||
if (utils_1.checkArray(arg, [elemTypeName, arraySizes])) { | ||
// flattern array | ||
utils_1.flatternArray(arg, param).forEach((e, idx) => { | ||
cParams_.push({ name: e.name, type: e.type, finalType: e.finalType }); | ||
args_.push(e.value); | ||
}); | ||
} | ||
else { | ||
throw new Error(`constructor ${index}-th parameter should be ${param.finalType}`); | ||
} | ||
} | ||
// flattern array | ||
arg.forEach((e, idx) => { | ||
cParams_.push({ name: `${param.name}[${idx}]`, type: elemTypeName, finalType: elemTypeName }); | ||
args_.push(e); | ||
}); | ||
else { | ||
throw new Error(`constructor ${index}-th parameter should be ${param.finalType}`); | ||
} | ||
} | ||
else if (scryptTypes_1.Struct.isStruct(arg)) { | ||
else if (utils_1.isStructType(param.finalType)) { | ||
const argS = arg; | ||
@@ -133,12 +140,6 @@ if (param.finalType != argS.finalType) { | ||
} | ||
const s = utils_1.findStructByType(param.finalType, this.structs); | ||
if (s) { | ||
s.params.forEach(e => { | ||
cParams_.push({ name: `${param.name}.${e.name}`, type: e.type, finalType: e.finalType }); | ||
args_.push(arg.value[e.name]); | ||
}); | ||
} | ||
else { | ||
throw new Error(`constructor does not accept struct ${argS.type} at ${index}-th parameter`); | ||
} | ||
utils_1.flatternStruct(argS, param.name).forEach(v => { | ||
cParams_.push({ name: `${v.name}`, type: v.type, finalType: v.finalType }); | ||
args_.push(v.value); | ||
}); | ||
} | ||
@@ -191,7 +192,9 @@ else { | ||
} | ||
const [elemTypeName, arraySize] = utils_1.arrayTypeAndSize(arrayParm.finalType); | ||
if (arraySize !== args.length) { | ||
throw new Error(`Array arguments wrong size, expected [${arraySize}] but got [${args.length}]`); | ||
const [elemTypeName, arraySizes] = utils_1.arrayTypeAndSize(arrayParm.finalType); | ||
if (utils_1.checkArray(args, [elemTypeName, arraySizes])) { | ||
return utils_1.flatternArray(args, arrayParm).map(arg => this.encodeParam(arg.value, { name: arg.name, type: arg.finalType, finalType: arg.finalType })).join(' '); | ||
} | ||
return args.map(arg => this.encodeParam(arg, { name: arrayParm.name, type: elemTypeName, finalType: elemTypeName })).join(' '); | ||
else { | ||
throw new Error(`checkArray ${arrayParm.type} fail`); | ||
} | ||
} | ||
@@ -205,4 +208,4 @@ encodeParam(arg, paramEntity) { | ||
else { | ||
const scryptType = arg.type; | ||
throw new Error(`expect param ${paramEntity.name} as Array but got ${scryptType}`); | ||
const scryptType = utils_1.typeOfArg(arg); | ||
throw new Error(`expect param ${paramEntity.name} as ${paramEntity.finalType} but got ${scryptType}`); | ||
} | ||
@@ -222,2 +225,6 @@ } | ||
} | ||
const scryptType = utils_1.typeOfArg(arg); | ||
if (scryptType != finalType) { | ||
throw new Error(`wrong argument type, expected ${paramEntity.finalType} or ${paramEntity.type} but got ${scryptType}`); | ||
} | ||
const typeofArg = typeof arg; | ||
@@ -233,6 +240,2 @@ if (typeofArg === 'boolean') { | ||
} | ||
const scryptType = arg.finalType; | ||
if (scryptType != finalType) { | ||
throw new Error(`wrong argument type, expected ${paramEntity.finalType} or ${paramEntity.type} but got ${scryptType}`); | ||
} | ||
return arg.toASM(); | ||
@@ -239,0 +242,0 @@ } |
@@ -93,2 +93,3 @@ import { ContractDescription } from './contract'; | ||
type: string; | ||
finalType: string; | ||
} | ||
@@ -115,6 +116,6 @@ export declare function compile(source: { | ||
export declare function getABIDeclaration(astRoot: any): ABI; | ||
export declare function getStructDeclaration(astRoot: any): Array<StructEntity>; | ||
export declare function getAliasDeclaration(astRoot: any): Array<AliasEntity>; | ||
export declare function getStructDeclaration(astRoot: any, dependencyAsts: any): Array<StructEntity>; | ||
export declare function getAliasDeclaration(astRoot: any, dependencyAsts: any): Array<AliasEntity>; | ||
export declare function getPlatformScryptc(): string; | ||
export declare function getDefaultScryptc(): string; | ||
export declare function desc2CompileResult(description: ContractDescription): CompileResult; |
@@ -152,4 +152,4 @@ "use strict"; | ||
result.contract = name; | ||
result.structs = getStructDeclaration(result.ast); | ||
result.alias = getAliasDeclaration(result.ast); | ||
result.structs = getStructDeclaration(result.ast, allAst); | ||
result.alias = getAliasDeclaration(result.ast, allAst); | ||
} | ||
@@ -357,14 +357,26 @@ let asmObj = null; | ||
exports.getABIDeclaration = getABIDeclaration; | ||
function getStructDeclaration(astRoot) { | ||
return ts_optchain_1.oc(astRoot).structs([]).map(s => ({ | ||
name: s['name'], | ||
params: s['fields'].map(p => { return { name: p['name'], type: p['type'], finalType: p['finalType'] }; }), | ||
})); | ||
function getStructDeclaration(astRoot, dependencyAsts) { | ||
const allAst = [astRoot]; | ||
Object.keys(dependencyAsts).forEach(key => { | ||
allAst.push(dependencyAsts[key]); | ||
}); | ||
return allAst.map(ast => { | ||
return ts_optchain_1.oc(ast).structs([]).map(s => ({ | ||
name: s['name'], | ||
params: s['fields'].map(p => { return { name: p['name'], type: p['type'], finalType: p['finalType'] }; }), | ||
})); | ||
}).flat(1); | ||
} | ||
exports.getStructDeclaration = getStructDeclaration; | ||
function getAliasDeclaration(astRoot) { | ||
return ts_optchain_1.oc(astRoot).alias([]).map(s => ({ | ||
name: s['alias'], | ||
type: s['type'], | ||
})); | ||
function getAliasDeclaration(astRoot, dependencyAsts) { | ||
const allAst = [astRoot]; | ||
Object.keys(dependencyAsts).forEach(key => { | ||
allAst.push(dependencyAsts[key]); | ||
}); | ||
return allAst.map(ast => { | ||
return ts_optchain_1.oc(ast).alias([]).map(s => ({ | ||
name: s['alias'], | ||
type: s['type'], | ||
})); | ||
}).flat(1); | ||
} | ||
@@ -425,5 +437,9 @@ exports.getAliasDeclaration = getAliasDeclaration; | ||
const asm = description.asm.split(' '); | ||
if (description.version === undefined || description.version < CURRENT_CONTRACT_DESCRIPTION_VERSION) { | ||
throw new Error(`Contract description version deprecated, Please update your sCrypt extension to the latest version and recompile`); | ||
const errorMessage = `Contract description version deprecated, Please update your sCrypt extension to the latest version and recompile`; | ||
if (description.version === undefined) { | ||
throw new Error(errorMessage); | ||
} | ||
if (description.version < CURRENT_CONTRACT_DESCRIPTION_VERSION) { | ||
console.warn(errorMessage); | ||
} | ||
const result = { | ||
@@ -430,0 +446,0 @@ compilerVersion: description.compilerVersion, |
import { ABICoder, Arguments, FunctionCall, Script } from "./abi"; | ||
import { State } from "./serializer"; | ||
import { ScryptType } from './scryptTypes'; | ||
import { Struct, ScryptType } from './scryptTypes'; | ||
import { StructEntity, ABIEntity, OpCode, CompileResult, AliasEntity } from "./compilerWrapper"; | ||
@@ -61,2 +61,7 @@ export interface TxContext { | ||
export declare function buildContractClass(desc: CompileResult | ContractDescription): any; | ||
/** | ||
* @deprecated use buildTypeClasses | ||
* @param desc CompileResult or ContractDescription | ||
*/ | ||
export declare function buildStructsClass(desc: CompileResult | ContractDescription): Record<string, typeof Struct>; | ||
export declare function buildTypeClasses(desc: CompileResult | ContractDescription): Record<string, typeof ScryptType>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.buildTypeClasses = exports.buildContractClass = exports.AbstractContract = void 0; | ||
exports.buildTypeClasses = exports.buildStructsClass = exports.buildContractClass = exports.AbstractContract = void 0; | ||
const abi_1 = require("./abi"); | ||
@@ -68,3 +68,3 @@ const serializer_1 = require("./serializer"); | ||
const result = bsi.verify(us, ls, tx, inputIndex, utils_1.DEFAULT_FLAGS, new utils_1.bsv.crypto.BN(inputSatoshis)); | ||
let error = `VerifyError: ${bsi.errstr}`; | ||
let error = result ? '' : `VerifyError: ${bsi.errstr}`; | ||
// some time there is no opcodes, such as when sourcemap flag is closeed. | ||
@@ -231,2 +231,6 @@ if (opcodes) { | ||
exports.buildContractClass = buildContractClass; | ||
/** | ||
* @deprecated use buildTypeClasses | ||
* @param desc CompileResult or ContractDescription | ||
*/ | ||
function buildStructsClass(desc) { | ||
@@ -241,9 +245,10 @@ const structTypes = {}; | ||
super(o); | ||
this.bind(element); | ||
} | ||
} | ||
}); | ||
structTypes[name].structAst = element; | ||
}); | ||
return structTypes; | ||
} | ||
exports.buildStructsClass = buildStructsClass; | ||
function buildTypeClasses(desc) { | ||
@@ -256,2 +261,3 @@ const structClasses = buildStructsClass(desc); | ||
const finalType = utils_1.resolveType(alias, structs, element.name); | ||
element.finalType = finalType; | ||
const C = scryptTypes_1.BasicScryptType[finalType]; | ||
@@ -258,0 +264,0 @@ if (C) { |
@@ -1,6 +0,6 @@ | ||
export { buildContractClass, VerifyResult, buildTypeClasses } from './contract'; | ||
export { buildContractClass, VerifyResult, buildTypeClasses, buildStructsClass } from './contract'; | ||
export { compile, StructEntity, getStructDeclaration, getABIDeclaration, ABIEntity, ABIEntityType, ABI } from './compilerWrapper'; | ||
export { Arguments, Argument } from './abi'; | ||
export { bsv, signTx, toHex, getPreimage, num2bin, bin2num, bool2Asm, int2Asm, parseLiteral, bytes2Literal, bytesToHexString, getValidatedHexString, literal2ScryptType, literal2Asm, findStructByType, findStructByName, checkStruct, isStructType, isArrayType, compileContract, arrayTypeAndSize, newCall, getStructNameByType, genLaunchConfigFile } from './utils'; | ||
export { bsv, signTx, toHex, getPreimage, num2bin, bin2num, bool2Asm, int2Asm, parseLiteral, bytes2Literal, bytesToHexString, getValidatedHexString, literal2ScryptType, literal2Asm, findStructByType, findStructByName, checkStruct, isStructType, isArrayType, compileContract, arrayTypeAndSize, newCall, getStructNameByType, genLaunchConfigFile, subArrayType } from './utils'; | ||
export { serializeState, deserializeState, State, STATE_LEN_2BYTES, STATE_LEN_4BYTES } from './serializer'; | ||
export { Int, Bool, Bytes, PrivKey, PubKey, Sig, Ripemd160, Sha1, Sha256, SigHashType, SigHashPreimage, OpCodeType, SingletonParamType, SupportedParamType, ScryptType, ValueType, Struct, StructObject, VariableType } from './scryptTypes'; |
@@ -6,2 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "buildTypeClasses", { enumerable: true, get: function () { return contract_1.buildTypeClasses; } }); | ||
Object.defineProperty(exports, "buildStructsClass", { enumerable: true, get: function () { return contract_1.buildStructsClass; } }); | ||
var compilerWrapper_1 = require("./compilerWrapper"); | ||
@@ -37,2 +38,3 @@ Object.defineProperty(exports, "compile", { enumerable: true, get: function () { return compilerWrapper_1.compile; } }); | ||
Object.defineProperty(exports, "genLaunchConfigFile", { enumerable: true, get: function () { return utils_1.genLaunchConfigFile; } }); | ||
Object.defineProperty(exports, "subArrayType", { enumerable: true, get: function () { return utils_1.subArrayType; } }); | ||
var serializer_1 = require("./serializer"); | ||
@@ -39,0 +41,0 @@ Object.defineProperty(exports, "serializeState", { enumerable: true, get: function () { return serializer_1.serializeState; } }); |
@@ -104,14 +104,30 @@ import { StructEntity } from "./compilerWrapper"; | ||
} | ||
export declare type SingletonParamType = ScryptType | boolean | number | bigint; | ||
export declare type BasicType = ScryptType | boolean | number | bigint; | ||
export declare type SingletonParamType = BasicType | BasicType[]; | ||
export declare type StructObject = Record<string, SingletonParamType>; | ||
export declare class Struct extends ScryptType { | ||
sorted: boolean; | ||
static structAst: StructEntity; | ||
constructor(o: StructObject); | ||
bind(structAst: StructEntity): void; | ||
private bind; | ||
toASM(): string; | ||
/** | ||
* @deprecated use flatternStruct, see toASM | ||
*/ | ||
toArray(): ScryptType[]; | ||
memberByIndex(index: number): string; | ||
/** | ||
* @deprecated use getMemberFinalType | ||
*/ | ||
getMemberType(key: string): string; | ||
/** | ||
* Get the real member type of the structure | ||
*/ | ||
getMemberFinalType(key: string): string; | ||
/** | ||
* Get the member type declared by the structure by structAst | ||
*/ | ||
getMemberAstFinalType(key: string): string; | ||
getMembers(): string[]; | ||
memberByKey(key: string): SingletonParamType | undefined; | ||
toLiteral(): string; | ||
@@ -118,0 +134,0 @@ toJSON(): {}; |
@@ -264,4 +264,6 @@ "use strict"; | ||
this.sorted = false; | ||
this.bind(); | ||
} | ||
bind(structAst) { | ||
bind() { | ||
const structAst = Object.getPrototypeOf(this).constructor.structAst; | ||
utils_1.checkStruct(structAst, this); | ||
@@ -288,5 +290,10 @@ const ordered = {}; | ||
} | ||
this._asm = this.toArray().map(v => v.toASM()).join(' '); | ||
this._asm = utils_1.flatternStruct(this, '').map(v => { | ||
return v.value.toASM(); | ||
}).join(' '); | ||
return this._asm; | ||
} | ||
/** | ||
* @deprecated use flatternStruct, see toASM | ||
*/ | ||
toArray() { | ||
@@ -319,2 +326,5 @@ if (!this.sorted) { | ||
} | ||
/** | ||
* @deprecated use getMemberFinalType | ||
*/ | ||
getMemberType(key) { | ||
@@ -338,24 +348,39 @@ const v = this.value; | ||
} | ||
/** | ||
* Get the real member type of the structure | ||
*/ | ||
getMemberFinalType(key) { | ||
const member = this.memberByKey(key); | ||
return utils_1.typeOfArg(member); | ||
} | ||
/** | ||
* Get the member type declared by the structure by structAst | ||
*/ | ||
getMemberAstFinalType(key) { | ||
const structAst = Object.getPrototypeOf(this).constructor.structAst; | ||
const paramEntity = structAst.params.find(p => { | ||
return p.name === key; | ||
}); | ||
return paramEntity.finalType; | ||
} | ||
getMembers() { | ||
const v = this.value; | ||
return Object.keys(v); | ||
} | ||
memberByKey(key) { | ||
const v = this.value; | ||
if (v[key] instanceof ScryptType) { | ||
return v[key].finalType; | ||
return v[key]; | ||
} | ||
else if (typeof v[key] === "boolean") { | ||
return new Bool(v[key]).finalType; | ||
return new Bool(v[key]); | ||
} | ||
else if (typeof v[key] === "number") { | ||
return new Int(v[key]).finalType; | ||
return new Int(v[key]); | ||
} | ||
else if (typeof v[key] === "bigint") { | ||
return new Int(v[key]).finalType; | ||
return new Int(v[key]); | ||
} | ||
else { | ||
return typeof v[key]; | ||
} | ||
return v[key]; | ||
} | ||
getMembers() { | ||
const v = this.value; | ||
return Object.keys(v); | ||
} | ||
toLiteral() { | ||
@@ -383,3 +408,11 @@ const v = this.value; | ||
if (v[key] instanceof ScryptType) { | ||
return Object.assign(obj, { [key]: v[key].toLiteral() }); | ||
if (Struct.isStruct(v[key])) { | ||
return Object.assign(obj, { [key]: v[key].toJSON() }); | ||
} | ||
else if (Array.isArray(v[key])) { | ||
return Object.assign(obj, { [key]: JSON.stringify(v[key]) }); | ||
} | ||
else { | ||
return Object.assign(obj, { [key]: v[key].toLiteral() }); | ||
} | ||
} | ||
@@ -386,0 +419,0 @@ else { |
/// <reference types="node" /> | ||
import { SigHashPreimage, ScryptType, ValueType, Struct, SupportedParamType, VariableType } from "./scryptTypes"; | ||
import { StructEntity, CompileResult, AliasEntity } from './compilerWrapper'; | ||
import { StructEntity, CompileResult, AliasEntity, ParamEntity } from './compilerWrapper'; | ||
import bsv = require('bsv'); | ||
@@ -49,3 +49,27 @@ import { AsmVarValues, TxContext } from './contract'; | ||
export declare function checkStruct(s: StructEntity, arg: Struct): void; | ||
export declare function arrayTypeAndSize(arrayTypeName: string): [string, number]; | ||
/** | ||
* return eg. int[2][3][4] => ['int', [2,3,4]] | ||
* @param arrayTypeName eg. int[2][3][4] | ||
*/ | ||
export declare function arrayTypeAndSize(arrayTypeName: string): [string, Array<number>]; | ||
/** | ||
* return eg. int[2][3][4] => int[3][4] | ||
* @param arrayTypeName eg. int[2][3][4] | ||
*/ | ||
export declare function subArrayType(arrayTypeName: string): string; | ||
export declare function checkArray(args: SupportedParamType[], arrayInfo: [string, Array<number>]): boolean; | ||
export declare function subscript(index: number, arraySizes: Array<number>): string; | ||
export declare function flatternArray(arg: any, param: ParamEntity): Array<{ | ||
value: ScryptType; | ||
name: string; | ||
type: string; | ||
finalType: string; | ||
}>; | ||
export declare function flatternStruct(arg: SupportedParamType, name: string): Array<{ | ||
value: ScryptType; | ||
name: string; | ||
type: string; | ||
finalType: string; | ||
}>; | ||
export declare function typeOfArg(arg: SupportedParamType): string; | ||
export declare function readFileByLine(path: string, index: number): string; | ||
@@ -52,0 +76,0 @@ export declare function isEmpty(obj: unknown): boolean; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.resolveType = exports.genLaunchConfigFile = exports.newCall = exports.compileContract = exports.isEmpty = exports.readFileByLine = exports.arrayTypeAndSize = exports.checkStruct = exports.findStructByType = exports.getStructNameByType = exports.isArrayType = exports.isStructType = exports.findStructByName = exports.literal2Asm = exports.uri2path = exports.path2uri = exports.bin2num = exports.num2bin = exports.getPreimage = exports.toHex = exports.signTx = exports.getValidatedHexString = exports.hexStringToBytes = exports.bytesToHexString = exports.bytes2Literal = exports.literal2ScryptType = exports.parseLiteral = exports.intValue2hex = exports.int2Value = exports.int2Asm = exports.bool2Asm = exports.DEFAULT_SIGHASH_TYPE = exports.DEFAULT_FLAGS = exports.bsv = void 0; | ||
exports.resolveType = exports.genLaunchConfigFile = exports.newCall = exports.compileContract = exports.isEmpty = exports.readFileByLine = exports.typeOfArg = exports.flatternStruct = exports.flatternArray = exports.subscript = exports.checkArray = exports.subArrayType = exports.arrayTypeAndSize = exports.checkStruct = exports.findStructByType = exports.getStructNameByType = exports.isArrayType = exports.isStructType = exports.findStructByName = exports.literal2Asm = exports.uri2path = exports.path2uri = exports.bin2num = exports.num2bin = exports.getPreimage = exports.toHex = exports.signTx = exports.getValidatedHexString = exports.hexStringToBytes = exports.bytesToHexString = exports.bytes2Literal = exports.literal2ScryptType = exports.parseLiteral = exports.intValue2hex = exports.int2Value = exports.int2Asm = exports.bool2Asm = exports.DEFAULT_SIGHASH_TYPE = exports.DEFAULT_FLAGS = exports.bsv = void 0; | ||
const url_1 = require("url"); | ||
@@ -357,11 +357,11 @@ const scryptTypes_1 = require("./scryptTypes"); | ||
function isStructType(type) { | ||
return /struct\s(\w+)\s\{\}/.test(type); | ||
return /^struct\s(\w+)\s\{\}$/.test(type); | ||
} | ||
exports.isStructType = isStructType; | ||
function isArrayType(type) { | ||
return /\w\[\d+\]/.test(type); | ||
return /[^[\]]+\[\d+\]/.test(type); | ||
} | ||
exports.isArrayType = isArrayType; | ||
function getStructNameByType(type) { | ||
const m = /struct\s(\w+)\s\{\}/.exec(type.trim()); | ||
const m = /^struct\s(\w+)\s\{\}$/.exec(type.trim()); | ||
if (m) { | ||
@@ -383,4 +383,4 @@ return m[1]; | ||
s.params.forEach(p => { | ||
const finalType = arg.getMemberFinalType(p.name); | ||
const type = arg.getMemberType(p.name); | ||
const member = arg.memberByKey(p.name); | ||
const finalType = typeOfArg(member); | ||
if (finalType === 'undefined') { | ||
@@ -390,3 +390,16 @@ throw new Error(`argument of type struct ${s.name} missing member ${p.name}`); | ||
else if (finalType != p.finalType) { | ||
throw new Error(`wrong argument type, expected ${p.type} but got ${type}`); | ||
if (isArrayType(p.finalType)) { | ||
const [elemTypeName, arraySize] = arrayTypeAndSize(p.finalType); | ||
if (Array.isArray(arg.value[p.name])) { | ||
if (!checkArray(arg.value[p.name], [elemTypeName, arraySize])) { | ||
throw new Error(`checkArray fail, struct ${s.name} property ${p.name} should be ${p.finalType}`); | ||
} | ||
} | ||
else { | ||
throw new Error(`struct ${s.name} property ${p.name} should be ${p.finalType}`); | ||
} | ||
} | ||
else { | ||
throw new Error(`wrong argument type, expected ${p.finalType} but got ${finalType}`); | ||
} | ||
} | ||
@@ -402,9 +415,150 @@ }); | ||
exports.checkStruct = checkStruct; | ||
/** | ||
* return eg. int[2][3][4] => ['int', [2,3,4]] | ||
* @param arrayTypeName eg. int[2][3][4] | ||
*/ | ||
function arrayTypeAndSize(arrayTypeName) { | ||
const arraySizes = []; | ||
[...arrayTypeName.matchAll(/\[([\d])+\]+/g)].map(match => { | ||
arraySizes.push(parseInt(match[1])); | ||
}); | ||
const group = arrayTypeName.split('['); | ||
const elemTypeName = group[0]; | ||
const arraySize = parseInt(group[1].slice(0, -1)); | ||
return [elemTypeName, arraySize]; | ||
return [elemTypeName, arraySizes]; | ||
} | ||
exports.arrayTypeAndSize = arrayTypeAndSize; | ||
/** | ||
* return eg. int[2][3][4] => int[3][4] | ||
* @param arrayTypeName eg. int[2][3][4] | ||
*/ | ||
function subArrayType(arrayTypeName) { | ||
const [elemTypeName, sizes] = arrayTypeAndSize(arrayTypeName); | ||
return [elemTypeName, sizes.slice(1).map(size => `[${size}]`).join('')].join(''); | ||
} | ||
exports.subArrayType = subArrayType; | ||
function checkArray(args, arrayInfo) { | ||
const [elemTypeName, arraySizes] = arrayInfo; | ||
if (!Array.isArray(args)) { | ||
return false; | ||
} | ||
const len = arraySizes[0]; | ||
if (!len) { | ||
return false; | ||
} | ||
if (args.length !== len) { | ||
return false; | ||
} | ||
if (!args.every(arg => { | ||
if (Array.isArray(arg)) { | ||
return checkArray(arg, [elemTypeName, arraySizes.slice(1)]); | ||
} | ||
else { | ||
const scryptType = typeOfArg(arg); | ||
return scryptType === elemTypeName && arraySizes.length == 1; | ||
} | ||
})) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
exports.checkArray = checkArray; | ||
function subscript(index, arraySizes) { | ||
if (arraySizes.length == 1) { | ||
return `[${index}]`; | ||
} | ||
else if (arraySizes.length > 1) { | ||
const subArraySizes = arraySizes.slice(1); | ||
const offset = subArraySizes.reduce(function (acc, val) { return acc * val; }, 1); | ||
return `[${Math.floor(index / offset)}]${subscript(index % offset, subArraySizes)}`; | ||
} | ||
} | ||
exports.subscript = subscript; | ||
function flatternArray(arg, param) { | ||
if (!Array.isArray(arg)) { | ||
throw new Error(`flatternArray only work on array`); | ||
} | ||
const [elemTypeName, arraySizes] = arrayTypeAndSize(param.finalType); | ||
return arg.map((item, index) => { | ||
if (typeof item === "boolean") { | ||
item = new scryptTypes_1.Bool(item); | ||
} | ||
else if (typeof item === "number") { | ||
item = new scryptTypes_1.Int(item); | ||
} | ||
else if (typeof item === "bigint") { | ||
item = new scryptTypes_1.Int(item); | ||
} | ||
else if (Array.isArray(item)) { | ||
return flatternArray(item, { | ||
name: `${param.name}[${index}]`, | ||
type: subArrayType(param.type), | ||
finalType: subArrayType(param.finalType), | ||
}); | ||
} | ||
else if (scryptTypes_1.Struct.isStruct(item)) { | ||
return flatternStruct(item, `${param.name}[${index}]`); | ||
} | ||
else { | ||
item = item; | ||
} | ||
return { | ||
value: item, | ||
name: `${param.name}${subscript(index, arraySizes)}`, | ||
type: elemTypeName, | ||
finalType: elemTypeName | ||
}; | ||
}).flat(Infinity); | ||
} | ||
exports.flatternArray = flatternArray; | ||
function flatternStruct(arg, name) { | ||
if (scryptTypes_1.Struct.isStruct(arg)) { | ||
const argS = arg; | ||
const keys = argS.getMembers(); | ||
return keys.map(key => { | ||
let member = argS.memberByKey(key); | ||
if (scryptTypes_1.Struct.isStruct(member)) { | ||
return flatternStruct(member, `${name}.${key}`); | ||
} | ||
else if (Array.isArray(member)) { | ||
const finalType = argS.getMemberAstFinalType(key); | ||
return flatternArray(member, { | ||
name: `${name}.${key}`, | ||
type: finalType, | ||
finalType: finalType | ||
}); | ||
} | ||
else { | ||
member = member; | ||
return { | ||
value: member, | ||
name: `${name}.${key}`, | ||
type: member.type, | ||
finalType: member.finalType | ||
}; | ||
} | ||
}).flat(Infinity); | ||
} | ||
else { | ||
throw new Error(`${arg} should be struct`); | ||
} | ||
} | ||
exports.flatternStruct = flatternStruct; | ||
function typeOfArg(arg) { | ||
if (arg instanceof scryptTypes_1.ScryptType) { | ||
const scryptType = arg.finalType; | ||
return scryptType; | ||
} | ||
const typeofArg = typeof arg; | ||
if (typeofArg === 'boolean') { | ||
return 'bool'; | ||
} | ||
if (typeofArg === 'number') { | ||
return 'int'; | ||
} | ||
if (typeofArg === 'bigint') { | ||
return 'int'; | ||
} | ||
return typeof arg; | ||
} | ||
exports.typeOfArg = typeOfArg; | ||
function readFileByLine(path, index) { | ||
@@ -486,3 +640,5 @@ let result = ""; | ||
const inputSatoshis = txContext.inputSatoshis || 0; | ||
Object.assign(debugTxContext, { hex: tx.toString(), inputIndex, inputSatoshis }); | ||
if (tx) { | ||
Object.assign(debugTxContext, { hex: tx.toString(), inputIndex, inputSatoshis }); | ||
} | ||
if (txContext.opReturn) { | ||
@@ -489,0 +645,0 @@ Object.assign(debugTxContext, { opReturn: txContext.opReturn }); |
{ | ||
"name": "scryptlib", | ||
"version": "0.2.34", | ||
"version": "0.3.1-beta", | ||
"description": "Javascript SDK for integration of Bitcoin SV Smart Contracts written in sCrypt language.", | ||
@@ -5,0 +5,0 @@ "engines": { |
Sorry, the diff of this file is not supported yet
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
141814
2920
23