@ledgerhq/coin-internet_computer
Version:
Ledger Internet Computer integration
155 lines • 5 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateSignaturesPayload = exports.ingressExpiry = exports.generateOperations = void 0;
const cbor = __importStar(require("simple-cbor"));
const candid_1 = require("@dfinity/candid");
const big_integer_1 = __importDefault(require("big-integer"));
const addresses_1 = require("../addresses");
const generateOperations = (tr, a) => {
const { address } = (0, addresses_1.getAddress)(a);
const currency = {
symbol: "ICP",
decimals: 8,
};
const type = "TRANSACTION";
const operations = [];
operations.push({
operation_identifier: {
index: 0,
},
type,
account: {
address,
},
amount: {
value: `-${tr.amount}`,
currency,
},
});
operations.push({
operation_identifier: {
index: 1,
},
type,
account: {
address: tr.recipient,
},
amount: {
value: `${tr.amount}`,
currency,
},
});
operations.push({
operation_identifier: {
index: 2,
},
type: "FEE",
account: {
address,
},
amount: {
value: `-${tr.fees}`,
currency,
},
});
return operations;
};
exports.generateOperations = generateOperations;
function expiryEncode(val) {
let value = (0, big_integer_1.default)(val.toString());
if (value < (0, big_integer_1.default)(0)) {
throw new Error("Cannot leb encode negative values.");
}
const byteLength = (value === (0, big_integer_1.default)(0) ? 0 : Math.ceil(Math.log2(Number(value)))) + 1;
const pipe = new candid_1.PipeArrayBuffer(new ArrayBuffer(byteLength), 0);
// eslint-disable-next-line no-constant-condition
while (true) {
const i = Number(value.and((0, big_integer_1.default)(0x7f)));
value = value.divide((0, big_integer_1.default)(0x80));
if (value.eq((0, big_integer_1.default)(0))) {
pipe.write(new Uint8Array([i]));
break;
}
else {
pipe.write(new Uint8Array([i | 0x80]));
}
}
return pipe.buffer;
}
class ingressExpiry {
value;
constructor(value) {
// Use bigint because it can overflow the maximum number allowed in a double float.
this.value = value;
}
toCBOR() {
return cbor.value.u64(this.value.toString(16), 16);
}
toHash() {
return expiryEncode(this.value);
}
}
exports.ingressExpiry = ingressExpiry;
const generateSignaturesPayload = (signs, payloads, pubkey) => {
const signatures = [];
const [txnPayload, readStatePayload] = payloads;
signatures.push({
signing_payload: {
account_identifier: {
address: txnPayload.account_identifier.address,
},
hex_bytes: txnPayload.hex_bytes,
signature_type: txnPayload.signature_type,
},
public_key: {
hex_bytes: pubkey,
curve_type: "secp256k1",
},
signature_type: "ecdsa",
hex_bytes: signs.txnSig,
});
signatures.push({
signing_payload: {
account_identifier: {
address: readStatePayload.account_identifier.address,
},
hex_bytes: readStatePayload.hex_bytes,
signature_type: readStatePayload.signature_type,
},
public_key: {
hex_bytes: pubkey,
curve_type: "secp256k1",
},
signature_type: "ecdsa",
hex_bytes: signs.readSig,
});
return signatures;
};
exports.generateSignaturesPayload = generateSignaturesPayload;
//# sourceMappingURL=utils.js.map