@stellar-broker/client 0.6.14 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,6 @@
1
- import {hash, nativeToScVal, StrKey, TransactionBuilder, xdr} from '@stellar/stellar-sdk'
1
+ import {authorizeEntry, TransactionBuilder} from '@stellar/stellar-sdk'
2
2
  import errors from './errors.js'
3
3
 
4
- const networkIdCache = {}
5
-
6
4
  /**
7
5
  * @param {StellarBrokerClient} client
8
6
  * @param {{xdr: string, hash: string, networkFee: string, [confirmed]: boolean}} txRequest
@@ -12,7 +10,7 @@ export async function processTxRequest(client, txRequest) {
12
10
  //parse incoming transaction
13
11
  let tx
14
12
  try {
15
- tx = TransactionBuilder.fromXDR(txRequest.xdr, client.network)
13
+ tx = TransactionBuilder.fromXdr(txRequest.xdr, client.network)
16
14
  } catch (e) {
17
15
  throw errors.invalidSwapTx()
18
16
  }
@@ -27,7 +25,7 @@ export async function processTxRequest(client, txRequest) {
27
25
  //sign auth
28
26
  await authorizeInvocation(client, tx)
29
27
  tx = await signTx(client, tx)
30
- return tx.toXDR()//wait for the signed tx from the server to wrap it with fee bump tx
28
+ return tx.toXdr()//wait for the signed tx from the server to wrap it with fee bump tx
31
29
  }
32
30
  } else {
33
31
  //sign transaction
@@ -38,50 +36,26 @@ export async function processTxRequest(client, txRequest) {
38
36
  //sign fee bump wrapper tx
39
37
  wrapped = await signTx(client, wrapped)
40
38
  //respond with signed transaction
41
- return wrapped.toXDR()
39
+ return wrapped.toXdr()
42
40
  }
43
41
 
44
42
  /**
43
+ * Sign trader's Soroban authorization entry
45
44
  * @param {StellarBrokerClient} client
46
45
  * @param {TransactionI} tx
47
- * @return {Promise<TransactionI>}
46
+ * @return {Promise}
48
47
  * @private
49
48
  */
50
49
  async function authorizeInvocation(client, tx) {
51
- //from https://github.com/stellar/js-stellar-base/blob/e3d6fc3351e7d242b374c7c6057668366364a279/src/auth.js#L97
52
- const auth = tx.operations[0].auth[0]
53
- /** @type {xdr.SorobanAddressCredentials} */
54
- const addrAuth = auth.credentials().address()
55
- addrAuth.signatureExpirationLedger(tx.ledgerBounds.maxLedger + 1)
56
- let networkId = networkIdCache[tx.networkPassphrase]
57
- if (!networkId) {
58
- networkId = networkIdCache[tx.networkPassphrase] = hash(Buffer.from(tx.networkPassphrase))
59
- }
60
- const preimage = xdr.HashIdPreimage.envelopeTypeSorobanAuthorization(
61
- new xdr.HashIdPreimageSorobanAuthorization({
62
- networkId,
63
- nonce: addrAuth.nonce(),
64
- invocation: auth.rootInvocation(),
65
- signatureExpirationLedger: addrAuth.signatureExpirationLedger()
66
- })
67
- )
68
- const payload = hash(preimage.toXDR())
69
- const signature = await client.authorization.authorize(payload)
70
- const publicKey = StrKey.encodeEd25519PublicKey(addrAuth.address().accountId().value())
71
- const sigScVal = nativeToScVal(
72
- {
73
- public_key: StrKey.decodeEd25519PublicKey(publicKey),
74
- signature
75
- },
76
- {
77
- type: {
78
- public_key: ['symbol', null],
79
- signature: ['symbol', null]
80
- }
81
- }
82
- )
83
-
84
- addrAuth.signature(xdr.ScVal.scvVec([sigScVal]))
50
+ //the operation shares its auth array with the transaction envelope, so the signed entry replaces the original one
51
+ const {auth} = tx.operations[0]
52
+ //SDK builds the signature payload for both legacy (v1) and CAP-71 (v2) address credentials,
53
+ //and verifies that the signature matches the trader account
54
+ auth[0] = await authorizeEntry(auth[0],
55
+ (preimage, payload) => client.authorization.authorize(payload),
56
+ tx.ledgerBounds.maxLedger + 1,
57
+ tx.networkPassphrase,
58
+ client.trader)
85
59
  }
86
60
 
87
61
  /**
@@ -94,7 +68,7 @@ function signTx(client, tx) {
94
68
  return client.authorization.authorize(tx)
95
69
  .then(tx => {
96
70
  if (typeof tx === 'string') { //normalize response
97
- tx = TransactionBuilder.fromXDR(tx, client.network)
71
+ tx = TransactionBuilder.fromXdr(tx, client.network)
98
72
  }
99
73
  if (!tx || tx.signatures?.length < 1)
100
74
  throw new Error(`Transaction was not signed`)