-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Updates for auth-next * Update stellar-base to 8.2.2-soroban.10 * Naming feedback * Add a couple unit tests for assembleTransaction
- Loading branch information
Paul Bellamy
authored
Feb 14, 2023
1 parent
9d5316d
commit b7d9441
Showing
8 changed files
with
187 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { | ||
Account, | ||
FeeBumpTransaction, | ||
Operation, | ||
Transaction, | ||
TransactionBuilder, | ||
xdr, | ||
} from "stellar-base"; | ||
|
||
// TODO: Transaction is immutable, so we need to re-build it here. :( | ||
export function assembleTransaction( | ||
raw: Transaction | FeeBumpTransaction, | ||
networkPassphrase: string, | ||
simulated: Array<null | { | ||
footprint: Buffer | string | xdr.LedgerFootprint; | ||
auth: Array<Buffer | string | xdr.ContractAuth>; | ||
}>, | ||
): Transaction { | ||
if ("innerTransaction" in raw) { | ||
// TODO: Handle feebump transactions | ||
return assembleTransaction( | ||
raw.innerTransaction, | ||
networkPassphrase, | ||
simulated, | ||
); | ||
} | ||
|
||
if (simulated.length !== raw.operations.length) { | ||
throw new Error( | ||
"number of simulated operations not equal to number of transaction operations", | ||
); | ||
} | ||
|
||
// TODO: Figure out a cleaner way to clone this transaction. | ||
const source = new Account(raw.source, `${parseInt(raw.sequence, 10) - 1}`); | ||
const txn = new TransactionBuilder(source, { | ||
fee: raw.fee, | ||
memo: raw.memo, | ||
networkPassphrase, | ||
timebounds: raw.timeBounds, | ||
ledgerbounds: raw.ledgerBounds, | ||
minAccountSequence: raw.minAccountSequence, | ||
minAccountSequenceAge: raw.minAccountSequenceAge, | ||
minAccountSequenceLedgerGap: raw.minAccountSequenceLedgerGap, | ||
extraSigners: raw.extraSigners, | ||
}); | ||
for (let i = 0; i < raw.operations.length; i++) { | ||
const rawOp = raw.operations[i]; | ||
if ("function" in rawOp) { | ||
const sim = simulated[i]; | ||
if (!sim) { | ||
throw new Error("missing simulated operation"); | ||
} | ||
let footprint = sim.footprint; | ||
if (!(footprint instanceof xdr.LedgerFootprint)) { | ||
footprint = xdr.LedgerFootprint.fromXDR(footprint.toString(), "base64"); | ||
} | ||
const auth = sim.auth.map((a) => | ||
a instanceof xdr.ContractAuth | ||
? a | ||
: xdr.ContractAuth.fromXDR(a.toString(), "base64"), | ||
); | ||
// TODO: Figure out a cleaner way to clone these operations | ||
txn.addOperation( | ||
Operation.invokeHostFunction({ | ||
function: rawOp.function, | ||
parameters: rawOp.parameters, | ||
footprint, | ||
auth, | ||
}), | ||
); | ||
} else { | ||
// TODO: Handle this. | ||
throw new Error("Unsupported operation type"); | ||
} | ||
} | ||
return txn.build(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
const xdr = SorobanClient.xdr; | ||
|
||
describe("assembleTransaction", () => { | ||
describe("FeeBumpTransaction", () => { | ||
// TODO: Add support for fee bump transactions | ||
}); | ||
|
||
describe("Transaction", () => { | ||
const networkPassphrase = SorobanClient.Networks.TESTNET; | ||
const source = new SorobanClient.Account( | ||
"GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI", | ||
"1", | ||
); | ||
const emptyFootprint = new xdr.LedgerFootprint({ | ||
readOnly: [], | ||
readWrite: [], | ||
}); | ||
function emptyTransaction() { | ||
return new SorobanClient.TransactionBuilder(source, { | ||
fee: 100, | ||
networkPassphrase, | ||
v1: true, | ||
}) | ||
.addOperation( | ||
SorobanClient.Operation.invokeHostFunction({ | ||
function: xdr.HostFunction.hostFunctionTypeInvokeContract([]), | ||
parameters: [], | ||
footprint: emptyFootprint, | ||
auth: [], | ||
}), | ||
) | ||
.setTimeout(SorobanClient.TimeoutInfinite) | ||
.build(); | ||
} | ||
|
||
it("adds the footprint to the transaction", () => { | ||
const txn = emptyTransaction(); | ||
|
||
const result = SorobanClient.assembleTransaction(txn, networkPassphrase, [ | ||
{ | ||
auth: [], | ||
footprint: new xdr.LedgerFootprint({ | ||
readOnly: [ | ||
xdr.LedgerKey.contractCode( | ||
new xdr.LedgerKeyContractCode({ | ||
hash: Buffer.alloc(32), | ||
}), | ||
), | ||
], | ||
readWrite: [], | ||
}), | ||
}, | ||
]); | ||
|
||
expect(result.operations[0].footprint.readOnly.length).to.equal(1); | ||
}); | ||
|
||
it("adds the auth to the transaction", () => { | ||
const txn = emptyTransaction(); | ||
|
||
const result = SorobanClient.assembleTransaction(txn, networkPassphrase, [ | ||
{ | ||
auth: [ | ||
new xdr.ContractAuth({ | ||
addressWithNonce: null, | ||
rootInvocation: new xdr.AuthorizedInvocation({ | ||
contractId: Buffer.alloc(32), | ||
functionName: "foo", | ||
args: [], | ||
subInvocations: [], | ||
}), | ||
signatureArgs: [], | ||
}), | ||
], | ||
footprint: emptyFootprint, | ||
}, | ||
]); | ||
|
||
expect(result.operations[0].auth.length).to.equal(1); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters