Skip to content
Merged
Show file tree
Hide file tree
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
32 changes: 32 additions & 0 deletions packages/connect-examples/expo-example/src/data/stellar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,38 @@ const api: PlaygroundProps[] = [
},
},
},
{
title: 'Sign Soroban Transaction (swap_chained)',
value: {
path: "m/44'/148'/0'",
networkPassphrase: 'Public Global Stellar Network ; September 2015',
transaction: {
source: 'GD437PAXIIYQRPCHNXM5I2O7O6D27JDAQFZ2GQ4NHFNP4NBOMU6NSFSS',
fee: 256588,
sequence: '259328278496542793',
timebounds: {
minTime: 0,
maxTime: 0,
},
memo: {
type: 0,
},
operations: [
{
type: 'invokeHostFunctionOneKey',
contract: 'CBQDHNBFBZYE4MKPWBSJOPIYLW4SFSXAXUTSXJN76GNKYVYPCKWC6QUK',
functionName: 'swap_chained',
callArgsXDRHex:
'00000005000000120000000000000000f9bfbc17423108bc476dd9d469df7787afa4608173a3438d395afe342e653cd9000000100000000100000004000000100000000100000003000000100000000100000002000000120000000128852f68c198488e346fb1212ed5029053761cc24f453392e35398201b0825e700000012000000016a38cfbcff1c1a12e5eb121dd0d0e88df128c201face46bd684434d039ebae740000000d00000020b2e02fcfca6c96f8ad5cbd84e7784a777b36d9c96a2459402c4f458462aab7f000000012000000016a38cfbcff1c1a12e5eb121dd0d0e88df128c201face46bd684434d039ebae7400000010000000010000000300000010000000010000000200000012000000016a38cfbcff1c1a12e5eb121dd0d0e88df128c201face46bd684434d039ebae740000001200000001e6a7d9eb7523006a469aa7483ad1107247443c0d82e62763de670848c4e97c900000000d00000020748b175fb23d4f2a75f5c8ccb5cccac3785302097f08c6c9cc94cfc30ec91d6f0000001200000001e6a7d9eb7523006a469aa7483ad1107247443c0d82e62763de670848c4e97c9000000010000000010000000300000010000000010000000300000012000000016a38cfbcff1c1a12e5eb121dd0d0e88df128c201face46bd684434d039ebae740000001200000001adefce59aee52968f76061d494c2525b75659fa4296a65f499ef29e56477e4960000001200000001e6a7d9eb7523006a469aa7483ad1107247443c0d82e62763de670848c4e97c900000000d0000002011f2921a212b9b1f548c3b2eccd0081b4324ffe1c6e208d1da33a5213de84df30000001200000001adefce59aee52968f76061d494c2525b75659fa4296a65f499ef29e56477e496000000100000000100000003000000100000000100000002000000120000000125b4fcd859aec2fa6348438c489b3c3c10c98b6d21be4fd3cb30cb68953ef9770000001200000001adefce59aee52968f76061d494c2525b75659fa4296a65f499ef29e56477e4960000000d00000020b2e02fcfca6c96f8ad5cbd84e7784a777b36d9c96a2459402c4f458462aab7f0000000120000000125b4fcd859aec2fa6348438c489b3c3c10c98b6d21be4fd3cb30cb68953ef977000000120000000128852f68c198488e346fb1212ed5029053761cc24f453392e35398201b0825e70000000900000000000000000000000020c855800000000900000000000000000000000000120961',
sorobanAuthXDRHex:
'000000010000000000000000000000016033b4250e704e314fb064973d185db922cae0bd272ba5bff19aac570f12ac2f0000000c737761705f636861696e656400000005000000120000000000000000f9bfbc17423108bc476dd9d469df7787afa4608173a3438d395afe342e653cd9000000100000000100000004000000100000000100000003000000100000000100000002000000120000000128852f68c198488e346fb1212ed5029053761cc24f453392e35398201b0825e700000012000000016a38cfbcff1c1a12e5eb121dd0d0e88df128c201face46bd684434d039ebae740000000d00000020b2e02fcfca6c96f8ad5cbd84e7784a777b36d9c96a2459402c4f458462aab7f000000012000000016a38cfbcff1c1a12e5eb121dd0d0e88df128c201face46bd684434d039ebae7400000010000000010000000300000010000000010000000200000012000000016a38cfbcff1c1a12e5eb121dd0d0e88df128c201face46bd684434d039ebae740000001200000001e6a7d9eb7523006a469aa7483ad1107247443c0d82e62763de670848c4e97c900000000d00000020748b175fb23d4f2a75f5c8ccb5cccac3785302097f08c6c9cc94cfc30ec91d6f0000001200000001e6a7d9eb7523006a469aa7483ad1107247443c0d82e62763de670848c4e97c9000000010000000010000000300000010000000010000000300000012000000016a38cfbcff1c1a12e5eb121dd0d0e88df128c201face46bd684434d039ebae740000001200000001adefce59aee52968f76061d494c2525b75659fa4296a65f499ef29e56477e4960000001200000001e6a7d9eb7523006a469aa7483ad1107247443c0d82e62763de670848c4e97c900000000d0000002011f2921a212b9b1f548c3b2eccd0081b4324ffe1c6e208d1da33a5213de84df30000001200000001adefce59aee52968f76061d494c2525b75659fa4296a65f499ef29e56477e496000000100000000100000003000000100000000100000002000000120000000125b4fcd859aec2fa6348438c489b3c3c10c98b6d21be4fd3cb30cb68953ef9770000001200000001adefce59aee52968f76061d494c2525b75659fa4296a65f499ef29e56477e4960000000d00000020b2e02fcfca6c96f8ad5cbd84e7784a777b36d9c96a2459402c4f458462aab7f0000000120000000125b4fcd859aec2fa6348438c489b3c3c10c98b6d21be4fd3cb30cb68953ef977000000120000000128852f68c198488e346fb1212ed5029053761cc24f453392e35398201b0825e70000000900000000000000000000000020c85580000000090000000000000000000000000012096100000001000000000000000128852f68c198488e346fb1212ed5029053761cc24f453392e35398201b0825e7000000087472616e7366657200000003000000120000000000000000f9bfbc17423108bc476dd9d469df7787afa4608173a3438d395afe342e653cd900000012000000016033b4250e704e314fb064973d185db922cae0bd272ba5bff19aac570f12ac2f0000000a00000000000000000000000020c8558000000000',
},
],
sorobanDataXDR:
'000000000000000f000000060000000125b4fcd859aec2fa6348438c489b3c3c10c98b6d21be4fd3cb30cb68953ef9770000001400000001000000060000000128852f68c198488e346fb1212ed5029053761cc24f453392e35398201b0825e7000000140000000100000006000000016033b4250e704e314fb064973d185db922cae0bd272ba5bff19aac570f12ac2f0000001000000001000000020000000f0000000e546f6b656e73536574506f6f6c7300000000000d000000203774e40f32ad0ed142de9f65e1596b610c75abcc845e493465b3c7a7211d01080000000100000006000000016033b4250e704e314fb064973d185db922cae0bd272ba5bff19aac570f12ac2f0000001000000001000000020000000f0000000e546f6b656e73536574506f6f6c7300000000000d0000002063ad1a844f8d16541d28d17361c577fe7de188171267ae6dfc990a444ea7309f0000000100000006000000016033b4250e704e314fb064973d185db922cae0bd272ba5bff19aac570f12ac2f0000001000000001000000020000000f0000000e546f6b656e73536574506f6f6c7300000000000d00000020bde1c77fd7f144a64f6c8900c805a2caf855818d36042c81050b87eb97904c730000000100000006000000016033b4250e704e314fb064973d185db922cae0bd272ba5bff19aac570f12ac2f0000001000000001000000020000000f0000000e546f6b656e73536574506f6f6c7300000000000d00000020fc100506bc78c7f63149462ade6ccae48f1e9fcc77a9d78002b883be46dc4e220000000100000006000000016033b4250e704e314fb064973d185db922cae0bd272ba5bff19aac570f12ac2f000000140000000100000006000000016a38cfbcff1c1a12e5eb121dd0d0e88df128c201face46bd684434d039ebae740000001400000001000000060000000180176910c0ed131a1c1e21fd8abbc94618e664834634b0b3fa72eef04b97238400000014000000010000000600000001adefce59aee52968f76061d494c2525b75659fa4296a65f499ef29e56477e49600000014000000010000000600000001e6a7d9eb7523006a469aa7483ad1107247443c0d82e62763de670848c4e97c900000001400000001000000073c030c6e93ac973cf26e39947176c85a2bf61210e9c1f98ce638b963da948c96000000073da9edcabd7491b2245920551bf7cce020c70114e219216b948ae3dd3af2e49c000000078844a760cf16788117b2a5a91d736794b3869c302aee47f8fbbcd0cc1a1096fd00000007baabbdd1d1816be62751880621a323168525f839442de5f4c1ce1c1a0ebf42b1000000170000000000000000f9bfbc17423108bc476dd9d469df7787afa4608173a3438d395afe342e653cd90000000100000000f9bfbc17423108bc476dd9d469df7787afa4608173a3438d395afe342e653cd90000000141515541000000005b942e53ac33c8fd0a80cc7c1b1a85d7d838a9c41977aad18b3af057f8e33df0000000060000000125b4fcd859aec2fa6348438c489b3c3c10c98b6d21be4fd3cb30cb68953ef9770000001000000001000000020000000f0000000742616c616e63650000000012000000013cfa2561c2b2aa114bca58a0e56453f4e99c0d191d6914bce1672c2d507a102100000001000000060000000125b4fcd859aec2fa6348438c489b3c3c10c98b6d21be4fd3cb30cb68953ef9770000001000000001000000020000000f0000000742616c616e63650000000012000000016033b4250e704e314fb064973d185db922cae0bd272ba5bff19aac570f12ac2f00000001000000060000000128852f68c198488e346fb1212ed5029053761cc24f453392e35398201b0825e70000001000000001000000020000000f0000000742616c616e63650000000012000000016033b4250e704e314fb064973d185db922cae0bd272ba5bff19aac570f12ac2f00000001000000060000000128852f68c198488e346fb1212ed5029053761cc24f453392e35398201b0825e70000001000000001000000020000000f0000000742616c616e6365000000001200000001a0da865d24da0436102004f23f4411b6e8835fa9b42bba58af5fd20d9c4b491f0000000100000006000000013cfa2561c2b2aa114bca58a0e56453f4e99c0d191d6914bce1672c2d507a1021000000140000000100000006000000016a38cfbcff1c1a12e5eb121dd0d0e88df128c201face46bd684434d039ebae740000001000000001000000020000000f0000000742616c616e63650000000012000000016033b4250e704e314fb064973d185db922cae0bd272ba5bff19aac570f12ac2f0000000100000006000000016a38cfbcff1c1a12e5eb121dd0d0e88df128c201face46bd684434d039ebae740000001000000001000000020000000f0000000742616c616e6365000000001200000001a0da865d24da0436102004f23f4411b6e8835fa9b42bba58af5fd20d9c4b491f0000000100000006000000016a38cfbcff1c1a12e5eb121dd0d0e88df128c201face46bd684434d039ebae740000001000000001000000020000000f0000000742616c616e6365000000001200000001e55b1f69f0b854bbc2e889d597952bd15ed96c6c20a936c9bb54b736eaebc19e00000001000000060000000180176910c0ed131a1c1e21fd8abbc94618e664834634b0b3fa72eef04b9723840000001000000001000000020000000f00000008506f6f6c4461746100000012000000013cfa2561c2b2aa114bca58a0e56453f4e99c0d191d6914bce1672c2d507a102100000001000000060000000180176910c0ed131a1c1e21fd8abbc94618e664834634b0b3fa72eef04b9723840000001000000001000000020000000f00000008506f6f6c446174610000001200000001a0da865d24da0436102004f23f4411b6e8835fa9b42bba58af5fd20d9c4b491f00000001000000060000000180176910c0ed131a1c1e21fd8abbc94618e664834634b0b3fa72eef04b9723840000001000000001000000020000000f00000008506f6f6c446174610000001200000001a1e95004b7224bcee8ee57f2ea8dbcf33801d1821e2136d0b4f7233716cce63500000001000000060000000180176910c0ed131a1c1e21fd8abbc94618e664834634b0b3fa72eef04b9723840000001000000001000000020000000f00000008506f6f6c446174610000001200000001e55b1f69f0b854bbc2e889d597952bd15ed96c6c20a936c9bb54b736eaebc19e000000010000000600000001a0da865d24da0436102004f23f4411b6e8835fa9b42bba58af5fd20d9c4b491f00000014000000010000000600000001a1e95004b7224bcee8ee57f2ea8dbcf33801d1821e2136d0b4f7233716cce63500000014000000010000000600000001adefce59aee52968f76061d494c2525b75659fa4296a65f499ef29e56477e4960000001000000001000000020000000f0000000742616c616e63650000000012000000013cfa2561c2b2aa114bca58a0e56453f4e99c0d191d6914bce1672c2d507a1021000000010000000600000001adefce59aee52968f76061d494c2525b75659fa4296a65f499ef29e56477e4960000001000000001000000020000000f0000000742616c616e63650000000012000000016033b4250e704e314fb064973d185db922cae0bd272ba5bff19aac570f12ac2f000000010000000600000001adefce59aee52968f76061d494c2525b75659fa4296a65f499ef29e56477e4960000001000000001000000020000000f0000000742616c616e6365000000001200000001a1e95004b7224bcee8ee57f2ea8dbcf33801d1821e2136d0b4f7233716cce635000000010000000600000001e55b1f69f0b854bbc2e889d597952bd15ed96c6c20a936c9bb54b736eaebc19e00000014000000010000000600000001e6a7d9eb7523006a469aa7483ad1107247443c0d82e62763de670848c4e97c900000001000000001000000020000000f0000000742616c616e63650000000012000000016033b4250e704e314fb064973d185db922cae0bd272ba5bff19aac570f12ac2f000000010000000600000001e6a7d9eb7523006a469aa7483ad1107247443c0d82e62763de670848c4e97c900000001000000001000000020000000f0000000742616c616e6365000000001200000001a1e95004b7224bcee8ee57f2ea8dbcf33801d1821e2136d0b4f7233716cce635000000010000000600000001e6a7d9eb7523006a469aa7483ad1107247443c0d82e62763de670848c4e97c900000001000000001000000020000000f0000000742616c616e6365000000001200000001e55b1f69f0b854bbc2e889d597952bd15ed96c6c20a936c9bb54b736eaebc19e000000010181c06b00000104000036d800000000000263ac',
},
},
},
],
},
];
Expand Down
176 changes: 165 additions & 11 deletions packages/core/src/api/stellar/StellarSignTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,22 @@ import type {
} from '@onekeyfe/hd-transport';
import type { StellarOperation, StellarSignTransactionParams } from '../../types';

// Firmware accepts up to 1024 bytes per chunk; 1 byte = 2 hex chars
const SOROBAN_CHUNK_BYTES = 1024;
const SOROBAN_CHUNK_HEX_CHARS = SOROBAN_CHUNK_BYTES * 2;

export default class StellarSignTransaction extends BaseMethod<HardwareStellarSignTx> {
operations: any[] = [];

private sorobanState?: {
callArgs: string;
callArgsSent: number;
auth: string;
authSent: number;
ext: string;
extSent: number;
};

parseOperation = (op: StellarOperation) => {
switch (op.type) {
case 'createAccount':
Expand Down Expand Up @@ -144,6 +157,59 @@ export default class StellarSignTransaction extends BaseMethod<HardwareStellarSi
source_account: op.source,
bump_to: op.bumpTo,
};

case 'invokeHostFunctionOneKey': {
const callArgs = op.callArgsXDRHex ?? '';
const auth = op.sorobanAuthXDRHex ?? '';

if (!this.sorobanState) {
throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'sorobanState not initialized');
}
this.sorobanState.callArgs = callArgs;
this.sorobanState.callArgsSent = Math.min(callArgs.length, SOROBAN_CHUNK_HEX_CHARS);
this.sorobanState.auth = auth;
this.sorobanState.authSent = Math.min(auth.length, SOROBAN_CHUNK_HEX_CHARS);

return {
type: 'StellarInvokeHostFunctionOp',
source_account: op.source,
contract_address: op.contract,
function_name: op.functionName,
call_args_xdr_size: callArgs.length / 2,
call_args_xdr_initial_chunk: callArgs.slice(0, SOROBAN_CHUNK_HEX_CHARS),
soroban_auth_xdr_size: auth.length / 2,
soroban_auth_xdr_initial_chunk: auth.slice(0, SOROBAN_CHUNK_HEX_CHARS),
};
}

case 'pathPaymentStrictReceive':
validateParams(op, [{ name: 'sendMax', type: 'bigNumber', required: true }]);
validateParams(op, [{ name: 'destAmount', type: 'bigNumber', required: true }]);
return {
type: 'StellarPathPaymentStrictReceiveOp',
source_account: op.source,
send_asset: op.sendAsset,
send_max: op.sendMax,
destination_account: op.destination,
destination_asset: op.destAsset,
destination_amount: op.destAmount,
paths: op.path,
};

case 'pathPaymentStrictSend':
validateParams(op, [{ name: 'sendAmount', type: 'bigNumber', required: true }]);
validateParams(op, [{ name: 'destMin', type: 'bigNumber', required: true }]);
return {
type: 'StellarPathPaymentStrictSendOp',
source_account: op.source,
send_asset: op.sendAsset,
send_amount: op.sendAmount,
destination_account: op.destination,
destination_asset: op.destAsset,
destination_min: op.destMin,
paths: op.path,
};

default:
return {};
}
Expand Down Expand Up @@ -171,6 +237,31 @@ export default class StellarSignTransaction extends BaseMethod<HardwareStellarSi
// init params
const addressN = validatePath(this.payload.path, 3);

const isSoroban = transaction.operations.some(op => op.type === 'invokeHostFunctionOneKey');

if (isSoroban) {
if (transaction.operations.length !== 1) {
throw ERRORS.TypedError(
HardwareErrorCode.CallMethodInvalidParameter,
'Soroban transactions must contain exactly one operation'
);
}
if (!transaction.sorobanDataXDR) {
throw ERRORS.TypedError(
HardwareErrorCode.CallMethodInvalidParameter,
'sorobanDataXDR is required for Soroban transactions'
);
}
this.sorobanState = {
callArgs: '',
callArgsSent: 0,
auth: '',
authSent: 0,
ext: transaction.sorobanDataXDR,
extSent: 0,
};
}

this.params = {
address_n: addressN,
network_passphrase: networkPassphrase,
Expand All @@ -181,6 +272,7 @@ export default class StellarSignTransaction extends BaseMethod<HardwareStellarSi
memo_type: StellarMemoType.NONE,
timebounds_start: transaction.timebounds.minTime,
timebounds_end: transaction.timebounds.maxTime,
...(this.sorobanState ? { soroban_data_size: this.sorobanState.ext.length / 2 } : {}),
};

if (transaction.memo) {
Expand All @@ -198,23 +290,85 @@ export default class StellarSignTransaction extends BaseMethod<HardwareStellarSi
});
}

processTxRequest = async (operations: any, index: number): Promise<StellarSignedTx> => {
const isLastOp = index + 1 >= operations.length;
const { type, ...op } = operations[index];
processTxRequest = async (
response: { type: string; message: any },
operations: any[],
index: number
): Promise<StellarSignedTx> => {
switch (response.type) {
case 'StellarSignedTx':
return response.message;

if (isLastOp) {
const response = await this.device.commands.typedCall(type, 'StellarSignedTx', op);
return response.message;
}
case 'StellarSorobanDataRequest': {
if (!this.sorobanState) {
throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'sorobanState not initialized');
}

await this.device.commands.typedCall(type, 'StellarTxOpRequest', op);
const reqType = response.message.type as string;
// data_length 是字节数,转为 hex 字符数
const hexLen = (response.message.data_length as number) * 2;
let chunk: string;

switch (reqType) {
// CALL: invoke contract call_args
case 'CALL': {
const { callArgs, callArgsSent } = this.sorobanState;
chunk = callArgs.slice(callArgsSent, callArgsSent + hexLen);
this.sorobanState.callArgsSent += chunk.length;
break;
}
// AUTH: soroban authorization entries
case 'AUTH': {
const { auth, authSent } = this.sorobanState;
chunk = auth.slice(authSent, authSent + hexLen);
this.sorobanState.authSent += chunk.length;
break;
}
// EXT: soroban transaction extension data
case 'EXT': {
const { ext, extSent } = this.sorobanState;
chunk = ext.slice(extSent, extSent + hexLen);
this.sorobanState.extSent += chunk.length;
break;
}
default:
throw ERRORS.TypedError(
HardwareErrorCode.RuntimeError,
`unknown soroban request type: ${reqType}`
);
}

const sorobanRes = await this.device.commands.typedCall(
'StellarSorobanDataAck',
['StellarSorobanDataRequest', 'StellarSignedTx'],
{ data_xdr: chunk }
);
return this.processTxRequest(sorobanRes, operations, index);
}

return this.processTxRequest(operations, index + 1);
case 'StellarTxOpRequest': {
const { type, ...op } = operations[index];
const nextRes = await this.device.commands.typedCall(
type,
['StellarTxOpRequest', 'StellarSorobanDataRequest', 'StellarSignedTx'],
op
);
return this.processTxRequest(nextRes, operations, index + 1);
}

default:
throw ERRORS.TypedError(
HardwareErrorCode.RuntimeError,
`unexpected response type: ${response.type}`
);
}
};

async run() {
await this.device.commands.typedCall('StellarSignTx', 'StellarTxOpRequest', { ...this.params });
const response = await this.device.commands.typedCall('StellarSignTx', 'StellarTxOpRequest', {
...this.params,
});

return this.processTxRequest(this.operations, 0);
return this.processTxRequest(response, this.operations, 0);
}
}
Loading
Loading