@atomicfinance/bitcoin-dlc-provider 3.5.0 → 3.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/dist/BitcoinDlcProvider.js +229 -75
- package/dist/BitcoinDlcProvider.js.map +1 -1
- package/lib/BitcoinDlcProvider.js +2181 -0
- package/lib/BitcoinDlcProvider.ts +316 -99
- package/lib/index.js +25 -0
- package/lib/utils/Utils.js +78 -0
- package/package.json +5 -5
|
@@ -60,6 +60,7 @@ import {
|
|
|
60
60
|
import {
|
|
61
61
|
CetAdaptorSignaturesV0,
|
|
62
62
|
ContractDescriptor,
|
|
63
|
+
ContractDescriptorV0,
|
|
63
64
|
ContractDescriptorV1,
|
|
64
65
|
ContractInfo,
|
|
65
66
|
ContractInfoV0,
|
|
@@ -581,8 +582,34 @@ export default class BitcoinDlcProvider
|
|
|
581
582
|
0,
|
|
582
583
|
);
|
|
583
584
|
|
|
584
|
-
|
|
585
|
-
|
|
585
|
+
let payouts: PayoutRequest[] = [];
|
|
586
|
+
let messagesList: Messages[] = [];
|
|
587
|
+
|
|
588
|
+
if (
|
|
589
|
+
dlcOffer.contractInfo.type === MessageType.ContractInfoV0 &&
|
|
590
|
+
(dlcOffer.contractInfo as ContractInfoV0).contractDescriptor.type ===
|
|
591
|
+
MessageType.ContractDescriptorV0
|
|
592
|
+
) {
|
|
593
|
+
for (const outcome of ((dlcOffer.contractInfo as ContractInfoV0)
|
|
594
|
+
.contractDescriptor as ContractDescriptorV0).outcomes) {
|
|
595
|
+
payouts.push({
|
|
596
|
+
local: outcome.localPayout,
|
|
597
|
+
remote:
|
|
598
|
+
dlcOffer.offerCollateralSatoshis +
|
|
599
|
+
dlcAccept.acceptCollateralSatoshis -
|
|
600
|
+
outcome.localPayout,
|
|
601
|
+
});
|
|
602
|
+
messagesList.push({ messages: [outcome.outcome.toString()] });
|
|
603
|
+
}
|
|
604
|
+
} else {
|
|
605
|
+
const payoutResponses = this.GetPayouts(dlcOffer);
|
|
606
|
+
const {
|
|
607
|
+
payouts: tempPayouts,
|
|
608
|
+
messagesList: tempMessagesList,
|
|
609
|
+
} = this.FlattenPayouts(payoutResponses);
|
|
610
|
+
payouts = tempPayouts;
|
|
611
|
+
messagesList = tempMessagesList;
|
|
612
|
+
}
|
|
586
613
|
|
|
587
614
|
const dlcTxRequest: CreateDlcTransactionsRequest = {
|
|
588
615
|
payouts,
|
|
@@ -868,27 +895,20 @@ export default class BitcoinDlcProvider
|
|
|
868
895
|
dlcOffer.contractInfo,
|
|
869
896
|
);
|
|
870
897
|
|
|
871
|
-
const indices = this.GetIndicesFromPayouts(this.GetPayouts(_dlcOffer));
|
|
872
898
|
const sigs: ISig[][] = [];
|
|
873
899
|
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
startingIndex,
|
|
882
|
-
endingIndex,
|
|
883
|
-
);
|
|
884
|
-
const oracleEventCetsHex = cetsHex.slice(startingIndex, endingIndex);
|
|
900
|
+
if (
|
|
901
|
+
dlcOffer.contractInfo.type === MessageType.ContractInfoV0 &&
|
|
902
|
+
(dlcOffer.contractInfo as ContractInfoV0).contractDescriptor.type ===
|
|
903
|
+
MessageType.ContractDescriptorV0
|
|
904
|
+
) {
|
|
905
|
+
for (const [_, { oracleInfo }] of contractOraclePairs.entries()) {
|
|
906
|
+
const oracleAnnouncement = oracleInfo.announcement;
|
|
885
907
|
|
|
886
|
-
|
|
887
|
-
const adaptorSigRequestPromises: Promise<AdaptorPair[]>[] = [];
|
|
908
|
+
const adaptorSigRequestPromises: Promise<AdaptorPair[]>[] = [];
|
|
888
909
|
|
|
889
|
-
|
|
890
|
-
const
|
|
891
|
-
const tempCetsHex = oracleEventCetsHex.slice(i, i + chunk);
|
|
910
|
+
const tempMessagesList = messagesList;
|
|
911
|
+
const tempCetsHex = cetsHex;
|
|
892
912
|
|
|
893
913
|
const cetSignRequest: CreateCetAdaptorSignaturesRequest = {
|
|
894
914
|
messagesList: tempMessagesList,
|
|
@@ -913,20 +933,81 @@ export default class BitcoinDlcProvider
|
|
|
913
933
|
return response.adaptorPairs;
|
|
914
934
|
})(),
|
|
915
935
|
);
|
|
936
|
+
|
|
937
|
+
const adaptorPairs: AdaptorPair[] = (
|
|
938
|
+
await Promise.all(adaptorSigRequestPromises)
|
|
939
|
+
).flat();
|
|
940
|
+
|
|
941
|
+
sigs.push(
|
|
942
|
+
adaptorPairs.map((adaptorPair) => {
|
|
943
|
+
return {
|
|
944
|
+
encryptedSig: Buffer.from(adaptorPair.signature, 'hex'),
|
|
945
|
+
dleqProof: Buffer.from(adaptorPair.proof, 'hex'),
|
|
946
|
+
};
|
|
947
|
+
}),
|
|
948
|
+
);
|
|
916
949
|
}
|
|
950
|
+
} else {
|
|
951
|
+
const indices = this.GetIndicesFromPayouts(this.GetPayouts(_dlcOffer));
|
|
917
952
|
|
|
918
|
-
const
|
|
919
|
-
|
|
920
|
-
).flat();
|
|
953
|
+
for (const [index, { oracleInfo }] of contractOraclePairs.entries()) {
|
|
954
|
+
const oracleAnnouncement = oracleInfo.announcement;
|
|
921
955
|
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
956
|
+
const startingIndex = indices[index].startingMessagesIndex,
|
|
957
|
+
endingIndex = indices[index + 1].startingMessagesIndex;
|
|
958
|
+
|
|
959
|
+
const oracleEventMessagesList = messagesList.slice(
|
|
960
|
+
startingIndex,
|
|
961
|
+
endingIndex,
|
|
962
|
+
);
|
|
963
|
+
const oracleEventCetsHex = cetsHex.slice(startingIndex, endingIndex);
|
|
964
|
+
|
|
965
|
+
const chunk = 100;
|
|
966
|
+
const adaptorSigRequestPromises: Promise<AdaptorPair[]>[] = [];
|
|
967
|
+
|
|
968
|
+
for (let i = 0, j = oracleEventMessagesList.length; i < j; i += chunk) {
|
|
969
|
+
const tempMessagesList = oracleEventMessagesList.slice(i, i + chunk);
|
|
970
|
+
const tempCetsHex = oracleEventCetsHex.slice(i, i + chunk);
|
|
971
|
+
|
|
972
|
+
const cetSignRequest: CreateCetAdaptorSignaturesRequest = {
|
|
973
|
+
messagesList: tempMessagesList,
|
|
974
|
+
cetsHex: tempCetsHex,
|
|
975
|
+
privkey: fundPrivateKey,
|
|
976
|
+
fundTxId: dlcTxs.fundTx.txId.toString(),
|
|
977
|
+
fundVout: dlcTxs.fundTxVout,
|
|
978
|
+
localFundPubkey: dlcOffer.fundingPubKey.toString('hex'),
|
|
979
|
+
remoteFundPubkey: dlcAccept.fundingPubKey.toString('hex'),
|
|
980
|
+
fundInputAmount:
|
|
981
|
+
dlcTxs.fundTx.outputs[dlcTxs.fundTxVout].value.sats,
|
|
982
|
+
oraclePubkey: oracleAnnouncement.oraclePubkey.toString('hex'),
|
|
983
|
+
oracleRValues: oracleAnnouncement.oracleEvent.oracleNonces.map(
|
|
984
|
+
(nonce) => nonce.toString('hex'),
|
|
985
|
+
),
|
|
927
986
|
};
|
|
928
|
-
|
|
929
|
-
|
|
987
|
+
|
|
988
|
+
adaptorSigRequestPromises.push(
|
|
989
|
+
(async () => {
|
|
990
|
+
const response = await this.CreateCetAdaptorSignatures(
|
|
991
|
+
cetSignRequest,
|
|
992
|
+
);
|
|
993
|
+
return response.adaptorPairs;
|
|
994
|
+
})(),
|
|
995
|
+
);
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
const adaptorPairs: AdaptorPair[] = (
|
|
999
|
+
await Promise.all(adaptorSigRequestPromises)
|
|
1000
|
+
).flat();
|
|
1001
|
+
|
|
1002
|
+
sigs.push(
|
|
1003
|
+
adaptorPairs.map((adaptorPair) => {
|
|
1004
|
+
return {
|
|
1005
|
+
encryptedSig: Buffer.from(adaptorPair.signature, 'hex'),
|
|
1006
|
+
dleqProof: Buffer.from(adaptorPair.proof, 'hex'),
|
|
1007
|
+
};
|
|
1008
|
+
}),
|
|
1009
|
+
);
|
|
1010
|
+
}
|
|
930
1011
|
}
|
|
931
1012
|
|
|
932
1013
|
const refundSignRequest: GetRawRefundTxSignatureRequest = {
|
|
@@ -967,36 +1048,28 @@ export default class BitcoinDlcProvider
|
|
|
967
1048
|
|
|
968
1049
|
const cetsHex = dlcTxs.cets.map((cet) => cet.serialize().toString('hex'));
|
|
969
1050
|
|
|
970
|
-
const chunk = 100;
|
|
971
|
-
|
|
972
1051
|
const contractOraclePairs = this.GetContractOraclePairs(
|
|
973
1052
|
dlcOffer.contractInfo,
|
|
974
1053
|
);
|
|
975
1054
|
|
|
976
|
-
|
|
1055
|
+
if (
|
|
1056
|
+
dlcOffer.contractInfo.type === MessageType.ContractInfoV0 &&
|
|
1057
|
+
(dlcOffer.contractInfo as ContractInfoV0).contractDescriptor.type ===
|
|
1058
|
+
MessageType.ContractDescriptorV0
|
|
1059
|
+
) {
|
|
1060
|
+
for (const [_, { oracleInfo }] of contractOraclePairs.entries()) {
|
|
1061
|
+
const oracleAnnouncement = oracleInfo.announcement;
|
|
977
1062
|
|
|
978
|
-
|
|
979
|
-
|
|
1063
|
+
const oracleEventCetsHex = cetsHex;
|
|
1064
|
+
const oracleEventSigs = isOfferer
|
|
1065
|
+
? dlcAccept.cetSignatures.sigs
|
|
1066
|
+
: dlcSign.cetSignatures.sigs;
|
|
980
1067
|
|
|
981
|
-
|
|
982
|
-
endingIndex = indices[index + 1].startingMessagesIndex;
|
|
1068
|
+
const sigsValidity: Promise<boolean>[] = [];
|
|
983
1069
|
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
);
|
|
988
|
-
const oracleEventCetsHex = cetsHex.slice(startingIndex, endingIndex);
|
|
989
|
-
const oracleEventSigs = (isOfferer
|
|
990
|
-
? dlcAccept.cetSignatures.sigs
|
|
991
|
-
: dlcSign.cetSignatures.sigs
|
|
992
|
-
).slice(startingIndex, endingIndex);
|
|
993
|
-
|
|
994
|
-
const sigsValidity: Promise<boolean>[] = [];
|
|
995
|
-
|
|
996
|
-
for (let i = 0, j = oracleEventMessagesList.length; i < j; i += chunk) {
|
|
997
|
-
const tempMessagesList = oracleEventMessagesList.slice(i, i + chunk);
|
|
998
|
-
const tempCetsHex = oracleEventCetsHex.slice(i, i + chunk);
|
|
999
|
-
const tempSigs = oracleEventSigs.slice(i, i + chunk);
|
|
1070
|
+
const tempMessagesList = messagesList;
|
|
1071
|
+
const tempCetsHex = oracleEventCetsHex;
|
|
1072
|
+
const tempSigs = oracleEventSigs;
|
|
1000
1073
|
const tempAdaptorPairs = tempSigs.map((sig) => {
|
|
1001
1074
|
return {
|
|
1002
1075
|
signature: sig.encryptedSig.toString('hex'),
|
|
@@ -1028,29 +1101,113 @@ export default class BitcoinDlcProvider
|
|
|
1028
1101
|
return response.valid;
|
|
1029
1102
|
})(),
|
|
1030
1103
|
);
|
|
1104
|
+
|
|
1105
|
+
let areSigsValid = (await Promise.all(sigsValidity)).every((b) => b);
|
|
1106
|
+
|
|
1107
|
+
const verifyRefundSigRequest: VerifyRefundTxSignatureRequest = {
|
|
1108
|
+
refundTxHex: dlcTxs.refundTx.serialize().toString('hex'),
|
|
1109
|
+
signature: isOfferer
|
|
1110
|
+
? dlcAccept.refundSignature.toString('hex')
|
|
1111
|
+
: dlcSign.refundSignature.toString('hex'),
|
|
1112
|
+
localFundPubkey: dlcOffer.fundingPubKey.toString('hex'),
|
|
1113
|
+
remoteFundPubkey: dlcAccept.fundingPubKey.toString('hex'),
|
|
1114
|
+
fundTxId: dlcTxs.fundTx.txId.toString(),
|
|
1115
|
+
fundVout: dlcTxs.fundTxVout,
|
|
1116
|
+
fundInputAmount: dlcTxs.fundTx.outputs[dlcTxs.fundTxVout].value.sats,
|
|
1117
|
+
verifyRemote: isOfferer,
|
|
1118
|
+
};
|
|
1119
|
+
|
|
1120
|
+
areSigsValid =
|
|
1121
|
+
areSigsValid &&
|
|
1122
|
+
(await this.VerifyRefundTxSignature(verifyRefundSigRequest)).valid;
|
|
1123
|
+
|
|
1124
|
+
if (!areSigsValid) {
|
|
1125
|
+
throw new Error('Invalid signatures received');
|
|
1126
|
+
}
|
|
1031
1127
|
}
|
|
1128
|
+
} else {
|
|
1129
|
+
const chunk = 100;
|
|
1032
1130
|
|
|
1033
|
-
|
|
1131
|
+
const indices = this.GetIndicesFromPayouts(this.GetPayouts(_dlcOffer));
|
|
1034
1132
|
|
|
1035
|
-
const
|
|
1036
|
-
|
|
1037
|
-
signature: isOfferer
|
|
1038
|
-
? dlcAccept.refundSignature.toString('hex')
|
|
1039
|
-
: dlcSign.refundSignature.toString('hex'),
|
|
1040
|
-
localFundPubkey: dlcOffer.fundingPubKey.toString('hex'),
|
|
1041
|
-
remoteFundPubkey: dlcAccept.fundingPubKey.toString('hex'),
|
|
1042
|
-
fundTxId: dlcTxs.fundTx.txId.toString(),
|
|
1043
|
-
fundVout: dlcTxs.fundTxVout,
|
|
1044
|
-
fundInputAmount: dlcTxs.fundTx.outputs[dlcTxs.fundTxVout].value.sats,
|
|
1045
|
-
verifyRemote: isOfferer,
|
|
1046
|
-
};
|
|
1133
|
+
for (const [index, { oracleInfo }] of contractOraclePairs.entries()) {
|
|
1134
|
+
const oracleAnnouncement = oracleInfo.announcement;
|
|
1047
1135
|
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
(await this.VerifyRefundTxSignature(verifyRefundSigRequest)).valid;
|
|
1136
|
+
const startingIndex = indices[index].startingMessagesIndex,
|
|
1137
|
+
endingIndex = indices[index + 1].startingMessagesIndex;
|
|
1051
1138
|
|
|
1052
|
-
|
|
1053
|
-
|
|
1139
|
+
const oracleEventMessagesList = messagesList.slice(
|
|
1140
|
+
startingIndex,
|
|
1141
|
+
endingIndex,
|
|
1142
|
+
);
|
|
1143
|
+
const oracleEventCetsHex = cetsHex.slice(startingIndex, endingIndex);
|
|
1144
|
+
const oracleEventSigs = (isOfferer
|
|
1145
|
+
? dlcAccept.cetSignatures.sigs
|
|
1146
|
+
: dlcSign.cetSignatures.sigs
|
|
1147
|
+
).slice(startingIndex, endingIndex);
|
|
1148
|
+
|
|
1149
|
+
const sigsValidity: Promise<boolean>[] = [];
|
|
1150
|
+
|
|
1151
|
+
for (let i = 0, j = oracleEventMessagesList.length; i < j; i += chunk) {
|
|
1152
|
+
const tempMessagesList = oracleEventMessagesList.slice(i, i + chunk);
|
|
1153
|
+
const tempCetsHex = oracleEventCetsHex.slice(i, i + chunk);
|
|
1154
|
+
const tempSigs = oracleEventSigs.slice(i, i + chunk);
|
|
1155
|
+
const tempAdaptorPairs = tempSigs.map((sig) => {
|
|
1156
|
+
return {
|
|
1157
|
+
signature: sig.encryptedSig.toString('hex'),
|
|
1158
|
+
proof: sig.dleqProof.toString('hex'),
|
|
1159
|
+
};
|
|
1160
|
+
});
|
|
1161
|
+
|
|
1162
|
+
const verifyCetAdaptorSignaturesRequest: VerifyCetAdaptorSignaturesRequest = {
|
|
1163
|
+
cetsHex: tempCetsHex,
|
|
1164
|
+
messagesList: tempMessagesList,
|
|
1165
|
+
oraclePubkey: oracleAnnouncement.oraclePubkey.toString('hex'),
|
|
1166
|
+
oracleRValues: oracleAnnouncement.oracleEvent.oracleNonces.map(
|
|
1167
|
+
(nonce) => nonce.toString('hex'),
|
|
1168
|
+
),
|
|
1169
|
+
adaptorPairs: tempAdaptorPairs,
|
|
1170
|
+
localFundPubkey: dlcOffer.fundingPubKey.toString('hex'),
|
|
1171
|
+
remoteFundPubkey: dlcAccept.fundingPubKey.toString('hex'),
|
|
1172
|
+
fundTxId: dlcTxs.fundTx.txId.toString(),
|
|
1173
|
+
fundVout: dlcTxs.fundTxVout,
|
|
1174
|
+
fundInputAmount:
|
|
1175
|
+
dlcTxs.fundTx.outputs[dlcTxs.fundTxVout].value.sats,
|
|
1176
|
+
verifyRemote: isOfferer,
|
|
1177
|
+
};
|
|
1178
|
+
|
|
1179
|
+
sigsValidity.push(
|
|
1180
|
+
(async () => {
|
|
1181
|
+
const response = await this.VerifyCetAdaptorSignatures(
|
|
1182
|
+
verifyCetAdaptorSignaturesRequest,
|
|
1183
|
+
);
|
|
1184
|
+
return response.valid;
|
|
1185
|
+
})(),
|
|
1186
|
+
);
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
let areSigsValid = (await Promise.all(sigsValidity)).every((b) => b);
|
|
1190
|
+
|
|
1191
|
+
const verifyRefundSigRequest: VerifyRefundTxSignatureRequest = {
|
|
1192
|
+
refundTxHex: dlcTxs.refundTx.serialize().toString('hex'),
|
|
1193
|
+
signature: isOfferer
|
|
1194
|
+
? dlcAccept.refundSignature.toString('hex')
|
|
1195
|
+
: dlcSign.refundSignature.toString('hex'),
|
|
1196
|
+
localFundPubkey: dlcOffer.fundingPubKey.toString('hex'),
|
|
1197
|
+
remoteFundPubkey: dlcAccept.fundingPubKey.toString('hex'),
|
|
1198
|
+
fundTxId: dlcTxs.fundTx.txId.toString(),
|
|
1199
|
+
fundVout: dlcTxs.fundTxVout,
|
|
1200
|
+
fundInputAmount: dlcTxs.fundTx.outputs[dlcTxs.fundTxVout].value.sats,
|
|
1201
|
+
verifyRemote: isOfferer,
|
|
1202
|
+
};
|
|
1203
|
+
|
|
1204
|
+
areSigsValid =
|
|
1205
|
+
areSigsValid &&
|
|
1206
|
+
(await this.VerifyRefundTxSignature(verifyRefundSigRequest)).valid;
|
|
1207
|
+
|
|
1208
|
+
if (!areSigsValid) {
|
|
1209
|
+
throw new Error('Invalid signatures received');
|
|
1210
|
+
}
|
|
1054
1211
|
}
|
|
1055
1212
|
}
|
|
1056
1213
|
}
|
|
@@ -1499,8 +1656,15 @@ Payout Group not found',
|
|
|
1499
1656
|
case MessageType.ContractInfoV0: {
|
|
1500
1657
|
const contractInfo = dlcOffer.contractInfo as ContractInfoV0;
|
|
1501
1658
|
switch (contractInfo.contractDescriptor.type) {
|
|
1502
|
-
case MessageType.ContractDescriptorV0:
|
|
1503
|
-
|
|
1659
|
+
case MessageType.ContractDescriptorV0: {
|
|
1660
|
+
const oracleInfo = contractInfo.oracleInfo;
|
|
1661
|
+
if (
|
|
1662
|
+
oracleInfo.announcement.oracleEvent.eventId !==
|
|
1663
|
+
oracleAttestation.eventId
|
|
1664
|
+
)
|
|
1665
|
+
throw Error('Incorrect Oracle Attestation. Event Id must match.');
|
|
1666
|
+
break;
|
|
1667
|
+
}
|
|
1504
1668
|
case MessageType.ContractDescriptorV1: {
|
|
1505
1669
|
const oracleInfo = contractInfo.oracleInfo;
|
|
1506
1670
|
if (
|
|
@@ -1551,39 +1715,76 @@ Payout Group not found',
|
|
|
1551
1715
|
if (isOfferer === undefined)
|
|
1552
1716
|
isOfferer = await this.isOfferer(dlcOffer, dlcAccept);
|
|
1553
1717
|
|
|
1554
|
-
const { index: outcomeIndex, groupLength } = await this.FindOutcomeIndex(
|
|
1555
|
-
dlcOffer,
|
|
1556
|
-
oracleAttestation,
|
|
1557
|
-
);
|
|
1558
|
-
|
|
1559
1718
|
const fundPrivateKey = await this.GetFundPrivateKey(
|
|
1560
1719
|
dlcOffer,
|
|
1561
1720
|
dlcAccept,
|
|
1562
1721
|
isOfferer,
|
|
1563
1722
|
);
|
|
1564
1723
|
|
|
1565
|
-
|
|
1724
|
+
let signCetRequest: SignCetRequest;
|
|
1566
1725
|
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1726
|
+
if (
|
|
1727
|
+
dlcOffer.contractInfo.type === MessageType.ContractInfoV0 &&
|
|
1728
|
+
(dlcOffer.contractInfo as ContractInfoV0).contractDescriptor.type ===
|
|
1729
|
+
MessageType.ContractDescriptorV0
|
|
1730
|
+
) {
|
|
1731
|
+
const outcomeIndex = ((dlcOffer.contractInfo as ContractInfoV0)
|
|
1732
|
+
.contractDescriptor as ContractDescriptorV0).outcomes.findIndex(
|
|
1733
|
+
(outcome) =>
|
|
1734
|
+
outcome.outcome.toString() ===
|
|
1735
|
+
oracleAttestation.outcomes[0].toString(),
|
|
1736
|
+
);
|
|
1571
1737
|
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1738
|
+
signCetRequest = {
|
|
1739
|
+
cetHex: dlcTxs.cets[outcomeIndex].serialize().toString('hex'),
|
|
1740
|
+
fundPrivkey: fundPrivateKey,
|
|
1741
|
+
fundTxId: dlcTxs.fundTx.txId.toString(),
|
|
1742
|
+
fundVout: dlcTxs.fundTxVout,
|
|
1743
|
+
localFundPubkey: dlcOffer.fundingPubKey.toString('hex'),
|
|
1744
|
+
remoteFundPubkey: dlcAccept.fundingPubKey.toString('hex'),
|
|
1745
|
+
oracleSignatures: oracleAttestation.signatures.map((sig) =>
|
|
1746
|
+
sig.toString('hex'),
|
|
1747
|
+
),
|
|
1748
|
+
fundInputAmount: dlcTxs.fundTx.outputs[dlcTxs.fundTxVout].value.sats,
|
|
1749
|
+
adaptorSignature: isOfferer
|
|
1750
|
+
? dlcAccept.cetSignatures.sigs[outcomeIndex].encryptedSig.toString(
|
|
1751
|
+
'hex',
|
|
1752
|
+
)
|
|
1753
|
+
: dlcSign.cetSignatures.sigs[outcomeIndex].encryptedSig.toString(
|
|
1754
|
+
'hex',
|
|
1755
|
+
),
|
|
1756
|
+
};
|
|
1757
|
+
} else {
|
|
1758
|
+
const { index: outcomeIndex, groupLength } = await this.FindOutcomeIndex(
|
|
1759
|
+
dlcOffer,
|
|
1760
|
+
oracleAttestation,
|
|
1761
|
+
);
|
|
1762
|
+
|
|
1763
|
+
const sliceIndex = -(oracleAttestation.signatures.length - groupLength);
|
|
1764
|
+
|
|
1765
|
+
const oracleSignatures =
|
|
1766
|
+
sliceIndex === 0
|
|
1767
|
+
? oracleAttestation.signatures
|
|
1768
|
+
: oracleAttestation.signatures.slice(0, sliceIndex);
|
|
1769
|
+
|
|
1770
|
+
signCetRequest = {
|
|
1771
|
+
cetHex: dlcTxs.cets[outcomeIndex].serialize().toString('hex'),
|
|
1772
|
+
fundPrivkey: fundPrivateKey,
|
|
1773
|
+
fundTxId: dlcTxs.fundTx.txId.toString(),
|
|
1774
|
+
fundVout: dlcTxs.fundTxVout,
|
|
1775
|
+
localFundPubkey: dlcOffer.fundingPubKey.toString('hex'),
|
|
1776
|
+
remoteFundPubkey: dlcAccept.fundingPubKey.toString('hex'),
|
|
1777
|
+
oracleSignatures: oracleSignatures.map((sig) => sig.toString('hex')),
|
|
1778
|
+
fundInputAmount: dlcTxs.fundTx.outputs[dlcTxs.fundTxVout].value.sats,
|
|
1779
|
+
adaptorSignature: isOfferer
|
|
1780
|
+
? dlcAccept.cetSignatures.sigs[outcomeIndex].encryptedSig.toString(
|
|
1781
|
+
'hex',
|
|
1782
|
+
)
|
|
1783
|
+
: dlcSign.cetSignatures.sigs[outcomeIndex].encryptedSig.toString(
|
|
1784
|
+
'hex',
|
|
1785
|
+
),
|
|
1786
|
+
};
|
|
1787
|
+
}
|
|
1587
1788
|
|
|
1588
1789
|
const finalCet = (await this.SignCet(signCetRequest)).hex;
|
|
1589
1790
|
|
|
@@ -2667,8 +2868,24 @@ Payout Group not found',
|
|
|
2667
2868
|
_dlcTxs,
|
|
2668
2869
|
});
|
|
2669
2870
|
|
|
2670
|
-
|
|
2671
|
-
|
|
2871
|
+
let messagesList: Messages[] = [];
|
|
2872
|
+
|
|
2873
|
+
if (
|
|
2874
|
+
dlcOffer.contractInfo.type === MessageType.ContractInfoV0 &&
|
|
2875
|
+
(dlcOffer.contractInfo as ContractInfoV0).contractDescriptor.type ===
|
|
2876
|
+
MessageType.ContractDescriptorV0
|
|
2877
|
+
) {
|
|
2878
|
+
for (const outcome of ((dlcOffer.contractInfo as ContractInfoV0)
|
|
2879
|
+
.contractDescriptor as ContractDescriptorV0).outcomes) {
|
|
2880
|
+
messagesList.push({ messages: [outcome.outcome.toString()] });
|
|
2881
|
+
}
|
|
2882
|
+
} else {
|
|
2883
|
+
const payoutResponses = this.GetPayouts(dlcOffer);
|
|
2884
|
+
const { messagesList: oracleEventMessagesList } = this.FlattenPayouts(
|
|
2885
|
+
payoutResponses,
|
|
2886
|
+
);
|
|
2887
|
+
messagesList = oracleEventMessagesList;
|
|
2888
|
+
}
|
|
2672
2889
|
|
|
2673
2890
|
await this.VerifyCetAdaptorAndRefundSigs(
|
|
2674
2891
|
dlcOffer,
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.default = void 0;
|
|
21
|
+
__exportStar(require("./utils/Utils"), exports);
|
|
22
|
+
var BitcoinDlcProvider_1 = require("./BitcoinDlcProvider");
|
|
23
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(BitcoinDlcProvider_1).default; } });
|
|
24
|
+
__exportStar(require("./BitcoinDlcProvider"), exports);
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.outputsToPayouts = exports.checkTypes = exports.generateSerialIds = exports.generateSerialId = exports.asyncForEach = void 0;
|
|
7
|
+
const messaging_1 = require("@node-dlc/messaging");
|
|
8
|
+
const randombytes_1 = __importDefault(require("randombytes"));
|
|
9
|
+
const asyncForEach = async (array, callback) => {
|
|
10
|
+
for (let index = 0; index < array.length; index++) {
|
|
11
|
+
await callback(array[index], index, array);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
exports.asyncForEach = asyncForEach;
|
|
15
|
+
function generateSerialId() {
|
|
16
|
+
return (0, randombytes_1.default)(4).reduce((acc, num, i) => acc + num ** i, 0);
|
|
17
|
+
}
|
|
18
|
+
exports.generateSerialId = generateSerialId;
|
|
19
|
+
function generateSerialIds(count) {
|
|
20
|
+
return Array.from({ length: count }, () => generateSerialId());
|
|
21
|
+
}
|
|
22
|
+
exports.generateSerialIds = generateSerialIds;
|
|
23
|
+
function checkTypes(types) {
|
|
24
|
+
const { _dlcOffer, _dlcAccept, _dlcSign, _dlcClose, _dlcTxs } = types;
|
|
25
|
+
if (_dlcOffer && _dlcOffer.type !== messaging_1.MessageType.DlcOfferV0)
|
|
26
|
+
throw Error('DlcOffer must be V0');
|
|
27
|
+
if (_dlcAccept && _dlcAccept.type !== messaging_1.MessageType.DlcAcceptV0)
|
|
28
|
+
throw Error('DlcAccept must be V0');
|
|
29
|
+
if (_dlcSign && _dlcSign.type !== messaging_1.MessageType.DlcSignV0)
|
|
30
|
+
throw Error('DlcSign must be V0');
|
|
31
|
+
if (_dlcClose && _dlcClose.type !== messaging_1.MessageType.DlcCloseV0)
|
|
32
|
+
throw Error('DlcClose must be V0');
|
|
33
|
+
if (_dlcTxs && _dlcTxs.type !== messaging_1.MessageType.DlcTransactionsV0)
|
|
34
|
+
throw Error('DlcTransactions must be V0');
|
|
35
|
+
let dlcOffer;
|
|
36
|
+
let dlcAccept;
|
|
37
|
+
let dlcSign;
|
|
38
|
+
let dlcClose;
|
|
39
|
+
let dlcTxs;
|
|
40
|
+
if (_dlcOffer)
|
|
41
|
+
dlcOffer = _dlcOffer;
|
|
42
|
+
if (_dlcAccept)
|
|
43
|
+
dlcAccept = _dlcAccept;
|
|
44
|
+
if (_dlcSign)
|
|
45
|
+
dlcSign = _dlcSign;
|
|
46
|
+
if (_dlcClose)
|
|
47
|
+
dlcClose = _dlcClose;
|
|
48
|
+
if (_dlcTxs)
|
|
49
|
+
dlcTxs = _dlcTxs;
|
|
50
|
+
return { dlcOffer, dlcAccept, dlcSign, dlcClose, dlcTxs };
|
|
51
|
+
}
|
|
52
|
+
exports.checkTypes = checkTypes;
|
|
53
|
+
function outputsToPayouts(outputs, rValuesMessagesList, localCollateral, remoteCollateral, payoutLocal) {
|
|
54
|
+
const payouts = [];
|
|
55
|
+
const messagesList = [];
|
|
56
|
+
outputs.forEach((output) => {
|
|
57
|
+
const { payout, groups } = output;
|
|
58
|
+
const payoutAmount = payout;
|
|
59
|
+
groups.forEach((group) => {
|
|
60
|
+
const messages = [];
|
|
61
|
+
for (let i = 0; i < group.length; i++) {
|
|
62
|
+
const digit = group[i];
|
|
63
|
+
messages.push(rValuesMessagesList[i].messages[digit]);
|
|
64
|
+
}
|
|
65
|
+
const local = payoutLocal
|
|
66
|
+
? payoutAmount
|
|
67
|
+
: localCollateral + remoteCollateral - payoutAmount;
|
|
68
|
+
const remote = payoutLocal
|
|
69
|
+
? localCollateral + remoteCollateral - payoutAmount
|
|
70
|
+
: payoutAmount;
|
|
71
|
+
payouts.push({ local, remote });
|
|
72
|
+
messagesList.push({ messages });
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
return { payouts, messagesList };
|
|
76
|
+
}
|
|
77
|
+
exports.outputsToPayouts = outputsToPayouts;
|
|
78
|
+
//# sourceMappingURL=Utils.js.map
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atomicfinance/bitcoin-dlc-provider",
|
|
3
3
|
"umdName": "BitcoinDlcProvider",
|
|
4
|
-
"version": "3.5.
|
|
4
|
+
"version": "3.5.2",
|
|
5
5
|
"description": "Bitcoin Abstraction Layer Dlc Provider",
|
|
6
6
|
"author": "Atomic Finance <info@atomic.finance>",
|
|
7
7
|
"homepage": "",
|
|
@@ -15,10 +15,10 @@
|
|
|
15
15
|
"lint:fix": "../../node_modules/.bin/eslint --fix --ignore-path ../../.eslintignore -c ../../.eslintrc.js ."
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@atomicfinance/bitcoin-utils": "3.5.
|
|
19
|
-
"@atomicfinance/provider": "^3.5.
|
|
20
|
-
"@atomicfinance/types": "^3.5.
|
|
21
|
-
"@atomicfinance/utils": "^3.5.
|
|
18
|
+
"@atomicfinance/bitcoin-utils": "3.5.2",
|
|
19
|
+
"@atomicfinance/provider": "^3.5.2",
|
|
20
|
+
"@atomicfinance/types": "^3.5.2",
|
|
21
|
+
"@atomicfinance/utils": "^3.5.2",
|
|
22
22
|
"@node-dlc/core": "0.23.6",
|
|
23
23
|
"@node-dlc/messaging": "0.23.6",
|
|
24
24
|
"@node-lightning/bitcoin": "0.26.1",
|