Comparing version 0.0.8 to 0.0.9
{ | ||
"name": "beaker-ts", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -5,3 +5,3 @@ import algosdk from "algosdk"; | ||
ts: bigint; | ||
seed: number[]; | ||
seed: Uint8Array; | ||
static codec: algosdk.ABIType = algosdk.ABIType.from("(uint64,byte[32])"); | ||
@@ -49,5 +49,5 @@ static fields: string[] = ["ts", "seed"]; | ||
} | ||
async sha3_256(to_hash: string): Promise<ABIResult<number[]>> { | ||
async sha3_256(to_hash: string): Promise<ABIResult<Uint8Array>> { | ||
const result = await this.call(algosdk.getMethodByName(this.methods, "sha3_256"), { to_hash: to_hash }); | ||
return new ABIResult<number[]>(result); | ||
return new ABIResult<Uint8Array>(result); | ||
} | ||
@@ -62,3 +62,3 @@ async noop(): Promise<ABIResult<void>> { | ||
} | ||
async ed25519verify_bare(msg: string, sig: number[]): Promise<ABIResult<boolean>> { | ||
async ed25519verify_bare(msg: string, sig: Uint8Array): Promise<ABIResult<boolean>> { | ||
const result = await this.call(algosdk.getMethodByName(this.methods, "ed25519verify_bare"), { msg: msg, sig: sig }); | ||
@@ -75,6 +75,6 @@ return new ABIResult<boolean>(result); | ||
} | ||
async vrf_verify(msg: number[], proof: number[], pub_key: string): Promise<ABIResult<number[]>> { | ||
async vrf_verify(msg: Uint8Array, proof: Uint8Array, pub_key: string): Promise<ABIResult<Uint8Array>> { | ||
const result = await this.call(algosdk.getMethodByName(this.methods, "vrf_verify"), { msg: msg, proof: proof, pub_key: pub_key }); | ||
return new ABIResult<number[]>(result); | ||
return new ABIResult<Uint8Array>(result); | ||
} | ||
} |
@@ -13,3 +13,3 @@ import { | ||
import algosdk, { abiCheckTransactionType, ABIMethod } from "algosdk"; | ||
import algosdk from "algosdk"; | ||
import ts, { factory, NodeFactory } from "typescript"; | ||
@@ -23,3 +23,3 @@ import { writeFileSync } from "fs"; | ||
// TODO: only import if we _need_ them | ||
const CLIENT_IMPORTS = `{${CLIENT_NAME}, ABIResult, decodeNamedTuple, Schema, AVMType}` | ||
const CLIENT_IMPORTS = `{${CLIENT_NAME}, ABIResult, decodeNamedTuple, Schema, AVMType}`; | ||
const CLIENT_PATH = "beaker-ts"; | ||
@@ -46,3 +46,3 @@ | ||
const structNodes = generateStructTypes(appSpec); | ||
nodes.push(...structNodes) | ||
nodes.push(...structNodes); | ||
@@ -98,3 +98,2 @@ const classNode = generateClass(appSpec); | ||
), | ||
]; | ||
@@ -106,5 +105,3 @@ } | ||
undefined, | ||
[ | ||
factory.createModifier(ts.SyntaxKind.ExportKeyword), | ||
], | ||
[factory.createModifier(ts.SyntaxKind.ExportKeyword)], | ||
factory.createIdentifier(appSpec.contract.name), | ||
@@ -122,3 +119,5 @@ undefined, | ||
...generateContractProperties(appSpec), | ||
...appSpec.contract.methods.map((meth) => generateMethodImpl(meth, appSpec)), | ||
...appSpec.contract.methods.map((meth) => | ||
generateMethodImpl(meth, appSpec) | ||
), | ||
] | ||
@@ -129,41 +128,66 @@ ); | ||
function tsTypeFromAbiType(argType: string | algosdk.ABIType): ts.TypeNode { | ||
if (typeof argType ==='string' ) { | ||
if (TXN_TYPES.includes(argType)) return factory.createUnionTypeNode([ | ||
factory.createTypeReferenceNode("algosdk.TransactionWithSigner"), | ||
factory.createTypeReferenceNode("algosdk.Transaction"), | ||
]); | ||
if (typeof argType === "string") { | ||
if (TXN_TYPES.includes(argType)) | ||
return factory.createUnionTypeNode([ | ||
factory.createTypeReferenceNode("algosdk.TransactionWithSigner"), | ||
factory.createTypeReferenceNode("algosdk.Transaction"), | ||
]); | ||
} | ||
try { | ||
// Might be a transaction | ||
const abiType = (typeof argType == 'string')?algosdk.ABIType.from(argType):argType | ||
switch(abiType.constructor) { | ||
case algosdk.ABIByteType: | ||
return factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword); | ||
case algosdk.ABIUintType: | ||
case algosdk.ABIUfixedType: | ||
return factory.createKeywordTypeNode(ts.SyntaxKind.BigIntKeyword); | ||
case algosdk.ABIAddressType: | ||
case algosdk.ABIStringType: | ||
return factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword); | ||
case algosdk.ABIBoolType: | ||
return factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword); | ||
case algosdk.ABIArrayStaticType: | ||
case algosdk.ABIArrayDynamicType: | ||
return factory.createArrayTypeNode(tsTypeFromAbiType((abiType as algosdk.ABIArrayStaticType).childType)) | ||
case algosdk.ABITupleType: | ||
return factory.createTupleTypeNode([ | ||
factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword), | ||
factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword) | ||
]) | ||
} | ||
// Might be a transaction | ||
const abiType = | ||
typeof argType == "string" ? algosdk.ABIType.from(argType) : argType; | ||
switch (abiType.constructor) { | ||
case algosdk.ABIByteType: | ||
return factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword); | ||
case algosdk.ABIUintType: | ||
case algosdk.ABIUfixedType: | ||
return factory.createKeywordTypeNode(ts.SyntaxKind.BigIntKeyword); | ||
case algosdk.ABIAddressType: | ||
case algosdk.ABIStringType: | ||
return factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword); | ||
case algosdk.ABIBoolType: | ||
return factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword); | ||
case algosdk.ABIArrayStaticType: | ||
const asStaticArr = abiType as algosdk.ABIArrayStaticType; | ||
switch (asStaticArr.childType.constructor) { | ||
// If its bytes, make it a uint8array | ||
case algosdk.ABIByteType: | ||
return factory.createTypeReferenceNode( | ||
factory.createIdentifier("Uint8Array") | ||
); | ||
} | ||
}catch{} | ||
return factory.createArrayTypeNode( | ||
tsTypeFromAbiType(asStaticArr.childType) | ||
); | ||
case algosdk.ABIArrayDynamicType: | ||
const asArr = abiType as algosdk.ABIArrayStaticType; | ||
switch (asArr.childType.constructor) { | ||
// If its bytes, make it a uint8array | ||
case algosdk.ABIByteType: | ||
return factory.createTypeReferenceNode( | ||
factory.createIdentifier("Uint8Array") | ||
); | ||
} | ||
return factory.createArrayTypeNode(tsTypeFromAbiType(asArr.childType)); | ||
case algosdk.ABITupleType: | ||
const asTuple = abiType as algosdk.ABITupleType; | ||
return factory.createTupleTypeNode( | ||
asTuple.childTypes.map((elem: algosdk.ABIType) => { | ||
return tsTypeFromAbiType(elem); | ||
}) | ||
); | ||
} | ||
return factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword); | ||
} | ||
function generateMethodImpl(method: ABIMethod, spec: AppSpec): ts.ClassElement { | ||
function generateMethodImpl( | ||
method: algosdk.ABIMethod, | ||
spec: AppSpec | ||
): ts.ClassElement { | ||
const params: ts.ParameterDeclaration[] = []; | ||
@@ -175,5 +199,4 @@ | ||
let hint = {} as Hint | ||
if(method.name in spec.hints) | ||
hint = spec.hints[method.name] | ||
let hint = {} as Hint; | ||
if (method.name in spec.hints) hint = spec.hints[method.name]; | ||
@@ -195,15 +218,7 @@ callArgs.push( | ||
for (const arg of method.args) { | ||
let argType | ||
if(hint.structs !== undefined && arg.name in hint.structs) { | ||
// Its got a struct def, so we should specify the struct type in args and | ||
// get the values when we call `call` | ||
argType = factory.createTypeReferenceNode(hint.structs[arg.name].name) | ||
abiMethodArgs.push( | ||
factory.createPropertyAssignment( | ||
factory.createIdentifier(arg.name), | ||
factory.createIdentifier(arg.name), | ||
) | ||
); | ||
}else{ | ||
argType = tsTypeFromAbiType(arg.type.toString()) | ||
let argType; | ||
if (hint.structs !== undefined && arg.name in hint.structs) { | ||
// Its got a struct def, so we should specify the struct type in args and | ||
// get the values when we call `call` | ||
argType = factory.createTypeReferenceNode(hint.structs[arg.name].name); | ||
abiMethodArgs.push( | ||
@@ -215,2 +230,10 @@ factory.createPropertyAssignment( | ||
); | ||
} else { | ||
argType = tsTypeFromAbiType(arg.type.toString()); | ||
abiMethodArgs.push( | ||
factory.createPropertyAssignment( | ||
factory.createIdentifier(arg.name), | ||
factory.createIdentifier(arg.name) | ||
) | ||
); | ||
} | ||
@@ -232,26 +255,28 @@ | ||
// Set up return type | ||
let abiRetType: ts.TypeNode = factory.createKeywordTypeNode(ts.SyntaxKind.VoidKeyword); | ||
let resultArgs: ts.Expression[] = [factory.createIdentifier("result")] | ||
let abiRetType: ts.TypeNode = factory.createKeywordTypeNode( | ||
ts.SyntaxKind.VoidKeyword | ||
); | ||
let resultArgs: ts.Expression[] = [factory.createIdentifier("result")]; | ||
if(method.returns.type.toString() !== "void"){ | ||
abiRetType = tsTypeFromAbiType(method.returns.type.toString()) | ||
// Always `output` here because pyteal, | ||
if (method.returns.type.toString() !== "void") { | ||
abiRetType = tsTypeFromAbiType(method.returns.type.toString()); | ||
// Always `output` here because pyteal, | ||
// when others app specs come in we should consider them | ||
if(hint.structs !== undefined && 'output' in hint.structs){ | ||
abiRetType = factory.createTypeReferenceNode(hint.structs['output'].name) | ||
if (hint.structs !== undefined && "output" in hint.structs) { | ||
abiRetType = factory.createTypeReferenceNode(hint.structs["output"].name); | ||
resultArgs.push( | ||
factory.createCallExpression( | ||
factory.createCallExpression( | ||
factory.createPropertyAccessExpression( | ||
factory.createIdentifier(hint.structs["output"].name), | ||
factory.createIdentifier("decodeResult") | ||
), | ||
undefined, | ||
[ | ||
factory.createPropertyAccessExpression( | ||
factory.createIdentifier(hint.structs['output'].name), | ||
factory.createIdentifier('decodeResult') | ||
factory.createIdentifier("result"), | ||
factory.createIdentifier("returnValue") | ||
), | ||
undefined, | ||
[ | ||
factory.createPropertyAccessExpression( | ||
factory.createIdentifier("result"), | ||
factory.createIdentifier("returnValue"), | ||
) | ||
] | ||
), | ||
) | ||
] | ||
) | ||
); | ||
} | ||
@@ -277,6 +302,9 @@ } | ||
undefined, | ||
[...callArgs, factory.createObjectLiteralExpression(abiMethodArgs)] | ||
[ | ||
...callArgs, | ||
factory.createObjectLiteralExpression(abiMethodArgs), | ||
] | ||
) | ||
) | ||
) | ||
), | ||
], | ||
@@ -292,3 +320,3 @@ ts.NodeFlags.Const | ||
) | ||
) | ||
), | ||
], | ||
@@ -298,8 +326,10 @@ true | ||
let retType = factory.createTypeReferenceNode( | ||
factory.createIdentifier("Promise"), | ||
[factory.createTypeReferenceNode( factory.createIdentifier("ABIResult") , [abiRetType])] | ||
) | ||
[ | ||
factory.createTypeReferenceNode(factory.createIdentifier("ABIResult"), [ | ||
abiRetType, | ||
]), | ||
] | ||
); | ||
@@ -363,3 +393,3 @@ const methodSpec = factory.createMethodDeclaration( | ||
factory.createIdentifier("max_keys"), | ||
factory.createNumericLiteral(sv[1].max_keys?sv[1].max_keys:0) | ||
factory.createNumericLiteral(sv[1].max_keys ? sv[1].max_keys : 0) | ||
), | ||
@@ -384,12 +414,12 @@ ]) | ||
function generateStructTypes(spec: AppSpec): ts.Node[] { | ||
const hints = spec.hints | ||
const hints = spec.hints; | ||
const structs = {} | ||
for(const k of Object.keys(hints)){ | ||
const hint = hints[k] | ||
if(hint.structs !== undefined){ | ||
for(const sk of Object.keys(hint.structs)){ | ||
const struct = hint.structs[sk] | ||
if(!(struct.name in struct)){ | ||
structs[struct.name] = generateStruct(struct) | ||
const structs = {}; | ||
for (const k of Object.keys(hints)) { | ||
const hint = hints[k]; | ||
if (hint.structs !== undefined) { | ||
for (const sk of Object.keys(hint.structs)) { | ||
const struct = hint.structs[sk]; | ||
if (!(struct.name in struct)) { | ||
structs[struct.name] = generateStruct(struct); | ||
} | ||
@@ -400,13 +430,13 @@ } | ||
return Object.values(structs) | ||
return Object.values(structs); | ||
} | ||
function generateStruct(s: Struct): ts.ClassDeclaration { | ||
const members: ts.ClassElement[] = [] | ||
const members: ts.ClassElement[] = []; | ||
const tupleTypes: string[] = []; | ||
const tupleNames: string[] = []; | ||
for(const elem of s.elements){ | ||
tupleNames.push(elem[0]) | ||
tupleTypes.push(elem[1]) | ||
for (const elem of s.elements) { | ||
tupleNames.push(elem[0]); | ||
tupleTypes.push(elem[1]); | ||
@@ -420,5 +450,5 @@ members.push( | ||
tsTypeFromAbiType(elem[1]), | ||
undefined, | ||
undefined | ||
) | ||
) | ||
); | ||
} | ||
@@ -428,36 +458,40 @@ | ||
factory.createPropertyDeclaration( | ||
undefined, | ||
[factory.createModifier(ts.SyntaxKind.StaticKeyword)], | ||
factory.createIdentifier("codec"), | ||
undefined, | ||
factory.createTypeReferenceNode( | ||
factory.createQualifiedName( | ||
undefined, | ||
[factory.createModifier(ts.SyntaxKind.StaticKeyword)], | ||
factory.createIdentifier("codec"), | ||
undefined, | ||
factory.createTypeReferenceNode( | ||
factory.createQualifiedName( | ||
factory.createIdentifier("algosdk"), | ||
factory.createIdentifier("ABIType") | ||
), | ||
undefined | ||
), | ||
factory.createCallExpression( | ||
factory.createPropertyAccessExpression( | ||
factory.createPropertyAccessExpression( | ||
factory.createIdentifier("algosdk"), | ||
factory.createIdentifier("ABIType") | ||
), | ||
undefined | ||
factory.createIdentifier("from") | ||
), | ||
factory.createCallExpression( | ||
factory.createPropertyAccessExpression( | ||
factory.createPropertyAccessExpression( | ||
factory.createIdentifier("algosdk"), | ||
factory.createIdentifier("ABIType") | ||
), | ||
factory.createIdentifier("from") | ||
), | ||
undefined, | ||
[factory.createStringLiteral(`(${tupleTypes.join(",")})`)] | ||
) | ||
undefined, | ||
[factory.createStringLiteral(`(${tupleTypes.join(",")})`)] | ||
) | ||
) | ||
) | ||
members.push(factory.createPropertyDeclaration( | ||
undefined, | ||
[factory.createModifier(ts.SyntaxKind.StaticKeyword)], | ||
factory.createIdentifier("fields"), | ||
undefined, | ||
factory.createTypeReferenceNode("string[]"), | ||
factory.createArrayLiteralExpression( | ||
tupleNames.map((name)=>{ return factory.createStringLiteral(name) }) | ||
); | ||
members.push( | ||
factory.createPropertyDeclaration( | ||
undefined, | ||
[factory.createModifier(ts.SyntaxKind.StaticKeyword)], | ||
factory.createIdentifier("fields"), | ||
undefined, | ||
factory.createTypeReferenceNode("string[]"), | ||
factory.createArrayLiteralExpression( | ||
tupleNames.map((name) => { | ||
return factory.createStringLiteral(name); | ||
}) | ||
) | ||
) | ||
)) | ||
); | ||
@@ -467,9 +501,10 @@ members.push( | ||
factory.createMethodDeclaration( | ||
undefined, | ||
[factory.createModifier(ts.SyntaxKind.StaticKeyword)], | ||
undefined, | ||
factory.createIdentifier("decodeResult"), | ||
undefined, | ||
undefined, | ||
[factory.createParameterDeclaration( | ||
undefined, | ||
[factory.createModifier(ts.SyntaxKind.StaticKeyword)], | ||
undefined, | ||
factory.createIdentifier("decodeResult"), | ||
undefined, | ||
undefined, | ||
[ | ||
factory.createParameterDeclaration( | ||
undefined, | ||
@@ -488,39 +523,48 @@ undefined, | ||
undefined | ||
)], | ||
factory.createTypeReferenceNode( | ||
factory.createIdentifier(s.name), | ||
undefined | ||
), | ||
factory.createBlock( | ||
[factory.createReturnStatement(factory.createAsExpression( | ||
factory.createCallExpression( | ||
factory.createIdentifier("decodeNamedTuple"), | ||
undefined, | ||
[ | ||
factory.createIdentifier("val"), | ||
factory.createPropertyAccessExpression( | ||
factory.createIdentifier(s.name), | ||
factory.createIdentifier("fields") | ||
), | ||
] | ||
), | ||
factory.createTypeReferenceNode( | ||
factory.createIdentifier(s.name), | ||
undefined | ||
], | ||
factory.createTypeReferenceNode( | ||
factory.createIdentifier(s.name), | ||
undefined | ||
), | ||
factory.createBlock( | ||
[ | ||
factory.createReturnStatement( | ||
factory.createAsExpression( | ||
factory.createCallExpression( | ||
factory.createIdentifier("decodeNamedTuple"), | ||
undefined, | ||
[ | ||
factory.createIdentifier("val"), | ||
factory.createPropertyAccessExpression( | ||
factory.createIdentifier(s.name), | ||
factory.createIdentifier("fields") | ||
), | ||
] | ||
), | ||
factory.createTypeReferenceNode( | ||
factory.createIdentifier(s.name), | ||
undefined | ||
) | ||
) | ||
))], | ||
true | ||
) | ||
), | ||
], | ||
true | ||
) | ||
) | ||
); | ||
members.push( | ||
factory.createMethodDeclaration( | ||
undefined, | ||
[factory.createModifier(ts.SyntaxKind.StaticKeyword)], | ||
undefined, | ||
factory.createIdentifier("decodeBytes"), | ||
undefined, undefined, | ||
[factory.createParameterDeclaration( | ||
undefined, undefined, undefined, | ||
members.push( | ||
factory.createMethodDeclaration( | ||
undefined, | ||
[factory.createModifier(ts.SyntaxKind.StaticKeyword)], | ||
undefined, | ||
factory.createIdentifier("decodeBytes"), | ||
undefined, | ||
undefined, | ||
[ | ||
factory.createParameterDeclaration( | ||
undefined, | ||
undefined, | ||
undefined, | ||
factory.createIdentifier("val"), | ||
@@ -533,42 +577,45 @@ undefined, | ||
undefined | ||
)], | ||
factory.createTypeReferenceNode( | ||
factory.createIdentifier(s.name), | ||
undefined | ||
), | ||
factory.createBlock( | ||
[factory.createReturnStatement(factory.createAsExpression( | ||
factory.createCallExpression( | ||
factory.createIdentifier("decodeNamedTuple"), | ||
undefined, | ||
[ | ||
factory.createCallExpression( | ||
factory.createPropertyAccessExpression( | ||
], | ||
factory.createTypeReferenceNode( | ||
factory.createIdentifier(s.name), | ||
undefined | ||
), | ||
factory.createBlock( | ||
[ | ||
factory.createReturnStatement( | ||
factory.createAsExpression( | ||
factory.createCallExpression( | ||
factory.createIdentifier("decodeNamedTuple"), | ||
undefined, | ||
[ | ||
factory.createCallExpression( | ||
factory.createPropertyAccessExpression( | ||
factory.createIdentifier(s.name), | ||
factory.createIdentifier("codec") | ||
factory.createPropertyAccessExpression( | ||
factory.createIdentifier(s.name), | ||
factory.createIdentifier("codec") | ||
), | ||
factory.createIdentifier("decode") | ||
), | ||
factory.createIdentifier("decode") | ||
undefined, | ||
[factory.createIdentifier("val")] | ||
), | ||
undefined, | ||
[factory.createIdentifier("val")] | ||
), | ||
factory.createPropertyAccessExpression( | ||
factory.createIdentifier(s.name), | ||
factory.createIdentifier("fields") | ||
), | ||
] | ||
), | ||
factory.createTypeReferenceNode( | ||
factory.createIdentifier(s.name), | ||
undefined | ||
factory.createPropertyAccessExpression( | ||
factory.createIdentifier(s.name), | ||
factory.createIdentifier("fields") | ||
), | ||
] | ||
), | ||
factory.createTypeReferenceNode( | ||
factory.createIdentifier(s.name), | ||
undefined | ||
) | ||
) | ||
))], | ||
true | ||
) | ||
), | ||
], | ||
true | ||
) | ||
) | ||
); | ||
return factory.createClassDeclaration( | ||
@@ -581,4 +628,3 @@ undefined, | ||
members | ||
) | ||
); | ||
} | ||
@@ -585,0 +631,0 @@ |
242535
4286