utxostack / rgbpp-sdk

Utilities for Bitcoin and RGB++ asset integration
ISC License
53 stars 19 forks source link

feat(rgbpp-sdk-service): support `generate_rgbpp_transfer_all_txs` API #278

Closed ShookLyngs closed 3 months ago

ShookLyngs commented 3 months ago

Changes

Found issues

Case transformation issue

The toSnakeCase() util transforms all object keys to snake_case, e.g. snakeCase to snake_case. But when transforming the TransactionGroupSummary object, an issue was found that if the object key is a hex string, it will also be transformed as snake_case, e.g. 0xe1e to 0_xe_1e. This is unexpected.

In the current implementation of the generate_rgbpp_transfer_all_txs API, I wrapped the result with an util ensureSafeJson(), which detects if the object key starts with 0_x, then all underscore marks in the key will be removed, reverting it to a regular hex string, e.g. 0_xe_1e to 0xe1e.

// XXX: Remove underscores from hex strings due to toSnakeCase() transformation issue
if (key.startsWith('0_x')) {
  key = key.replaceAll('_', '');
}

BigInt to String issue

The requests in the rgbpp-sdk-service returns JSON as response. An JSON-related issue was found that it doesn't serialize BIgInt values. When an object that contains such type of value is passed to JSON.stringify(), the function throws an error:

TypeError: Do not know how to serialize a BigInt

Currently, I just transform all BIgInt values to hex strings:

// XXX: Convert BigInt to hex string for JSON.stringify() compatibility
if (typeof value === 'bigint') {
  obj[key] = `0x${value.toString(16)}`;
} else {
  obj[key] = value;
}
duanyytop commented 3 months ago

an issue was found that if the object key is a hex string, it will also be transformed as snake_case, e.g. 0xe1e to 0_xe_1e. This is unexpected.

Why does a key with 0x-hex-string appear?

duanyytop commented 3 months ago

Relative imports must end with .js

May I ask why the .js suffix is needed? It worked fine in my local computer without type: module and .js suffix.