Skip to content
This repository was archived by the owner on Mar 20, 2024. It is now read-only.

Commit 3378be2

Browse files
committed
Parity with Network 0.3.0
1 parent efd8465 commit 3378be2

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@a-block/a-blockjs",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"description": "API wrapper to access the ABlock network",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/mgmt/script.mgmt.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export function constructTxInsAddress(txIns: ICreateTxIn[]): IResult<string> {
118118
script_sig.Pay2PkH.address_version,
119119
);
120120
if (script.isErr()) return err(script.error);
121+
121122
return previousOutPoint !== null
122123
? `${getFormattedOutPointString(previousOutPoint)}-${getFormattedScriptString(
123124
script.value,
@@ -174,7 +175,7 @@ export function p2pkh(
174175
);
175176
const addr = constructAddress(getHexStringBytes(publicKeyData), addressVersion);
176177
if (addr.isErr()) return err(addr.error);
177-
stackEntries.push(new StackEntry('PubKeyHash', addr.value));
178+
stackEntries.push(new StackEntry('Bytes', addr.value));
178179
stackEntries.push(new StackEntry('Op', 'OP_EQUALVERIFY'));
179180
stackEntries.push(new StackEntry('Op', 'OP_CHECKSIG'));
180181
return ok({

src/mgmt/tx.mgmt.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ export function getInputsForTx(
7171
: initIAssetItem({
7272
Item: {
7373
amount: 0,
74-
drs_tx_hash: paymentAsset.Item.drs_tx_hash,
75-
metadata: paymentAsset.Item.metadata,
74+
drs_tx_hash: paymentAsset.Item.drs_tx_hash || '',
75+
metadata: paymentAsset.Item.metadata || null,
7676
},
7777
});
78+
7879
// A list of all addresses used to gather inputs
7980
const usedAddresses: string[] = [];
8081
// A list of all addresses which no longer contain usable assets after this transaction is created

src/services/ablock.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
initIAssetItem,
4242
initIAssetToken,
4343
throwIfErr,
44+
formatAssetStructures,
4445
transformCreateTxResponseFromNetwork,
4546
} from '../utils';
4647
import { mgmtClient } from './mgmt.service';
@@ -1323,7 +1324,8 @@ export class ABlockWallet {
13231324
);
13241325

13251326
// We assume that the filtered data should contain a single key-value pair since DRUID values are unique
1326-
const txInfo = throwIfErr(formatSingleCustomKeyValuePair(rbDataForDruid)).value.value;
1327+
const rawTxInfo = throwIfErr(formatSingleCustomKeyValuePair(rbDataForDruid)).value.value;
1328+
const txInfo = throwIfErr(formatAssetStructures(rawTxInfo));
13271329

13281330
// Get the key-pair assigned to this receiver address
13291331
const receiverKeypair = keyPairMap.get(txInfo.receiverExpectation.to);
@@ -1332,6 +1334,8 @@ export class ABlockWallet {
13321334
// Set the status of the pending request
13331335
txInfo.status = status;
13341336

1337+
console.log('receiverExpectation', txInfo);
1338+
13351339
// Handle case for 'accepted'; create and send transaction to mempool node
13361340
if (status === 'accepted') {
13371341
const sendIbTxHalf = throwIfErr(

src/utils/general.utils.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { v4 as uuidv4 } from 'uuid';
44

55
import { BAL_LIMIT } from '../mgmt/constants';
66

7-
import { IClientResponse, ICustomKeyPair, IErrorInternal, IResult } from '../interfaces';
7+
import { IClientResponse, ICustomKeyPair, IErrorInternal, IPendingIbTxDetails, IResult } from '../interfaces';
88

99
/**
1010
* Cast `status` received from ABlock network to lowercase string variant
@@ -266,3 +266,36 @@ export const formatSingleCustomKeyValuePair = <K extends string | number | symbo
266266
};
267267
return ok(returnValue);
268268
};
269+
270+
/**
271+
* Adds sensible defaults to asset structures for receiving and sending in 2 way transactions. Ensures
272+
* error handling for missing fields.
273+
*
274+
* @param {IPendingIbTxDetails} txStructure
275+
* @return {*} {IResult<IPendingIbTxDetails>}
276+
*/
277+
export const formatAssetStructures = (txStructure: IPendingIbTxDetails): IResult<IPendingIbTxDetails> => {
278+
const defaults = {
279+
amount: 0,
280+
drs_tx_hash: null,
281+
metadata: null
282+
};
283+
284+
if ('Item' in txStructure.senderExpectation.asset) {
285+
let senderExpectation = txStructure.senderExpectation.asset.Item;
286+
Object.assign(senderExpectation, ...Object.entries(defaults).filter(([key]) => !(key in senderExpectation)).map(([key, value]) => ({ [key]: value })));
287+
288+
senderExpectation.metadata = null;
289+
txStructure.senderExpectation.asset.Item = senderExpectation;
290+
}
291+
292+
if ('Item' in txStructure.receiverExpectation.asset) {
293+
let receiverExpectation = txStructure.receiverExpectation.asset.Item;
294+
Object.assign(receiverExpectation, ...Object.entries(defaults).filter(([key]) => !(key in receiverExpectation)).map(([key, value]) => ({ [key]: value })));
295+
296+
receiverExpectation.metadata = null;
297+
txStructure.receiverExpectation.asset.Item = receiverExpectation;
298+
}
299+
300+
return ok(txStructure);
301+
}

0 commit comments

Comments
 (0)