function onChanOpenInit(
order: ChannelOrder,
connectionHops: [Identifier],
portIdentifier: Identifier,
channelIdentifier: Identifier,
counterpartyPortIdentifier: Identifier,
counterpartyChannelIdentifier: Identifier,
version: string): (version: string, err: Error) {
if version != "" {
// try to unmarshal JSON-encoded version string and pass
// the app-specific version to app callback.
// otherwise, pass version directly to app callback.
metadata, err = UnmarshalJSON(version)
if err != nil {
// call the underlying application's onChanOpenInit callback
return app.onChanOpenInit(
order,
connectionHops,
portIdentifier,
channelIdentifier,
counterpartyPortIdentifier,
counterpartyChannelIdentifier,
version,
)
}
} else {
metadata = {
// set middleware version to default value
middlewareVersion: defaultMiddlewareVersion,
// allow application to return its default version
appVersion: "",
}
}
doCustomLogic()
// call the underlying application's OnChanOpenInit callback.
// if the version string is empty, OnChanOpenInit is expected to return
// a default version string representing the version(s) it supports
appVersion, err = app.OnChanOpenInit(
order,
connectionHops,
portIdentifier,
channelIdentifier,
counterpartyPortIdentifier,
counterpartyChannelIdentifier,
metadata.appVersion, // note we only pass app version here
)
abortTransactionUnless(err != nil)
// a new version string is constructed with the app version returned
// by the underlying application, in case it is different than the
// one passed by the caller
metadata = {
// note this should have a different field name specific to middleware
middlewareVersion: metadata.middlewareVersion,
appVersion: appVersion,
}
return MarshallJSON(metadata), nil
}
function onChanOpenTry(
order: ChannelOrder,
connectionHops: [Identifier],
portIdentifier: Identifier,
channelIdentifier: Identifier,
counterpartyPortIdentifier: Identifier,
counterpartyChannelIdentifier: Identifier,
counterpartyVersion: string): (version: string, err: Error) {
// try to unmarshal JSON-encoded version string and pass
// the app-specific version to app callback.
// otherwise, pass version directly to app callback.
cpMetadata, err = UnmarshalJSON(counterpartyVersion)
if err != nil {
// call the underlying application's OnChanOpenTry callback
return app.onChanOpenTry(
order,
connectionHops,
portIdentifier,
channelIdentifier,
counterpartyPortIdentifier,
counterpartyChannelIdentifier,
counterpartyVersion,
)
}
// select mutually compatible middleware version
if !isCompatible(cpMetadata.middlewareVersion) {
return "", error
}
middlewareVersion = selectMiddlewareVersion(cpMetadata.middlewareVersion)
doCustomLogic()
// call the underlying application's OnChanOpenTry callback
appVersion, err = app.OnChanOpenTry(
order,
connectionHops,
portIdentifier,
channelIdentifier,
counterpartyPortIdentifier,
counterpartyChannelIdentifier,
cpMetadata.appVersion, // note we only pass counterparty app version here
)
abortTransactionUnless(err != nil)
// a new version string is constructed with the final middleware version
// that is selected and the app version returned by the underlying
// application (which may be different than the one passed by the caller)
metadata = {
// note this should have a different field name specific to middleware
middlewareVersion: middlewareVersion,
appVersion: appVersion,
}
return MarshalJSON(metadata), nil
}
function onChanOpenAck(
portIdentifier: Identifier,
channelIdentifier: Identifier,
counterpartyChannelIdentifier: Identifier,
counterpartyVersion: string) {
cpMetadata, err = UnmarshalJSON(counterpartyVersion)
if err != nil {
// call the underlying application's OnChanOpenAck callback
return app.onChanOpenAck(
portIdentifier,
channelIdentifier,
counterpartyChannelIdentifier,
counterpartyVersion,
)
}
if !isSupported(cpMetadata.middlewareVersion) {
return error
}
doCustomLogic()
// call the underlying application's OnChanOpenAck callback
return app.onChanOpenAck(
portIdentifier,
channelIdentifier,
counterpartyChannelIdentifier,
cpMetadata.appVersion,
)
}
function onChanOpenConfirm(
portIdentifier: Identifier,
channelIdentifier: Identifier) {
doCustomLogic()
app.OnChanOpenConfirm(portIdentifier, channelIdentifier)
}