Skip to content

Commit

Permalink
Fix detection, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed Nov 13, 2024
1 parent bfd5aa9 commit 6160040
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
15 changes: 8 additions & 7 deletions src/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ import { nativeToScVal } from './scval';
* or a function which takes a {@link xdr.HashIdPreimageSorobanAuthorization}
* input payload and returns EITHER
*
* (a) an object containing a `signature` in bytes and a `signer` in bytes
* (the public key that created this signature), or
* (b) just the signature of the hash of the raw payload bytes (where the
* signing key should correspond to the address in the `entry`).
* (a) an object containing a `signature` of the hash of the raw payload bytes
* as a Buffer-like and a `publicKey` string representing who just
* created this signature, or
* (b) just the naked signature of the hash of the raw payload bytes (where
* the signing key is implied to be the address in the `entry`).
*
* The latter option (b) is JUST for backwards compatibility and will be
* removed in the future.
Expand All @@ -67,8 +68,8 @@ import { nativeToScVal } from './scval';
* {@link Operation.invokeHostFunction}
*
* @note If using the `SigningCallback` variation, the signer is assumed to be
* the entry's credential address. If you need a different key to sign the
* entry, you will need to use different method (e.g., fork this code).
* the entry's credential address unless you use the variant that returns
* the object.
*
* @see authorizeInvocation
* @example
Expand Down Expand Up @@ -147,7 +148,7 @@ export async function authorizeEntry(
let publicKey;
if (typeof signer === 'function') {
const sigResult = await signer(preimage);
if (typeof sigResult === 'object') {
if (sigResult?.signature) {
signature = Buffer.from(sigResult.signature);
publicKey = sigResult.publicKey;
} else {
Expand Down
15 changes: 11 additions & 4 deletions test/unit/auth_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ describe('building authorization entries', function () {

[
[kp, 'Keypair'],
[(preimage) => kp.sign(StellarBase.hash(preimage.toXDR())), 'callback']
[(preimage) => kp.sign(StellarBase.hash(preimage.toXDR())), 'callback'],
[
(preimage) => {
return {
signature: kp.sign(StellarBase.hash(preimage.toXDR())),
publicKey: kp.publicKey()
};
},
'callback w/ obj'
]
].forEach(([signer, methodName]) => {
it(`signs the entry correctly (${methodName})`, function (done) {
StellarBase.authorizeEntry(authEntry, signer, 10)
Expand Down Expand Up @@ -87,9 +96,7 @@ describe('building authorization entries', function () {

it('can build from scratch', function (done) {
StellarBase.authorizeInvocation(kp, 10, authEntry.rootInvocation())
.then((signedEntry) => {
done();
})
.then((signedEntry) => done())
.catch((err) => done(err));
});
});

0 comments on commit 6160040

Please sign in to comment.