Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrypt-ts: remove scryptType class, use type #211

Merged
merged 67 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
4f6227c
add error log
Nov 17, 2022
be8178d
don't clean if output sourceMap
Nov 17, 2022
458214d
add bsv type
Nov 17, 2022
266f872
fix compileAsync
Nov 17, 2022
cadd53a
rename to isGenesis
Nov 18, 2022
720f0dc
pump version to 1.1.0-beta2
Nov 18, 2022
aa68904
add dependencies
Nov 23, 2022
e55a716
remove types/bsv
Dec 3, 2022
dc72584
update setInputScript
Dec 3, 2022
45ccc74
remove log
Dec 3, 2022
50bfd74
1.1.0-beta.4
Dec 3, 2022
21e6b96
update bsv
Dec 3, 2022
ae732c9
1.1.0-beta.5
Dec 3, 2022
3c05705
update bsv.d.ts
Dec 3, 2022
c42455b
1.1.0-beta.6
Dec 3, 2022
d635412
update bsv
Dec 4, 2022
3624991
update package-lock.json
Dec 4, 2022
40bd338
1.1.0-beta.7
Dec 4, 2022
d5c9f08
optimize cli
Dec 5, 2022
89af021
update bsv
Dec 5, 2022
10adf97
1.1.0-beta.8
Dec 5, 2022
ffc81e6
delete no use file
Dec 5, 2022
67663e7
don't exit when no compiler foud
Dec 5, 2022
42458be
1.1.0-beta.9
Dec 5, 2022
5e2b25e
Fix findcompile
Dec 5, 2022
6fab7d5
1.1.0-beta.10
Dec 5, 2022
f8f4244
bind scrypt compile version
Dec 5, 2022
fc3fab7
1.1.0-beta.11
Dec 5, 2022
9bac5f6
update bsv
Dec 5, 2022
dbceec1
rename
Dec 5, 2022
ac1696b
1.1.0-beta.12
Dec 5, 2022
95cb3cc
compile before publish
Dec 5, 2022
9817423
Fix buildtype error
Dec 6, 2022
84a9f3e
update bsv
Dec 6, 2022
502b439
add test
Dec 6, 2022
16b222d
1.1.0-beta.13
Dec 6, 2022
06e23b9
update bsv
Dec 7, 2022
a0c1437
remove FORKID from sighash flag
Dec 7, 2022
a3e87c0
add md5 function
Dec 8, 2022
3a3ad85
1.1.0-beta.14
Dec 8, 2022
60553e5
Fix ci
Dec 8, 2022
f297345
1.1.0-beta.14
Dec 8, 2022
4e6062a
Fix ci
Dec 8, 2022
00c64db
remove ScryptType class, use type
Dec 18, 2022
abbd3e9
2.0.0-beta.1
Dec 18, 2022
2fb2154
exclude findCompiler
Dec 18, 2022
bd89240
don't run boilerplate now
Dec 18, 2022
090de0d
refactor: add deserializer
Dec 19, 2022
16d6723
Fix export
Dec 19, 2022
5ee3cb7
2.0.0-beta.2
Dec 19, 2022
9878177
use param type, not argument type
Dec 20, 2022
58ac86b
2.0.0-beta.3
Dec 20, 2022
ee5d056
desc files are only contractname.json now
msinkec Dec 29, 2022
6d271a4
Renamed all occurences of desc to artifacts.
msinkec Dec 30, 2022
b6da9f8
update findKeyIndex
Jan 4, 2023
e35091a
2.0.0-beta.4
Jan 4, 2023
1be3cc1
Organized imports and some other fixes to make linting pass.
msinkec Jan 5, 2023
636c32b
Merge pull request #215 from msinkec/scrypt-ts
zhfnjust Jan 5, 2023
bb1c9fe
2.0.0-beta.5
Jan 6, 2023
736c9db
auto calc keyIndex
Jan 7, 2023
0a168e5
2.0.0-beta.6
Jan 7, 2023
b7e1d56
rename to getSortedItem
Jan 7, 2023
b64e19c
Fix genLaunchConfigFile
Jan 7, 2023
f2bcaa0
mirror fix
Jan 7, 2023
f83ad19
run bp test
Jan 8, 2023
5e87c0d
update readme
Jan 8, 2023
820ae9d
update change log
Jan 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
auto calc keyIndex
  • Loading branch information
hh committed Jan 7, 2023
commit 736c9dba4a16825a50d680f982fbafcfd5f10c2d
12 changes: 6 additions & 6 deletions src/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ export class ABICoder {
const constructorABI = this.abi.filter(entity => entity.type === ABIEntityType.CONSTRUCTOR)[0];
const cParams = constructorABI?.params || [];

contract.checkArgs('constructor', cParams, ...args);
const args_ = contract.checkArgs('constructor', cParams, ...args);

// handle array type
const flatteredArgs = cParams.flatMap((p, index) => {
const a = Object.assign({ ...p }, {
value: args[index]
value: args_[index]
}) as Argument;

return flatternArg(a, this.resolver, { state: false, ignoreValue: false });
Expand All @@ -190,7 +190,7 @@ export class ABICoder {
args: cParams.map((param, index) => ({
name: param.name,
type: param.type,
value: args[index]
value: args_[index]
}))
});

Expand Down Expand Up @@ -320,11 +320,11 @@ export class ABICoder {
encodePubFunctionCall(contract: AbstractContract, name: string, args: SupportedParamType[]): FunctionCall {
for (const entity of this.abi) {
if (entity.name === name) {
contract.checkArgs(name, entity.params, ...args);
const args_ = contract.checkArgs(name, entity.params, ...args);

const flatteredArgs = entity.params.flatMap((p, index) => {
const a = Object.assign({ ...p }, {
value: args[index]
value: args_[index]
}) as Argument;

return flatternArg(a, this.resolver, { state: false, ignoreValue: false });
Expand All @@ -341,7 +341,7 @@ export class ABICoder {
contract, unlockingScript: bsv.Script.fromHex(hex), args: entity.params.map((param, index) => ({
name: param.name,
type: param.type,
value: args[index]
value: args_[index]
}))
});
}
Expand Down
160 changes: 136 additions & 24 deletions src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { basename, dirname } from 'path';
import { ABIEntityType, Argument, LibraryEntity, ParamEntity, parseGenericType } from '.';
import { ContractEntity, getFullFilePath, loadSourceMapfromArtifact, OpCode, StaticEntity } from './compilerWrapper';
import {
ABICoder, ABIEntity, AliasEntity, Arguments, bsv, buildContractCode, CompileResult, DEFAULT_FLAGS, findSrcInfoV1, findSrcInfoV2, FunctionCall, hash160, JSONParserSync, path2uri, resolveType, Script, StructEntity, TypeResolver, uri2path
ABICoder, ABIEntity, AliasEntity, Arguments, bsv, buildContractCode, CompileResult, DEFAULT_FLAGS, findSrcInfoV1, findSrcInfoV2, FunctionCall, hash160, isArrayType, JSONParserSync, path2uri, resolveType, Script, StructEntity, subscript, TypeResolver, uri2path
} from './internal';
import { Bytes, HashedMap, HashedSet, isScryptType, SupportedParamType, SymbolType, TypeInfo } from './scryptTypes';
import { Bytes, isScryptType, SupportedParamType, SymbolType, TypeInfo } from './scryptTypes';
import Stateful from './stateful';
import { checkSupportedParamType, flatternArg } from './typeCheck';
import { arrayTypeAndSize, checkSupportedParamType, flatternArg, subArrayType } from './typeCheck';


export interface TxContext {
Expand Down Expand Up @@ -189,7 +189,8 @@ export class AbstractContract {
const newState: Arguments = stateArgs.map(arg => {
if (Object.prototype.hasOwnProperty.call(states, arg.name)) {
resolveKeys.push(arg.name);
const state = states[arg.name];
let state = states[arg.name];
state = this.transformerArg(state, arg, true);
const error = checkSupportedParamType(state, arg, this.resolver);

if (error) {
Expand All @@ -201,7 +202,7 @@ export class AbstractContract {
...arg
},
{
value: states[arg.name]
value: state
}
);
} else {
Expand Down Expand Up @@ -482,20 +483,141 @@ export class AbstractContract {
* all values is hex string, need convert it to number or bytes on using
*/
get asmVars(): AsmVarValues | null {
const ContractClass = Object.getPrototypeOf(this).constructor as typeof AbstractContract;
return ContractClass.getAsmVars(this.scriptedConstructor.toHex());
return this.ContractClass.getAsmVars(this.scriptedConstructor.toHex());
}

public checkArgs(funname: string, params: ParamEntity[], ...args: SupportedParamType[]): void {
get ContractClass(): typeof AbstractContract {
return Object.getPrototypeOf(this).constructor;
}


private transformerArgs(args: SupportedParamType, params: ParamEntity[], state: boolean): SupportedParamType[] {
return params.map((p, index) => this.transformerArg(args[index], p, state));
}


private transformerArg(arg: SupportedParamType, param: ParamEntity, state: boolean): SupportedParamType {

const typeInfo = this.resolver(param.type);

if (isArrayType(typeInfo.finalType)) {
const [_, arraySizes] = arrayTypeAndSize(typeInfo.finalType);

if (!Array.isArray(arg)) {
return arg;
}

if (arg.length !== arraySizes[0]) {
return arg;
}

const subType = subArrayType(param.type);

const results = [] as SupportedParamType[];

for (let i = 0; i < arraySizes[0]; i++) {
const elem = arg[i];
results.push(this.transformerArg(elem, {
name: `${param.name}${subscript(i, arraySizes)}`,
type: subType
}, state));
}

return results;

} else if (typeInfo.symbolType === SymbolType.Library) {

const entity: LibraryEntity = typeInfo.info as LibraryEntity;

if (entity.name === 'HashedMap') {
if (arg instanceof Map) {
if (state) {
return {
_data: this.ContractClass.toData(arg as Map<SupportedParamType, SupportedParamType>, param.type)
};
} else {
return [this.ContractClass.toData(arg as Map<SupportedParamType, SupportedParamType>, param.type)];
}
}
} else if (entity.name === 'HashedSet') {
if (arg instanceof Set) {
if (state) {
return {
_data: this.ContractClass.toData(arg as Set<SupportedParamType>, param.type)
};
} else {
return [this.ContractClass.toData(arg as Set<SupportedParamType>, param.type)];
}
}
}

const params: ParamEntity[] = state ? entity.properties : entity.params;

if (!state && Array.isArray(arg)) {
return params.map((p, index) => {
return this.transformerArg(arg[index], p, state);
});
} else if (state && typeof arg === 'object') {
return params.reduce((acc: any, p: ParamEntity) => {

Object.assign(acc, {
[p.name]: this.transformerArg(arg[p.name], p, state)
});
return acc;
}, {});
}



} else if (typeInfo.symbolType === SymbolType.Struct) {

if (!Array.isArray(arg) && typeof arg === 'object') {
const entity: StructEntity = typeInfo.info as StructEntity;

if (entity.name === 'SortedItem') {
if (arg['idx'] === -1n && (arg['image'] instanceof Map || arg['image'] instanceof Set)) {

const [_, genericTypes] = parseGenericType(typeInfo.finalType);
return Object.assign({}, {
idx: this.ContractClass.findKeyIndex(arg['image'], arg['item'], genericTypes[0]),
item: arg['item']
});
}

return arg;
}

const clone = Object.assign({}, arg);
entity.params.forEach(property => {
if (arg[property.name]) {
clone[property.name] = this.transformerArg(arg[property.name], property, state);
}
});

return clone;
}


}

return arg;

}


public checkArgs(funname: string, params: ParamEntity[], ...args: SupportedParamType[]): SupportedParamType[] {

if (args.length !== params.length) {
throw new Error(`wrong number of arguments for '${this.contractName}.${funname}', expected ${params.length} but got ${args.length}`);
}

const args_ = this.transformerArgs(args, params, false);
params.forEach((param, index) => {
const arg = args[index];
const arg = args_[index];
const error = checkSupportedParamType(arg, param, this.resolver);
if (error) throw error;
});
return args_;
}


Expand Down Expand Up @@ -559,7 +681,7 @@ export class AbstractContract {
}

// sort the map by the result of flattenSha256 of the key
static sortmap(map: Map<SupportedParamType, SupportedParamType>, keyType: string): Map<SupportedParamType, SupportedParamType> {
private static sortmap(map: Map<SupportedParamType, SupportedParamType>, keyType: string): Map<SupportedParamType, SupportedParamType> {
return new Map([...map.entries()].sort((a, b) => {
return bsv.crypto.BN.fromSM(Buffer.from(this.flattenSha256(a[0], keyType), 'hex'), {
endian: 'little'
Expand All @@ -570,7 +692,7 @@ export class AbstractContract {
}

// sort the set by the result of flattenSha256 of the key
static sortset(set: Set<SupportedParamType>, keyType: string): Set<SupportedParamType> {
private static sortset(set: Set<SupportedParamType>, keyType: string): Set<SupportedParamType> {
return new Set([...set.keys()].sort((a, b) => {
return bsv.crypto.BN.fromSM(Buffer.from(this.flattenSha256(a, keyType), 'hex'), {
endian: 'little'
Expand All @@ -581,7 +703,7 @@ export class AbstractContract {
}


static sortkeys(keys: SupportedParamType[], keyType: string): SupportedParamType[] {
private static sortkeys(keys: SupportedParamType[], keyType: string): SupportedParamType[] {
return keys.sort((a, b) => {
return bsv.crypto.BN.fromSM(Buffer.from(this.flattenSha256(a, keyType), 'hex'), {
endian: 'little'
Expand Down Expand Up @@ -632,19 +754,7 @@ export class AbstractContract {
}

return Bytes(storage);
}

static toHashedMap(collection: Map<SupportedParamType, SupportedParamType>, collectionType: string): HashedMap {
const data = this.toData(collection, collectionType);
const hashedMap = HashedMap(data);

return hashedMap;
}

static toHashedSet(collection: Set<SupportedParamType>, collectionType: string): HashedSet {
const data = this.toData(collection, collectionType);
const hashedSet = HashedSet(data);
return hashedSet;
}

}
Expand Down Expand Up @@ -759,6 +869,8 @@ export function buildContractClass(artifact: ContractArtifact | CompileResult):

if (arg) {

value = this.transformerArg(value, arg, true);

const error = checkSupportedParamType(value, arg, this.resolver);
if (error) throw error;

Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ export {
librarySign, structSign, resolveGenericType, canAssignProperty,
buildTypeResolver, getStructDeclaration, getABIDeclaration, typeOfArg,
compilerVersion, parseLiteral,
isEmpty, JSONParser, getFullFilePath, path2uri, uri2path, md5, FunctionCall, stringToBytes, isScryptType, isSubBytes, toJSON
isEmpty, JSONParser, getFullFilePath, path2uri, uri2path, md5, FunctionCall, stringToBytes, isScryptType, isSubBytes, toJSON,
getMapSortedItem, getSetSortedItem
} from './internal';

export {
Int, Bool, Bytes, PrivKey, PubKey, Sig, Ripemd160, Sha1, Sha256, SigHashType, SigHashPreimage,
OpCodeType, SupportedParamType, PubKeyHash, TxContext, ContractClass, Contract,
OpCodeType, SupportedParamType, PubKeyHash, TxContext, ContractClass, Contract, SortedItem, HashedMap, HashedSet,
StructObject, TypeResolver, PrimitiveTypes, AsmVarValues,
Arguments, Argument, StructEntity, LibraryEntity, ABIEntity, ABIEntityType, ABI, ParamEntity,
BuildType, RelatedInformation, ContractArtifact, VerifyResult, VerifyError, AbstractContract,
Expand Down
42 changes: 32 additions & 10 deletions src/scryptTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export type Sha256 = Bytes & { __type: 'Sha256' };
export type SigHashType = Bytes & { __type: 'SigHashType' };
export type SigHashPreimage = Bytes & { __type: 'SigHashPreimage' };
export type OpCodeType = Bytes & { __type: 'OpCodeType' };
export type HashedSet = Flavor<[Bytes], 'HashedSet'>;
export type HashedMap = Flavor<[Bytes], 'HashedMap'>;
export type HashedSet = Flavor<Set<SupportedParamType>, 'HashedSet'>;
export type HashedMap = Flavor<Map<SupportedParamType, SupportedParamType>, 'HashedMap'>;


export function Int(n: number | bigint): Int {
Expand Down Expand Up @@ -110,12 +110,12 @@ export function Sha256(b: Bytes): Sha256 {
return getValidatedHexString(b, false) as Sha256;
}

export function HashedSet(b: Bytes): HashedSet {
return [Bytes(b)] as HashedSet;
export function HashedSet(set: Set<SupportedParamType>): HashedSet {
return set as HashedSet;
}

export function HashedMap(b: Bytes): HashedMap {
return [Bytes(b)] as HashedMap;
export function HashedMap(map: Map<SupportedParamType, SupportedParamType>): HashedMap {
return map as HashedMap;
}

export enum SigHash {
Expand Down Expand Up @@ -147,9 +147,32 @@ export function OpCodeType(b: Bytes): OpCodeType {
return getValidatedHexString(b) as OpCodeType;
}

export type SortedItem<T> = {
idx: bigint,
item: T
};

export function getMapSortedItem<K, V>(map: Map<K, V>, k: K): SortedItem<K> {
return Object.assign({
idx: -1n,
item: k
}, {
image: new Map(map)
});
}

export function getSetSortedItem<E>(set: Set<E>, k: E): SortedItem<E> {
return Object.assign({
idx: -1n,
item: k
}, {
image: new Set(set)
});
}



export type PrimitiveTypes = Int | Bool | Bytes | PrivKey | PubKey | Sig | Sha256 | Sha1 | SigHashType | Ripemd160 | OpCodeType | HashedSet | HashedMap;
export type PrimitiveTypes = Int | Bool | Bytes | PrivKey | PubKey | Sig | Sha256 | Sha1 | SigHashType | Ripemd160 | OpCodeType | HashedMap | HashedSet;


export type SubBytes = PubKey | Sig | Sha256 | Sha1 | SigHashType | Ripemd160 | OpCodeType;
Expand All @@ -162,8 +185,6 @@ export interface StructObject {
export type SupportedParamType = PrimitiveTypes | StructObject | SupportedParamType[];




export function getValidatedHexString(hex: string, allowEmpty = true): string {

const ret = hex.trim();
Expand All @@ -184,7 +205,8 @@ export function getValidatedHexString(hex: string, allowEmpty = true): string {
}


export function toJSON(value: SupportedParamType): any {

export function toJSON(value: SupportedParamType): unknown {

if (Array.isArray(value)) {
const v = value as SupportedParamType[];
Expand Down
Loading