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

chore: package/statement: Add function prepareExtrinsic() to return extrinsic and change cord version to latest #182

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions packages/statement/src/Statement.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import type {
SchemaUri,
IStatementEntry,
HexString,
SubmittableExtrinsic,
} from '@cord.network/types'
import * as Did from '@cord.network/did'
import {
Expand Down Expand Up @@ -209,6 +210,58 @@ export async function dispatchRegisterToChain(
authorizationUri: AuthorizationUri,
signCallback: SignExtrinsicCallback
): Promise<StatementUri> {
try {
const tx = await prepareExtrinsic(
stmtEntry,
creatorUri,
authorAccount,
authorizationUri,
signCallback
)

await Chain.signAndSubmitTx(tx, authorAccount)

return stmtEntry.elementUri
} catch (error) {
throw new SDKErrors.CordDispatchError(
`Error dispatching to chain: "${error}".`
)
}
}

/**
* This function prepares and returns a SubmittableExtrinsic for registering a statement on
* the blockchain.
* @param {IStatementEntry} stmtEntry - The `stmtEntry` parameter is an object of type
* `IStatementEntry`, which contains information about a statement entry.
*
* @param {DidUri} creatorUri - The `creatorUri` parameter is a URI that identifies the creator of the
* statement entry. It is used to authorize the transaction when preparing the extrinsic.
*
* @param {CordKeyringPair} authorAccount - The `authorAccount` parameter in the `prepareExtrinsic`
* function is of type `CordKeyringPair`. It represents the keyring pair used for signing the extrinsic
* transaction. This keyring pair contains the cryptographic key pair necessary for signing and
* verifying messages in the Corda network.
*
* @param {AuthorizationUri} authorizationUri - The `authorizationUri` parameter in the
* `prepareExtrinsic` function is a URI that represents the authorization needed for the statement
* entry. It is used to identify and retrieve the authorization details required for registering the
* statement on the chain.
*
* @param {SignExtrinsicCallback} signCallback - The `signCallback` parameter in the `prepareExtrinsic`
* function is a callback function that is used to sign the extrinsic transaction before it is
* submitted to the blockchain. This function typically takes care of the signing process using the
* private key of the account that is authorizing the transaction. It is
*
* @returns A `SubmittableExtrinsic` is being returned from the `prepareExtrinsic` function.
*/
export async function prepareExtrinsic(
stmtEntry: IStatementEntry,
creatorUri: DidUri,
authorAccount: CordKeyringPair,
authorizationUri: AuthorizationUri,
signCallback: SignExtrinsicCallback
): Promise<SubmittableExtrinsic> {
try {
const api = ConfigService.get('api')
const authorizationId: AuthorizationId = uriToIdentifier(authorizationUri)
Expand All @@ -218,6 +271,7 @@ export async function dispatchRegisterToChain(
: undefined

const exists = await isStatementStored(stmtEntry.digest, stmtEntry.spaceUri)

if (exists) {
throw new SDKErrors.DuplicateStatementError(
`The statement is already anchored in the chain\nIdentifier: ${stmtEntry.elementUri}`
Expand All @@ -235,12 +289,10 @@ export async function dispatchRegisterToChain(
authorAccount.address
)

await Chain.signAndSubmitTx(extrinsic, authorAccount)

return stmtEntry.elementUri
return extrinsic
} catch (error) {
throw new SDKErrors.CordDispatchError(
`Error dispatching to chain: "${error}".`
`Error returning extrinsic: "${error}".`
)
}
}
Expand Down