|
| 1 | +import * as StellarSdk from '@stellar/stellar-sdk'; |
| 2 | +import { readFileSync } from 'fs'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import path from 'path'; |
| 5 | +import { fileURLToPath } from 'url'; |
| 6 | +import { call_contract_function, extractLogEvent } from './test_helpers.js'; |
| 7 | +import { assert } from 'console'; |
| 8 | + |
| 9 | +const __filename = fileURLToPath(import.meta.url); |
| 10 | +const dirname = path.dirname(__filename); |
| 11 | +const server = new StellarSdk.SorobanRpc.Server("https://soroban-testnet.stellar.org:443"); |
| 12 | + |
| 13 | +function readContractAddress(filename) { |
| 14 | + return readFileSync(path.join(dirname, '.soroban', 'contract-ids', filename), 'utf8').trim(); |
| 15 | +} |
| 16 | + |
| 17 | +describe('Auth Framework', () => { |
| 18 | + let keypair, a, b, c, a_invalid; |
| 19 | + |
| 20 | + before(async () => { |
| 21 | + console.log('Setting up cross contract tests...'); |
| 22 | + |
| 23 | + keypair = StellarSdk.Keypair.fromSecret(readFileSync('alice.txt', 'utf8').trim()); |
| 24 | + a = new StellarSdk.Contract(readContractAddress('a.txt')); |
| 25 | + b = new StellarSdk.Contract(readContractAddress('b.txt')); |
| 26 | + c = new StellarSdk.Contract(readContractAddress('c.txt')); |
| 27 | + a_invalid = new StellarSdk.Contract(readContractAddress('a_invalid.txt')); |
| 28 | + }); |
| 29 | + |
| 30 | + it('calls a', async () => { |
| 31 | + |
| 32 | + |
| 33 | + let values = [ |
| 34 | + b.address().toScVal(), |
| 35 | + c.address().toScVal() |
| 36 | + ]; |
| 37 | + |
| 38 | + |
| 39 | + let res = await call_contract_function("call_b", server, keypair, a, ...values); |
| 40 | + |
| 41 | + |
| 42 | + expect(res.returnValue().value().toString()).to.equal("22"); |
| 43 | + |
| 44 | + }); |
| 45 | + |
| 46 | + it ('call falis with invalid `a` contract', async () => { |
| 47 | + |
| 48 | + |
| 49 | + let values = [ |
| 50 | + b.address().toScVal(), |
| 51 | + c.address().toScVal() |
| 52 | + ]; |
| 53 | + |
| 54 | + let res = await call_contract_function("call_b", server, keypair, a_invalid, ...values); |
| 55 | + |
| 56 | + assert(res.toString().includes("recording authorization only] encountered authorization not tied to the root contract invocation for an address. Use `require_auth()` in the top invocation or enable non-root authorization.")); |
| 57 | + |
| 58 | + }); |
| 59 | + |
| 60 | + |
| 61 | +}); |
0 commit comments