scryptlib
Advanced tools
Comparing version 0.2.19 to 0.2.20-beta
import { AbstractContract, TxContext, VerifyResult, AsmVarValues } from './contract'; | ||
import { ScryptType } from './scryptTypes'; | ||
import { SingletonParamType, SupportedParamType } from './scryptTypes'; | ||
export declare enum ABIEntityType { | ||
@@ -20,4 +20,2 @@ FUNCTION = "function", | ||
} | ||
export declare type SingletonParamType = ScryptType | boolean | number | bigint; | ||
export declare type SupportedParamType = SingletonParamType | SingletonParamType[]; | ||
export declare class FunctionCall { | ||
@@ -24,0 +22,0 @@ methodName: string; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -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, bool2Asm, int2Asm, parseLiteral, bytes2Literal, bytesToHexString, getValidatedHexString, literal2ScryptType, VariableType } 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 } from './scryptTypes'; | ||
export { Int, Bool, Bytes, PrivKey, PubKey, Sig, Ripemd160, Sha1, Sha256, SigHashType, SigHashPreimage, OpCodeType, SingletonParamType, SupportedParamType, ScryptType, ValueType } from './scryptTypes'; |
@@ -16,6 +16,8 @@ "use strict"; | ||
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, "parseLiteral", { enumerable: true, get: function () { return utils_1.parseLiteral; } }); | ||
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; } }); | ||
Object.defineProperty(exports, "literal2ScryptType", { enumerable: true, get: function () { return utils_1.literal2ScryptType; } }); | ||
Object.defineProperty(exports, "VariableType", { enumerable: true, get: function () { return utils_1.VariableType; } }); | ||
var serializer_1 = require("./serializer"); | ||
@@ -39,2 +41,3 @@ Object.defineProperty(exports, "serializeState", { enumerable: true, get: function () { return serializer_1.serializeState; } }); | ||
Object.defineProperty(exports, "OpCodeType", { enumerable: true, get: function () { return scryptTypes_1.OpCodeType; } }); | ||
Object.defineProperty(exports, "ScryptType", { enumerable: true, get: function () { return scryptTypes_1.ScryptType; } }); | ||
//# sourceMappingURL=index.js.map |
@@ -0,8 +1,9 @@ | ||
export declare type ValueType = number | bigint | boolean | string; | ||
export declare abstract class ScryptType { | ||
protected _value: number | bigint | boolean | string; | ||
protected _value: ValueType; | ||
protected _literal: string; | ||
private _asm; | ||
private _type; | ||
constructor(value: number | bigint | boolean | string); | ||
get value(): number | bigint | boolean | string; | ||
constructor(value: ValueType); | ||
get value(): ValueType; | ||
get literal(): string; | ||
@@ -26,3 +27,3 @@ get type(): string; | ||
export declare class PrivKey extends ScryptType { | ||
constructor(intVal: number); | ||
constructor(intVal: bigint); | ||
toLiteral(): string; | ||
@@ -99,2 +100,4 @@ } | ||
} | ||
export declare type SingletonParamType = ScryptType | boolean | number | bigint; | ||
export declare type SupportedParamType = SingletonParamType | SingletonParamType[]; | ||
export {}; |
@@ -10,3 +10,3 @@ "use strict"; | ||
this._literal = this.toLiteral(); | ||
const [asm, scrType] = utils_1.literal2Asm(this._literal); | ||
const [asm, _, scrType] = utils_1.parseLiteral(this._literal); | ||
this._type = scrType; | ||
@@ -65,3 +65,4 @@ this._asm = asm; | ||
toLiteral() { | ||
return `PrivKey(${this._value})`; | ||
const v = this._value; | ||
return `PrivKey(0x${utils_1.intValue2hex(v)})`; | ||
} | ||
@@ -68,0 +69,0 @@ } |
@@ -0,0 +0,0 @@ import { bsv } from './utils'; |
@@ -0,0 +0,0 @@ "use strict"; |
/// <reference types="node" /> | ||
import { SigHashPreimage } from "./scryptTypes"; | ||
import { SigHashPreimage, ScryptType, ValueType } from "./scryptTypes"; | ||
import bsv = require('bsv'); | ||
@@ -9,9 +9,29 @@ export { bsv }; | ||
/** | ||
* decimal int to little-endian signed magnitude | ||
* decimal or hex int to little-endian signed magnitude | ||
*/ | ||
export declare function int2Asm(str: string): string; | ||
/** | ||
* convert literals to script ASM format | ||
* decimal int or hex str to number or bigint | ||
*/ | ||
export declare function literal2Asm(l: string): [string, string]; | ||
export declare function int2Value(str: string): number | bigint; | ||
export declare function intValue2hex(val: number | bigint): string; | ||
export declare enum VariableType { | ||
BOOL = "bool", | ||
INT = "int", | ||
BYTES = "bytes", | ||
PUBKEY = "PubKey", | ||
PRIVKEY = "PrivKey", | ||
SIG = "Sig", | ||
RIPEMD160 = "Ripemd160", | ||
SHA1 = "Sha1", | ||
SHA256 = "Sha256", | ||
SIGHASHTYPE = "SigHashType", | ||
SIGHASHPREIMAGE = "SigHashPreimage", | ||
OPCODETYPE = "OpCodeType" | ||
} | ||
export declare function parseLiteral(l: string): [string, ValueType, VariableType]; | ||
/** | ||
* convert literals to Scrypt Type | ||
*/ | ||
export declare function literal2ScryptType(l: string): ScryptType; | ||
export declare function bytes2Literal(bytearray: number[], type: string): string; | ||
@@ -18,0 +38,0 @@ export declare function bytesToHexString(bytearray: number[]): string; |
"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.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.literal2ScryptType = exports.parseLiteral = exports.VariableType = exports.intValue2hex = exports.int2Value = exports.int2Asm = exports.bool2Asm = exports.DEFAULT_SIGHASH_TYPE = exports.DEFAULT_FLAGS = exports.bsv = void 0; | ||
const url_1 = require("url"); | ||
@@ -31,49 +31,88 @@ const scryptTypes_1 = require("./scryptTypes"); | ||
/** | ||
* decimal int to little-endian signed magnitude | ||
* decimal or hex int to little-endian signed magnitude | ||
*/ | ||
function int2Asm(str) { | ||
if (!/^-?(0x)?\d+$/.test(str)) { | ||
if (/^(-?\d+)$/.test(str) || /^0x([0-9a-fA-F]+)$/.test(str)) { | ||
const number = str.startsWith('0x') ? new BN(str.substring(2), 16) : new BN(str, 10); | ||
if (number.eqn(-1)) { | ||
return 'OP_1NEGATE'; | ||
} | ||
if (number.gten(0) && number.lten(16)) { | ||
return 'OP_' + str; | ||
} | ||
const m = number.toSM({ endian: 'little' }); | ||
return m.toString('hex'); | ||
} | ||
else { | ||
throw new Error(`invalid str '${str}' to convert to int`); | ||
} | ||
const number = new BN(str, 10); | ||
if (number.eqn(-1)) { | ||
return 'OP_1NEGATE'; | ||
} | ||
if (number.gten(0) && number.lten(16)) { | ||
return 'OP_' + str; | ||
} | ||
const m = number.toSM({ endian: 'little' }); | ||
return m.toString('hex'); | ||
} | ||
exports.int2Asm = int2Asm; | ||
/** | ||
* convert literals to script ASM format | ||
* decimal int or hex str to number or bigint | ||
*/ | ||
function literal2Asm(l) { | ||
function int2Value(str) { | ||
if (/^(-?\d+)$/.test(str) || /^0x([0-9a-fA-F]+)$/.test(str)) { | ||
const number = str.startsWith('0x') ? new BN(str.substring(2), 16) : new BN(str, 10); | ||
if (number.toNumber() < Number.MAX_SAFE_INTEGER) { | ||
return number.toNumber(); | ||
} | ||
else { | ||
return BigInt(str); | ||
} | ||
} | ||
else { | ||
throw new Error(`invalid str '${str}' to convert to int`); | ||
} | ||
} | ||
exports.int2Value = int2Value; | ||
function intValue2hex(val) { | ||
let hex = val.toString(16); | ||
if (hex.length % 2 === 1) { | ||
hex = "0" + hex; | ||
} | ||
return hex; | ||
} | ||
exports.intValue2hex = intValue2hex; | ||
var VariableType; | ||
(function (VariableType) { | ||
VariableType["BOOL"] = "bool"; | ||
VariableType["INT"] = "int"; | ||
VariableType["BYTES"] = "bytes"; | ||
VariableType["PUBKEY"] = "PubKey"; | ||
VariableType["PRIVKEY"] = "PrivKey"; | ||
VariableType["SIG"] = "Sig"; | ||
VariableType["RIPEMD160"] = "Ripemd160"; | ||
VariableType["SHA1"] = "Sha1"; | ||
VariableType["SHA256"] = "Sha256"; | ||
VariableType["SIGHASHTYPE"] = "SigHashType"; | ||
VariableType["SIGHASHPREIMAGE"] = "SigHashPreimage"; | ||
VariableType["OPCODETYPE"] = "OpCodeType"; | ||
})(VariableType = exports.VariableType || (exports.VariableType = {})); | ||
function parseLiteral(l) { | ||
// bool | ||
if (l === 'false') { | ||
return ['OP_FALSE', 'bool']; | ||
return ["OP_FALSE", false, VariableType.BOOL]; | ||
} | ||
if (l === 'true') { | ||
return ['OP_TRUE', 'bool']; | ||
return ["OP_TRUE", true, VariableType.BOOL]; | ||
} | ||
// hex int | ||
if (/^0x[0-9a-fA-F]+$/.test(l)) { | ||
return [int2Asm(l), 'int']; | ||
let m = /^(0x[0-9a-fA-F]+)$/.exec(l); | ||
if (m) { | ||
return [int2Asm(m[1]), int2Value(m[1]), VariableType.INT]; | ||
} | ||
// decimal int | ||
if (/^-?\d+$/.test(l)) { | ||
return [int2Asm(l), 'int']; | ||
m = /^(-?\d+)$/.exec(l); | ||
if (m) { | ||
return [int2Asm(m[1]), int2Value(m[1]), VariableType.INT]; | ||
} | ||
// bytes | ||
// note: special handling of empty bytes b'' | ||
let m = /^b'([\da-fA-F]*)'$/.exec(l); | ||
m = /^b'([\da-fA-F]*)'$/.exec(l); | ||
if (m) { | ||
return [m[1].length > 0 ? getValidatedHexString(m[1]) : 'OP_0', 'bytes']; | ||
const value = getValidatedHexString(m[1]); | ||
const asm = value === '' ? 'OP_0' : value; | ||
return [asm, value, VariableType.BYTES]; | ||
} | ||
// byte | ||
m = /^'([\da-fA-F]*)'$/.exec(l); | ||
if (m) { | ||
return [m[1], 'byte']; | ||
} | ||
// PrivKey | ||
@@ -83,3 +122,3 @@ // 1) decimal int | ||
if (m) { | ||
return [m[1], 'PrivKey']; | ||
return [m[1], int2Value(m[1]), VariableType.PRIVKEY]; | ||
} | ||
@@ -89,3 +128,3 @@ // 2) hex int | ||
if (m) { | ||
return [m[1], 'PrivKey']; | ||
return [m[1], int2Value(m[1]), VariableType.PRIVKEY]; | ||
} | ||
@@ -95,3 +134,4 @@ // PubKey | ||
if (m) { | ||
return [getValidatedHexString(m[1]), 'PubKey']; | ||
const value = getValidatedHexString(m[1]); | ||
return [value, value, VariableType.PUBKEY]; | ||
} | ||
@@ -101,3 +141,4 @@ // Sig | ||
if (m) { | ||
return [getValidatedHexString(m[1]), 'Sig']; | ||
const value = getValidatedHexString(m[1]); | ||
return [value, value, VariableType.SIG]; | ||
} | ||
@@ -107,3 +148,4 @@ // Ripemd160 | ||
if (m) { | ||
return [getValidatedHexString(m[1]), 'Ripemd160']; | ||
const value = getValidatedHexString(m[1]); | ||
return [value, value, VariableType.RIPEMD160]; | ||
} | ||
@@ -113,3 +155,4 @@ // Sha1 | ||
if (m) { | ||
return [getValidatedHexString(m[1]), 'Sha1']; | ||
const value = getValidatedHexString(m[1]); | ||
return [value, value, VariableType.SHA1]; | ||
} | ||
@@ -119,3 +162,4 @@ // Sha256 | ||
if (m) { | ||
return [getValidatedHexString(m[1]), 'Sha256']; | ||
const value = getValidatedHexString(m[1]); | ||
return [value, value, VariableType.SHA256]; | ||
} | ||
@@ -125,3 +169,4 @@ // SigHashType | ||
if (m) { | ||
return [getValidatedHexString(m[1]), 'SigHashType']; | ||
const bn = new BN(getValidatedHexString(m[1]), 16); | ||
return [bn.toString("hex", 2), bn.toNumber(), VariableType.SIGHASHTYPE]; | ||
} | ||
@@ -131,3 +176,4 @@ // SigHashPreimage | ||
if (m) { | ||
return [getValidatedHexString(m[1]), 'SigHashPreimage']; | ||
const value = getValidatedHexString(m[1]); | ||
return [value, value, VariableType.SIGHASHPREIMAGE]; | ||
} | ||
@@ -137,7 +183,43 @@ // OpCodeType | ||
if (m) { | ||
return [getValidatedHexString(m[1]), 'OpCodeType']; | ||
const value = getValidatedHexString(m[1]); | ||
return [value, value, VariableType.OPCODETYPE]; | ||
} | ||
throw new Error(`<${l}> cannot be cast to ASM format, only sCrypt native types supported`); | ||
} | ||
exports.literal2Asm = literal2Asm; | ||
exports.parseLiteral = parseLiteral; | ||
/** | ||
* convert literals to Scrypt Type | ||
*/ | ||
function literal2ScryptType(l) { | ||
const [asm, value, type] = parseLiteral(l); | ||
switch (type) { | ||
case VariableType.BOOL: | ||
return new scryptTypes_1.Bool(value); | ||
case VariableType.INT: | ||
return new scryptTypes_1.Int(value); | ||
case VariableType.BYTES: | ||
return new scryptTypes_1.Bytes(value); | ||
case VariableType.PRIVKEY: | ||
return new scryptTypes_1.PrivKey(value); | ||
case VariableType.PUBKEY: | ||
return new scryptTypes_1.PubKey(value); | ||
case VariableType.SIG: | ||
return new scryptTypes_1.Sig(value); | ||
case VariableType.RIPEMD160: | ||
return new scryptTypes_1.Ripemd160(value); | ||
case VariableType.SHA1: | ||
return new scryptTypes_1.Sha1(value); | ||
case VariableType.SHA256: | ||
return new scryptTypes_1.Sha256(value); | ||
case VariableType.SIGHASHTYPE: | ||
return new scryptTypes_1.SigHashType(value); | ||
case VariableType.SIGHASHPREIMAGE: | ||
return new scryptTypes_1.SigHashPreimage(value); | ||
case VariableType.OPCODETYPE: | ||
return new scryptTypes_1.OpCodeType(value); | ||
default: | ||
throw new Error(`<${l}> cannot be cast to ScryptType, only sCrypt native types supported`); | ||
} | ||
} | ||
exports.literal2ScryptType = literal2ScryptType; | ||
function bytes2Literal(bytearray, type) { | ||
@@ -144,0 +226,0 @@ switch (type) { |
{ | ||
"name": "scryptlib", | ||
"version": "0.2.19", | ||
"version": "0.2.20-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.8", | ||
"scryptc": "^0.2.9", | ||
"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
77958
20
1690
Updatedscryptc@^0.2.9