2021-11-05 11:20:58 -07:00
/* eslint-disable no-unused-expressions */
2021-11-04 06:24:58 -07:00
const PricecasterLib = require ( '../lib/pricecaster' )
const tools = require ( '../tools/app-tools' )
const algosdk = require ( 'algosdk' )
const { expect } = require ( 'chai' )
const chai = require ( 'chai' )
chai . use ( require ( 'chai-as-promised' ) )
2021-11-09 18:51:29 -08:00
const spawnSync = require ( 'child_process' ) . spawnSync
const fs = require ( 'fs' )
let pclib
let algodClient
2021-11-04 06:24:58 -07:00
const OWNER _ADDR = 'OPDM7ACAW64Q4VBWAL77Z5SHSJVZZ44V3BAN7W44U43SUXEOUENZMZYOQU'
const OWNER _MNEMO = 'assault approve result rare float sugar power float soul kind galaxy edit unusual pretty tone tilt net range pelican avoid unhappy amused recycle abstract master'
const OTHER _ADDR = 'DMTBK62XZ6KNI7L5E6TRBTPB4B3YNVB4WYGSWR42SEV4XKV4LYHGBW4O34'
const OTHER _MNEMO = 'old agree harbor cost pink fog chunk hope vital used rural soccer model acquire clown host friend bring marriage surge dirt surge slab absent punch'
const SIGNATURES = { }
SIGNATURES [ OWNER _ADDR ] = algosdk . mnemonicToSecretKey ( OWNER _MNEMO )
SIGNATURES [ OTHER _ADDR ] = algosdk . mnemonicToSecretKey ( OTHER _MNEMO )
2021-11-09 18:51:29 -08:00
const gkeys = [
'13947Bd48b18E53fdAeEe77F3473391aC727C638' ,
'F18AbBac073741DD0F002147B735Ff642f3D113F' ,
'9925A94DC043D0803f8ef502D2dB15cAc9e02D76' ,
'9e4EC2D92af8602bCE74a27F99A836f93C4a31E4' ,
'9C40c4052A3092AfB8C99B985fcDfB586Ed19c98' ,
'B86020cF1262AA4dd5572Af76923E271169a2CA7' ,
'1937617fE1eD801fBa14Bd8BB9EDEcBA7A942FFe' ,
'9475b8D45DdE53614d92c779787C27fE2ef68752' ,
'15A53B22c28AbC7B108612146B6aAa4a537bA305' ,
'63842657C7aC7e37B04FBE76b8c54EFe014D04E1' ,
'948ca1bBF4B858DF1A505b4C69c5c61bD95A12Bd' ,
'A6923e2259F8B5541eD18e410b8DdEE618337ff0' ,
'F678Daf4b7f2789AA88A081618Aa966D6a39e064' ,
'8cF31021838A8B3fFA43a71a50609877846f9E6d' ,
'eB15bCF2ae4f957012330B4741ecE3242De96184' ,
'cc3766a03e4faec44Bda7a46D9Ea2A9D124e9Bf8' ,
'841f499Ba89a6a8E9dD273BAd82Beb175094E5d7' ,
'f5F2b82576e6CA17965dee853d08bbB471FA2433' ,
'2bC2B1204599D4cA0d4Dde4a658a42c4dD13103a'
]
2021-11-05 11:33:58 -07:00
function makeVAA ( ) {
2021-11-05 11:20:58 -07:00
2021-11-04 06:24:58 -07:00
}
2021-11-09 18:51:29 -08:00
async function createApp ( gsexptime , gkeys ) {
const txId = await pclib . createVaaProcessorApp ( OWNER _ADDR , gsexptime , gkeys . join ( '' ) , signCallback )
const txResponse = await pclib . waitForTransactionResponse ( txId )
const appId = pclib . appIdFromCreateAppResponse ( txResponse )
pclib . setAppId ( appId )
return appId
}
2021-11-05 11:33:58 -07:00
function signCallback ( sender , tx ) {
2021-11-04 06:24:58 -07:00
const txSigned = tx . signTxn ( SIGNATURES [ sender ] . sk )
return txSigned
}
describe ( 'VAA Processor Smart-contract Tests' , function ( ) {
let appId
before ( async function ( ) {
2021-11-09 18:51:29 -08:00
algodClient = new algosdk . Algodv2 ( '' , 'https://api.testnet.algoexplorer.io' , '' )
2021-11-04 06:24:58 -07:00
pclib = new PricecasterLib . PricecasterLib ( algodClient )
console . log ( 'Clearing accounts of all previous apps...' )
const appsTo = await tools . readCreatedApps ( algodClient , OWNER _ADDR )
for ( let i = 0 ; i < appsTo . length ; i ++ ) {
console . log ( 'Clearing ' + appsTo [ i ] . id )
try {
const txId = await pclib . deleteApp ( OWNER _ADDR , signCallback , appsTo [ i ] . id )
await pclib . waitForConfirmation ( txId )
} catch ( e ) {
console . error ( 'Could not delete application! Reason: ' + e )
}
}
2021-11-09 18:51:29 -08:00
const vaaProcessorClearState = 'test/temp/vaa-clear-state.teal'
const vaaProcessorApproval = 'test/temp/vaa-processor.teal'
pclib . setVaaProcessorApprovalFile ( vaaProcessorApproval )
pclib . setVaaProcessorClearStateFile ( vaaProcessorClearState )
console . log ( spawnSync ( 'python' , [ 'teal/wormhole/pyteal/vaa-processor.py' , vaaProcessorApproval , vaaProcessorClearState ] ) . output . toString ( ) )
2021-11-05 11:20:58 -07:00
}
)
2021-11-09 18:51:29 -08:00
it ( 'Must fail to create app with incorrect guardian keys length' , async function ( ) {
const gsexptime = 2524618800
await expect ( createApp ( gsexptime , [ 'BADADDRESS' ] ) ) . to . be . rejectedWith ( 'Bad Request' )
} )
it ( 'Must create app with initial guardians and proper initial state' , async function ( ) {
const gsexptime = 2524618800
appId = await createApp ( gsexptime , gkeys )
console . log ( ' [Created appId: %d]' , appId )
const gscount = await tools . readAppGlobalStateByKey ( algodClient , appId , OWNER _ADDR , 'gscount' )
const gsexp = await tools . readAppGlobalStateByKey ( algodClient , appId , OWNER _ADDR , 'gsexp' )
expect ( gscount . toString ( ) ) . to . equal ( ( gkeys . length ) . toString ( ) )
expect ( gsexp . toString ( ) ) . to . equal ( gsexptime . toString ( ) )
let i = 0
const buf = Buffer . alloc ( 8 )
for ( const gk of gkeys ) {
buf . writeBigUint64BE ( BigInt ( i ++ ) )
const gkstate = await tools . readAppGlobalStateByKey ( algodClient , appId , OWNER _ADDR , buf . toString ( ) )
expect ( Buffer . from ( gkstate , 'base64' ) . toString ( 'hex' ) ) . to . equal ( gk . toLowerCase ( ) )
}
} )
it ( 'Must set stateless logic hash from owner' , async function ( ) {
const teal = 'test/temp/vaa-verify.teal'
spawnSync ( 'python' , [ 'teal/wormhole/pyteal/vaa-verify.py' , appId , teal ] )
const program = fs . readFileSync ( teal , 'utf8' )
const compiledProgram = await pclib . compileProgram ( program )
const txid = await pclib . setVAAVerifyProgramHash ( OWNER _ADDR , compiledProgram . hash , signCallback )
await pclib . waitForTransactionResponse ( txid )
const vphstate = await tools . readAppGlobalStateByKey ( algodClient , appId , OWNER _ADDR , 'vphash' )
2021-11-10 09:46:46 -08:00
expect ( vphstate ) . to . equal ( compiledProgram . hash )
2021-11-09 18:51:29 -08:00
} )
2021-11-05 11:33:58 -07:00
it ( 'Must disallow setting stateless logic hash from non-owner' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-09 18:51:29 -08:00
} )
it ( 'Must reject setting stateless logic hash from group transaction' , async function ( ) {
} )
it ( 'Must reject setting stateless logic hash with invalid address length' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-09 18:51:29 -08:00
} )
2021-11-05 11:33:58 -07:00
it ( 'Must verify and handle Pyth VAA' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-09 18:51:29 -08:00
} )
2021-11-05 11:33:58 -07:00
it ( 'Must verify and handle governance VAA' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-09 18:51:29 -08:00
} )
2021-11-05 11:33:58 -07:00
it ( 'Must reject unknown VAA' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-09 18:51:29 -08:00
} )
2021-11-05 11:33:58 -07:00
it ( 'Must reject incorrect transaction group size' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-09 18:51:29 -08:00
} )
2021-11-05 11:33:58 -07:00
it ( 'Must reject incorrect argument count for verify call' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-09 18:51:29 -08:00
} )
2021-11-05 11:33:58 -07:00
it ( 'Must reject unknown sender for verify call' , async function ( ) {
2021-11-09 18:51:29 -08:00
} )
2021-11-05 11:33:58 -07:00
it ( 'Must reject guardian set count argument not matching global state' , async function ( ) {
2021-11-09 18:51:29 -08:00
} )
2021-11-05 11:33:58 -07:00
it ( 'Must reject guardian key list argument not matching global state' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-09 18:51:29 -08:00
} )
2021-11-05 11:33:58 -07:00
it ( 'Must reject non-app call transaction in group' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-09 18:51:29 -08:00
} )
2021-11-05 11:33:58 -07:00
it ( 'Must reject app-call with mismatched AppId in group' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-09 18:51:29 -08:00
} )
2021-11-05 11:33:58 -07:00
it ( 'Must reject transaction with not verified bit set in group' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-05 11:33:58 -07:00
} )
it ( 'Stateless: Must reject transaction with excess fee' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-05 11:33:58 -07:00
} )
it ( 'Stateless: Must reject incorrect number of logic program arguments' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-05 11:33:58 -07:00
} )
it ( 'Stateless: Must reject transaction with mismatching number of signatures' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-05 11:33:58 -07:00
} )
it ( 'Stateless: Must reject transaction with non-zero rekey' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-05 11:33:58 -07:00
} )
it ( 'Stateless: Must reject transaction call from bad app-id' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-05 11:33:58 -07:00
} )
it ( 'Stateless: Must reject non-app call tx type' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-05 11:33:58 -07:00
} )
it ( 'Stateless: Must reject invalid group size' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-05 11:33:58 -07:00
} )
it ( 'Stateless: Must reject signature verification failure' , async function ( ) {
2021-11-05 11:20:58 -07:00
2021-11-05 11:33:58 -07:00
} )
} )