Skip to content

Commit

Permalink
Update stellar sdk version (#163)
Browse files Browse the repository at this point in the history
* fix: handle contract authorization for subinvocation

* chore: update stellar sdk to 12.2.0

* chore: bump version to 0.11.0
  • Loading branch information
fazzatti authored Aug 1, 2024
1 parent e41d145 commit 5eea266
Show file tree
Hide file tree
Showing 32 changed files with 235 additions and 245 deletions.
261 changes: 117 additions & 144 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stellar-plus",
"version": "0.10.2",
"version": "0.11.0",
"description": "beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down Expand Up @@ -61,12 +61,12 @@
"tsc-alias": "^1.8.8",
"tsconfig-paths": "^4.2.0",
"tslib": "^2.6.2",
"typescript": "^5.3.2"
"typescript": "^5.5.4"
},
"dependencies": {
"@hyperledger/cactus-test-tooling": "^2.0.0-rc.2",
"@stellar/freighter-api": "^1.7.1",
"@stellar/stellar-sdk": "^11.2.2",
"@stellar/stellar-sdk": "^12.2.0",
"axios": "^1.6.2",
"uuid": "^9.0.1"
}
Expand Down
6 changes: 3 additions & 3 deletions src/stellar-plus/asset/classic/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HorizonApi } from '@stellar/stellar-sdk/lib/horizon'
import { Horizon } from '@stellar/stellar-sdk'

import { AccountHandler } from 'stellar-plus/account/account-handler/types'
import { AssetType, AssetTypes } from 'stellar-plus/asset/types'
Expand Down Expand Up @@ -32,7 +32,7 @@ export type ClassicTokenInterfaceManagement = {
to: string
amount: number
} & TransactionInvocation
) => Promise<HorizonApi.SubmitTransactionResponse>
) => Promise<Horizon.HorizonApi.SubmitTransactionResponse>
}

export type ClassicTokenInterfaceUser = {
Expand All @@ -47,5 +47,5 @@ export type ClassicTokenInterfaceUser = {
export type ClassicUtils = {
addTrustlineAndMint: (
args: { to: string; amount: number } & TransactionInvocation
) => Promise<HorizonApi.SubmitTransactionResponse>
) => Promise<Horizon.HorizonApi.SubmitTransactionResponse>
}
4 changes: 2 additions & 2 deletions src/stellar-plus/asset/soroban-token/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ContractSpec } from '@stellar/stellar-sdk'
import { Spec } from '@stellar/stellar-sdk/contract'

export const spec = new ContractSpec([
export const spec = new Spec([
'AAAAAAAAAAAAAAAKaW5pdGlhbGl6ZQAAAAAABAAAAAAAAAAFYWRtaW4AAAAAAAATAAAAAAAAAAdkZWNpbWFsAAAAAAQAAAAAAAAABG5hbWUAAAAQAAAAAAAAAAZzeW1ib2wAAAAAABAAAAAA',
'AAAAAAAAAAAAAAAEbWludAAAAAIAAAAAAAAAAnRvAAAAAAATAAAAAAAAAAZhbW91bnQAAAAAAAsAAAAA',
'AAAAAAAAAAAAAAAJc2V0X2FkbWluAAAAAAAAAQAAAAAAAAAJbmV3X2FkbWluAAAAAAAAEwAAAAA=',
Expand Down
7 changes: 4 additions & 3 deletions src/stellar-plus/asset/soroban-token/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Address, ContractSpec } from '@stellar/stellar-sdk'
import { Address } from '@stellar/stellar-sdk'
import { Spec } from '@stellar/stellar-sdk/contract'

import { spec as defaultSpec, methods } from 'stellar-plus/asset/soroban-token/constants'
import { SorobanTokenHandlerConstructorArgs, SorobanTokenInterface } from 'stellar-plus/asset/soroban-token/types'
Expand All @@ -16,7 +17,7 @@ export class SorobanTokenHandler extends ContractEngine implements SorobanTokenI
* @args args
* @param {NetworkConfig} args.networkConfig - Network to connect to
* @param args.contractParameters - Contract parameters
* @param {ContractSpec=} args.contractParameters.spec - Contract specification
* @param {Spec=} args.contractParameters.spec - Contract specification
* @param {string=} args.contractParameters.contractId - Contract ID
* @param {Buffer=} args.wasm - Contract WASM file as Buffer
* @param {string=} args.wasmHash - Contract WASM hash identifier
Expand All @@ -34,7 +35,7 @@ export class SorobanTokenHandler extends ContractEngine implements SorobanTokenI
...args,
contractParameters: {
...args.contractParameters,
spec: args.contractParameters?.spec || (defaultSpec as ContractSpec),
spec: args.contractParameters?.spec || (defaultSpec as Spec),
},
})
}
Expand Down
5 changes: 3 additions & 2 deletions src/stellar-plus/asset/soroban-token/index.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Address, ContractSpec } from '@stellar/stellar-sdk'
import { Address } from '@stellar/stellar-sdk'
import { Spec } from '@stellar/stellar-sdk/contract'

import { SorobanTokenHandler } from 'stellar-plus/asset/soroban-token'
import { spec as DEFAULT_SPEC, methods } from 'stellar-plus/asset/soroban-token/constants'
Expand Down Expand Up @@ -44,7 +45,7 @@ describe('SorobanToken', () => {
})

it('should initialize with a custom spec', () => {
const mockedSpec = new ContractSpec(['AAAAAAAAAAAAAAAEbmFtZQAAAAAAAAABAAAAEA=='])
const mockedSpec = new Spec(['AAAAAAAAAAAAAAAEbmFtZQAAAAAAAAABAAAAEA=='])
const token = new SorobanTokenHandler({
networkConfig: NETWORK_CONFIG,
contractParameters: {
Expand Down
4 changes: 2 additions & 2 deletions src/stellar-plus/asset/soroban-token/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContractSpec } from '@stellar/stellar-sdk'
import { Spec } from '@stellar/stellar-sdk/contract'

import { AssetType } from 'stellar-plus/asset/types'
import { Options } from 'stellar-plus/core/contract-engine/types'
Expand All @@ -9,7 +9,7 @@ import { NetworkConfig, i128, u32 } from 'stellar-plus/types'
export type SorobanTokenHandlerConstructorArgs = {
networkConfig: NetworkConfig
contractParameters?: {
spec?: ContractSpec
spec?: Spec
contractId?: string
wasm?: Buffer
wasmHash?: string
Expand Down
3 changes: 1 addition & 2 deletions src/stellar-plus/asset/stellar-asset-contract/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { SorobanTokenHandler } from 'stellar-plus/asset/soroban-token'
import { SorobanTokenHandlerConstructorArgs } from 'stellar-plus/asset/soroban-token/types'
import { SACConstructorArgs, SACHandler as SACHandlerType } from 'stellar-plus/asset/stellar-asset-contract/types'
import { AssetTypes } from 'stellar-plus/asset/types'

import { TransactionInvocation } from 'stellar-plus/core/types'

export class SACHandler implements SACHandlerType {
Expand All @@ -23,7 +22,7 @@ export class SACHandler implements SACHandlerType {
* @param {string} args.code - The asset code.
* @param {string | AccountHandler} args.issuerAccount - The issuer account. Can be a public key or an account handler. If it's an account handler, it will enable management functions.
* @param contractParameters - The contract parameters.
* @param {ContractSpec=} contractParameters.spec - The contract specification object.
* @param {Spec=} contractParameters.spec - The contract specification object.
* @param {Buffer=} contractParameters.wasm - The contract wasm file as a buffer.
* @param {string=} contractParameters.wasmHash - The contract wasm hash id.
* @param {string=} contractParameters.contractId - The contract id.
Expand Down
4 changes: 2 additions & 2 deletions src/stellar-plus/asset/stellar-asset-contract/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContractSpec } from '@stellar/stellar-sdk'
import { Spec } from '@stellar/stellar-sdk/contract'

import { ClassicAssetHandler } from 'stellar-plus/asset/classic'
import { ClassicAssetHandlerConstructorArgs } from 'stellar-plus/asset/classic/types'
Expand All @@ -15,7 +15,7 @@ export type SACHandler = AssetType & {
export type SACConstructorArgs = ClassicAssetHandlerConstructorArgs & {
networkConfig: NetworkConfig
contractParameters?: {
spec?: ContractSpec
spec?: Spec
contractId?: string
wasm?: Buffer
wasmHash?: string
Expand Down
7 changes: 4 additions & 3 deletions src/stellar-plus/core/contract-engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { Buffer } from 'buffer'
import {
Address,
Contract,
ContractSpec,
Operation,
OperationOptions,
SorobanDataBuilder,
SorobanRpc as SorobanRpcNamespace,
xdr,
} from '@stellar/stellar-sdk'
import { Spec } from '@stellar/stellar-sdk/contract'

import { CEError } from 'stellar-plus/core/contract-engine/errors'
import {
Expand Down Expand Up @@ -44,7 +44,7 @@ import { ExtractInvocationOutputPlugin } from 'stellar-plus/utils/pipeline/plugi
import { ExtractWasmHashPlugin } from 'stellar-plus/utils/pipeline/plugins/soroban-get-transaction/extract-wasm-hash'

export class ContractEngine {
private spec?: ContractSpec
private spec?: Spec
private contractId?: string
private wasm?: Buffer
private wasmHash?: string
Expand All @@ -59,7 +59,7 @@ export class ContractEngine {
*
* @param {NetworkConfig} networkConfig - The network to use.
* @param contractParameters - The contract parameters.
* @param {ContractSpec} contractParameters.spec - The contract specification object.
* @param {Spec} contractParameters.spec - The contract specification object.
* @param {string=} contractParameters.contractId - The contract id.
* @param {Buffer=} contractParameters.wasm - The contract wasm file as a buffer.
* @param {string=} contractParameters.wasmHash - The contract wasm hash id.
Expand Down Expand Up @@ -370,6 +370,7 @@ export class ContractEngine {
}
} finally {
if (!isAssetAlreadyWrapped) {
// eslint-disable-next-line no-unsafe-finally
throw CEError.failedToWrapAsset(error as StellarPlusError)
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/stellar-plus/core/contract-engine/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ContractSpec, SorobanDataBuilder, Asset as StellarAsset, xdr } from '@stellar/stellar-sdk'
import { SorobanDataBuilder, Asset as StellarAsset, xdr } from '@stellar/stellar-sdk'
import { Spec } from '@stellar/stellar-sdk/contract'

import { AccountHandler } from 'stellar-plus/account'
import {
Expand All @@ -10,7 +11,7 @@ import { EnvelopeHeader, FeeBumpHeader, NetworkConfig, TransactionInvocation } f
export type ContractEngineConstructorArgs = {
networkConfig: NetworkConfig
contractParameters: {
spec?: ContractSpec
spec?: Spec
contractId?: string
wasm?: Buffer
wasmHash?: string
Expand Down
5 changes: 3 additions & 2 deletions src/stellar-plus/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContractSpec } from '@stellar/stellar-sdk'
import { Spec } from '@stellar/stellar-sdk/contract'

import { ContractEngine } from './contract-engine'
import { BuildTransactionPipeline } from './pipelines/build-transaction'
Expand All @@ -24,5 +24,6 @@ export const Core = {
SubmitTransaction: SubmitTransactionPipeline,
},
ContractEngine: ContractEngine,
ContractSpec,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
Spec,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Asset, Operation } from '@stellar/stellar-base'
import { Horizon, TransactionBuilder } from '@stellar/stellar-sdk'
import { AccountResponse } from '@stellar/stellar-sdk/lib/horizon'

import {
BuildTransactionPipelineInput as BTInput,
Expand Down Expand Up @@ -32,7 +31,7 @@ jest.mock('@stellar/stellar-sdk', () => ({

export function createMockedHorizonHandler(): jest.Mocked<HorizonHandler> {
return {
loadAccount: jest.fn().mockResolvedValue({} as AccountResponse),
loadAccount: jest.fn().mockResolvedValue({} as Horizon.AccountResponse),
server: new Horizon.Server(TestNet().horizonUrl as string),
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/stellar-plus/core/pipelines/classic-transaction/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { xdr } from '@stellar/stellar-sdk'
import { HorizonApi } from '@stellar/stellar-sdk/lib/horizon'
import { Horizon, xdr } from '@stellar/stellar-sdk'

import {
BuildTransactionPipelineOutput,
Expand Down Expand Up @@ -38,7 +37,7 @@ export type ClassicTransactionPipelineInput = {
export type ClassicTransactionPipelineOutput =
| ClassicTransactionPipelineOutputSimple
| ClassicTransactionPipelineOutputVerbose
export type ClassicTransactionPipelineOutputSimple = { response: HorizonApi.SubmitTransactionResponse }
export type ClassicTransactionPipelineOutputSimple = { response: Horizon.HorizonApi.SubmitTransactionResponse }
export type ClassicTransactionPipelineOutputVerbose = (VerboseOutput | undefined) & {
classicTransactionOutput: ClassicTransactionPipelineOutputSimple
hash?: string
Expand Down
16 changes: 16 additions & 0 deletions src/stellar-plus/core/pipelines/soroban-auth/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,25 @@ const couldntSimulateAuthorizedTransaction = (
})
}

const contractAuthNotSupported = (
contractId: string,
conveyorBeltErrorMeta: ConveyorBeltErrorMeta<SorobanAuthPipelineInput, BeltMetadata>
): StellarPlusError => {
return new StellarPlusError({
code: ErrorCodesPipelineSorobanAuth.PSA004,
message: 'Contract authorization not supported!',
source: 'PipelineSorobanAuth',
details: `Contract authorization is not supported in the current version of Stellar Plus. This contract function is verifying against a specific contract authorization from (${contractId}) which is likely expected to be accessed as a sub-invocation instead of root invocation.`,
meta: {
conveyorBeltErrorMeta,
},
})
}

export const PSAError = {
signerNotFound,
noSignersProvided,
couldntUpdateTransaction,
couldntSimulateAuthorizedTransaction,
contractAuthNotSupported,
}
19 changes: 16 additions & 3 deletions src/stellar-plus/core/pipelines/soroban-auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ export class SorobanAuthPipeline extends ConveyorBelt<
? [...additionalSignedSorobanAuth]
: []
for (const authEntry of authEntriesToSign) {
const requiredSigner = Address.account(
authEntry.credentials().address().address().accountId().ed25519()
).toString()
const requiredSigner = this.getRequiredSigner(authEntry, item, itemId)

const signer = signers.find((s) => s.getPublicKey() === requiredSigner) as AccountHandler

Expand Down Expand Up @@ -198,4 +196,19 @@ export class SorobanAuthPipeline extends ConveyorBelt<
})
: []
}

protected getRequiredSigner(
authEntry: xdr.SorobanAuthorizationEntry,
item: SorobanAuthPipelineInput,
itemId: string
): string {
if (authEntry.credentials().address().address().switch().name === 'scAddressTypeContract') {
throw PSAError.contractAuthNotSupported(
Address.contract(authEntry.credentials().address().address().contractId()).toString(),
extractConveyorBeltErrorMeta(item, this.getMeta(itemId))
)
}

return Address.account(authEntry.credentials().address().address().accountId().ed25519()).toString()
}
}
4 changes: 2 additions & 2 deletions src/stellar-plus/core/pipelines/submit-transaction/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HorizonApi } from '@stellar/stellar-sdk/lib/horizon'
import { Horizon } from '@stellar/stellar-sdk'

import { StellarPlusError } from 'stellar-plus/error'
import { ConveyorBeltErrorMeta } from 'stellar-plus/error/helpers/conveyor-belt'
Expand Down Expand Up @@ -54,7 +54,7 @@ const horizonSubmissionFailed = (
})
}
const transactionSubmittedThroughHorizonFailed = (
response: HorizonApi.SubmitTransactionResponse,
response: Horizon.HorizonApi.SubmitTransactionResponse,
conveyorBeltErrorMeta: ConveyorBeltErrorMeta<SubmitTransactionPipelineInput, BeltMetadata>,
transaction: Transaction | FeeBumpTransaction
): StellarPlusError => {
Expand Down
9 changes: 4 additions & 5 deletions src/stellar-plus/core/pipelines/submit-transaction/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FeeBumpTransaction, SorobanRpc, Transaction } from '@stellar/stellar-sdk'
import { HorizonApi } from '@stellar/stellar-sdk/lib/horizon'
import { FeeBumpTransaction, Horizon, SorobanRpc, Transaction } from '@stellar/stellar-sdk'

import {
SubmitTransactionPipelineInput,
Expand Down Expand Up @@ -38,7 +37,7 @@ export class SubmitTransactionPipeline extends ConveyorBelt<
// ===================
//
if (networkHandler instanceof HorizonHandlerClient) {
let response: HorizonApi.SubmitTransactionResponse
let response: Horizon.HorizonApi.SubmitTransactionResponse
try {
response = await this.submitTransactionThroughHorizon(transaction, networkHandler)
} catch (error) {
Expand Down Expand Up @@ -87,10 +86,10 @@ export class SubmitTransactionPipeline extends ConveyorBelt<
private async submitTransactionThroughHorizon(
transaction: Transaction | FeeBumpTransaction,
horizonHandler: HorizonHandlerClient
): Promise<HorizonApi.SubmitTransactionResponse> {
): Promise<Horizon.HorizonApi.SubmitTransactionResponse> {
const response = (await horizonHandler.server.submitTransaction(transaction, {
skipMemoRequiredCheck: true, // Not skipping memo required check causes an error when submitting fee bump transactions
})) as HorizonApi.SubmitTransactionResponse
})) as Horizon.HorizonApi.SubmitTransactionResponse
return response
}

Expand Down
5 changes: 2 additions & 3 deletions src/stellar-plus/core/pipelines/submit-transaction/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FeeBumpTransaction, SorobanRpc, Transaction } from '@stellar/stellar-sdk'
import { HorizonApi } from '@stellar/stellar-sdk/lib/horizon'
import { FeeBumpTransaction, Horizon, SorobanRpc, Transaction } from '@stellar/stellar-sdk'

import { HorizonHandler } from 'stellar-plus'
import { RpcHandler } from 'stellar-plus/rpc/types'
Expand All @@ -11,7 +10,7 @@ export type SubmitTransactionPipelineInput = {
}

export type SubmitTransactionPipelineOutput = {
response: HorizonApi.SubmitTransactionResponse | SorobanRpc.Api.SendTransactionResponse
response: Horizon.HorizonApi.SubmitTransactionResponse | SorobanRpc.Api.SendTransactionResponse
}

export enum SubmitTransactionPipelineType {
Expand Down
7 changes: 7 additions & 0 deletions src/stellar-plus/core/spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// import { contract } from '@stellar/stellar-sdk'

// const SpecClass = [contract.Spec]
// export type Spec = (typeof SpecClass)[0]

// // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
// export default { SpecClass: SpecClass[0] }
Loading

0 comments on commit 5eea266

Please sign in to comment.