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

error: XDR Write Error: 3374067422357648927 is not a Hyper when trying to .toXDR() a xdr.HashIdPreimage.envelopeTypeSorobanAuthorization #1021

Open
kalepail opened this issue Jul 24, 2024 · 0 comments
Labels

Comments

@kalepail
Copy link
Contributor

Describe the bug
When I try to convert a xdr.HashIdPreimage.envelopeTypeSorobanAuthorization .toXDR() I get an error.

What version are you on?
"@stellar/stellar-sdk": "^12.2.0",
I'm also using TS bindings for this and this does seem to be related to trying to use the TS bindings simulated response.

To Reproduce
Build a preimage with a TS bindings simulationData and then try to convert that ``toXDR()`

Expected behavior
It should convert the preimage to XDR.

Additional context
Here's my code.

import { SorobanRpc, Networks, Keypair, xdr, hash, Address, Operation, Transaction, authorizeEntry } from '@stellar/stellar-sdk'
import { basicNodeSigner, DEFAULT_TIMEOUT } from '@stellar/stellar-sdk/contract';
import { Client, networks } from 'puzzle-sdk'

const keypair = Keypair.fromSecret('<SK>');
const publicKey = keypair.publicKey()

const puzzleSAC = 'CDGOXJBEKI3MQDB3J477NN3HAQBDCNK5YYB2ZKAG24US53RXW44QIF6Z'
const puzzleId = 'CCPYY3EQZQ6SQE2XRHCU5VVH4DCR3ZCNRHIS5ITCSMBK2WOPMN56LEAV'
const solverId = 'CD3QSVJ2FLC4XMRHPVS3NM2TFFKXNTGINP3A55W5P5FKZR5YHS6PABT2'

const rpcUrl = 'https://soroban-testnet.stellar.org'
const rpc = new SorobanRpc.Server(rpcUrl)

const networkPassphrase = Networks.TESTNET

const contract = new Client({
    ...networks.testnet,
    ...basicNodeSigner(keypair, networkPassphrase),
    networkPassphrase,
    contractId: solverId,
    publicKey,
    rpcUrl,
})

const { built, simulationData } = await contract.call({
    puzzle: puzzleId,
    sac: puzzleSAC
})

const entry = simulationData.result.auth[0];
const credentials = entry.credentials().address();
const lastLedger = await rpc.getLatestLedger().then(({ sequence }) => sequence);

const preimage = xdr.HashIdPreimage.envelopeTypeSorobanAuthorization(
    new xdr.HashIdPreimageSorobanAuthorization({
        networkId: hash(Buffer.from(networkPassphrase)),
        nonce: credentials.nonce(),
        signatureExpirationLedger: lastLedger + DEFAULT_TIMEOUT,
        invocation: entry.rootInvocation()
    })
)
const payload = hash(preimage.toXDR()) // <- breaks here
const signature = keypair.sign(payload)

credentials.signatureExpirationLedger(lastLedger + DEFAULT_TIMEOUT)
credentials.signature(xdr.ScVal.scvMap([
    new xdr.ScMapEntry({
        key: xdr.ScVal.scvSymbol('address'),
        val: Address.fromString(publicKey).toScVal()
    }),
    new xdr.ScMapEntry({
        key: xdr.ScVal.scvSymbol('signature'),
        val: xdr.ScVal.scvBytes(signature),
    }),
]))

const op = built?.operations[0] as Operation.InvokeHostFunction

op.auth?.splice(0, 1, entry)

console.log('\n', built?.toXDR(), '\n');

const sim = await rpc.simulateTransaction(built!)

if (SorobanRpc.Api.isSimulationError(sim)) 
    throw sim.error

if (SorobanRpc.Api.isSimulationRestore(sim))
    throw 'Restore required'

const txn = SorobanRpc.assembleTransaction(new Transaction(built!.toXDR(), networkPassphrase), sim).build()

txn.sign(keypair)

console.log('\n', txn.toXDR(), '\n');

const res = await rpc.sendTransaction(txn)

console.log(res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant