@emurgo/yoroi-lib-core
Advanced tools
Comparing version 0.8.8-alpha.50 to 0.8.9-alpha.51
@@ -34,2 +34,3 @@ "use strict"; | ||
const transactions_1 = require("./internals/utils/transactions"); | ||
const wasm_contract_1 = require("./internals/wasm-contract"); | ||
const ledgerjs_hw_app_cardano_1 = require("@cardano-foundation/ledgerjs-hw-app-cardano"); | ||
@@ -115,20 +116,3 @@ var account_1 = require("./account"); | ||
const rewardAddressHex = Buffer.from(yield rewardAddress.toAddress().then(x => x.toBytes())).toString('hex'); | ||
const registrationData = yield this.Wasm.encodeJsonStrToMetadatum(JSON.stringify({ | ||
'1': `0x${votingPublicKeyHex}`, | ||
'2': `0x${stakingPublicKeyHex}`, | ||
'3': `0x${rewardAddressHex}`, | ||
'4': `0x${nonce}` | ||
}), models_1.MetadataJsonSchema.BasicConversions); | ||
const generalMetadata = yield this.Wasm.GeneralTransactionMetadata.new(); | ||
yield generalMetadata.insert(yield this.Wasm.BigNum.fromStr(models_1.CatalystLabels.DATA.toString()), registrationData); | ||
const hashedMetadataStr = yield (0, hash_wasm_1.blake2b)(yield generalMetadata.toBytes(), 256); | ||
const hashedMetadata = Buffer.from(hashedMetadataStr, 'hex'); | ||
const signedHashedMetadata = yield signer(hashedMetadata); | ||
yield generalMetadata.insert(yield this.Wasm.BigNum.fromStr(models_1.CatalystLabels.SIG.toString()), yield this.Wasm.encodeJsonStrToMetadatum(JSON.stringify({ | ||
'1': `0x${signedHashedMetadata}` | ||
}), models_1.MetadataJsonSchema.BasicConversions)); | ||
const metadataList = yield this.Wasm.MetadataList.new(); | ||
yield metadataList.add(yield this.Wasm.TransactionMetadatum.fromBytes(yield generalMetadata.toBytes())); | ||
yield metadataList.add(yield this.Wasm.TransactionMetadatum.newList(yield this.Wasm.MetadataList.new())); | ||
const auxData = yield this.Wasm.AuxiliaryData.fromBytes(yield metadataList.toBytes()); | ||
const auxData = yield (0, transactions_1.generateRegistrationMetadata)(this.Wasm, votingPublicKeyHex, stakingPublicKeyHex, rewardAddressHex, nonce.toString(), signer); | ||
const protocolParams = { | ||
@@ -147,3 +131,5 @@ keyDeposit: yield this._wasmV4.BigNum.fromStr(config.keyDeposit), | ||
stakingKeyPath: stakingKeyPath, | ||
votingPublicKey: Buffer.from(yield stakingPublicKey.asBytes()).toString('hex') | ||
votingPublicKeyHex, | ||
stakingPublicKeyHex, | ||
rewardAddressHex, | ||
}, stakingPublicKey, undefined, []); | ||
@@ -271,7 +257,7 @@ return unsignedTx; | ||
if (unsignedTx.catalystRegistrationData) { | ||
const { votingPublicKey, stakingKeyPath, nonce } = unsignedTx.catalystRegistrationData; | ||
const { votingPublicKeyHex, stakingKeyPath, nonce } = unsignedTx.catalystRegistrationData; | ||
auxiliaryData = { | ||
type: ledgerjs_hw_app_cardano_1.TxAuxiliaryDataType.CATALYST_REGISTRATION, | ||
params: { | ||
votingPublicKeyHex: votingPublicKey.replace(/^0x/, ''), | ||
votingPublicKeyHex: votingPublicKeyHex.replace(/^0x/, ''), | ||
stakingPath: stakingKeyPath, | ||
@@ -317,2 +303,110 @@ rewardsDestination: { | ||
} | ||
buildLedgerSignedTx(unsignedTx, signedLedgerTx, purpose, publicKeyHex) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const key = yield this.Wasm.Bip32PublicKey.fromBytes(Buffer.from(publicKeyHex, 'hex')); | ||
const addressing = { | ||
path: [ | ||
purpose, | ||
2147485463, | ||
2147483648, | ||
], | ||
startLevel: 1, | ||
}; | ||
const isSameArray = (array1, array2) => array1.length === array2.length && array1.every((value, index) => value === array2[index]); | ||
const findWitness = (path) => { | ||
for (const witness of signedLedgerTx.witnesses) { | ||
if (isSameArray(witness.path, path)) { | ||
return witness.witnessSignatureHex; | ||
} | ||
} | ||
throw new Error(`buildSignedTransaction no witness for ${JSON.stringify(path)}`); | ||
}; | ||
const keyLevel = addressing.startLevel + addressing.path.length - 1; | ||
const witSet = yield this.Wasm.TransactionWitnessSet.new(); | ||
const bootstrapWitnesses = []; | ||
const vkeys = []; | ||
// Note: Ledger removes duplicate witnesses | ||
// but there may be a one-to-many relationship | ||
// ex: same witness is used in both a bootstrap witness and a vkey witness | ||
const seenVKeyWit = new Set(); | ||
const seenBootstrapWit = new Set(); | ||
for (const utxo of unsignedTx.senderUtxos) { | ||
(0, ledger_1.verifyFromBip44Root)(utxo.addressing); | ||
const witness = findWitness(utxo.addressing.path); | ||
const addressKey = yield (0, addresses_1.derivePublicByAddressing)(utxo.addressing, { | ||
level: keyLevel, | ||
key, | ||
}); | ||
if (yield this.Wasm.ByronAddress.isValid(utxo.receiver)) { | ||
const byronAddr = yield this.Wasm.ByronAddress.fromBase58(utxo.receiver); | ||
const bootstrapWit = yield wasm_contract_1.BootstrapWitness.new(yield this.Wasm.Vkey.new(yield addressKey.toRawKey()), yield this.Wasm.Ed25519Signature.fromBytes(Buffer.from(witness, 'hex')), yield addressKey.chaincode(), yield byronAddr.attributes()); | ||
const asString = Buffer.from(yield bootstrapWit.toBytes()).toString('hex'); | ||
if (seenBootstrapWit.has(asString)) { | ||
continue; | ||
} | ||
seenBootstrapWit.add(asString); | ||
bootstrapWitnesses.push(bootstrapWit); | ||
continue; | ||
} | ||
const vkeyWit = yield wasm_contract_1.Vkeywitness.new(yield this.Wasm.Vkey.new(yield addressKey.toRawKey()), yield this.Wasm.Ed25519Signature.fromBytes(Buffer.from(witness, 'hex'))); | ||
const asString = Buffer.from(yield vkeyWit.toBytes()).toString('hex'); | ||
if (seenVKeyWit.has(asString)) { | ||
continue; | ||
} | ||
seenVKeyWit.add(asString); | ||
vkeys.push(vkeyWit); | ||
} | ||
// add any staking key needed | ||
for (const witness of signedLedgerTx.witnesses) { | ||
const addressing = { | ||
path: witness.path, | ||
startLevel: 1, | ||
}; | ||
(0, ledger_1.verifyFromBip44Root)(addressing); | ||
if (witness.path[3] === 2) { | ||
const stakingKey = yield (0, addresses_1.derivePublicByAddressing)(addressing, { | ||
level: keyLevel, | ||
key, | ||
}); | ||
const vkeyWit = yield wasm_contract_1.Vkeywitness.new(yield this.Wasm.Vkey.new(yield stakingKey.toRawKey()), yield this.Wasm.Ed25519Signature.fromBytes(Buffer.from(witness.witnessSignatureHex, 'hex'))); | ||
const asString = Buffer.from(yield vkeyWit.toBytes()).toString('hex'); | ||
if (seenVKeyWit.has(asString)) { | ||
continue; | ||
} | ||
seenVKeyWit.add(asString); | ||
vkeys.push(vkeyWit); | ||
} | ||
} | ||
if (bootstrapWitnesses.length > 0) { | ||
const bootstrapWitWasm = yield this.Wasm.BootstrapWitnesses.new(); | ||
for (const bootstrapWit of bootstrapWitnesses) { | ||
yield bootstrapWitWasm.add(bootstrapWit); | ||
} | ||
yield witSet.setBootstraps(bootstrapWitWasm); | ||
} | ||
if (vkeys.length > 0) { | ||
const vkeyWitWasm = yield this.Wasm.Vkeywitnesses.new(); | ||
for (const vkey of vkeys) { | ||
yield vkeyWitWasm.add(vkey); | ||
} | ||
yield witSet.setVkeys(vkeyWitWasm); | ||
} | ||
let auxData = unsignedTx.auxiliaryData; | ||
if (unsignedTx.catalystRegistrationData) { | ||
auxData = yield (0, transactions_1.generateRegistrationMetadata)(this.Wasm, unsignedTx.catalystRegistrationData.votingPublicKeyHex, unsignedTx.catalystRegistrationData.stakingPublicKeyHex, unsignedTx.catalystRegistrationData.rewardAddressHex, unsignedTx.catalystRegistrationData.nonce, () => __awaiter(this, void 0, void 0, function* () { var _a, _b; return (_b = (_a = signedLedgerTx.auxiliaryDataSupplement) === null || _a === void 0 ? void 0 : _a.catalystRegistrationSignatureHex) !== null && _b !== void 0 ? _b : ''; })); | ||
} | ||
// TODO: handle script witnesses | ||
const signedTx = yield this.Wasm.Transaction.new(unsignedTx.txBody, witSet, auxData); | ||
const id = yield signedTx | ||
.body() | ||
.then((txBody) => this.Wasm.hashTransaction(txBody)) | ||
.then((hash) => hash.toBytes()) | ||
.then((bytes) => Buffer.from(bytes).toString('hex')); | ||
const encodedTx = yield signedTx.toBytes(); | ||
return { | ||
id, | ||
encodedTx, | ||
}; | ||
}); | ||
} | ||
getBalanceForStakingCredentials(utxos) { | ||
@@ -319,0 +413,0 @@ return __awaiter(this, void 0, void 0, function* () { |
@@ -94,3 +94,5 @@ import * as WasmContract from './wasm-contract'; | ||
export declare type CatalystRegistrationData = { | ||
votingPublicKey: string; | ||
votingPublicKeyHex: string; | ||
stakingPublicKeyHex: string; | ||
rewardAddressHex: string; | ||
stakingKeyPath: number[]; | ||
@@ -97,0 +99,0 @@ nonce: string; |
@@ -0,3 +1,4 @@ | ||
import { Addressing } from '../models'; | ||
import * as WasmContract from '../wasm-contract'; | ||
import { StakeCredential } from '../wasm-contract'; | ||
import { Bip32PublicKey, StakeCredential } from '../wasm-contract'; | ||
export declare function normalizeToAddress(wasm: WasmContract.WasmModuleProxy, addr: string): Promise<WasmContract.Address | undefined>; | ||
@@ -9,1 +10,5 @@ export declare function toHexOrBase58(wasm: WasmContract.WasmModuleProxy, address: WasmContract.Address): Promise<string>; | ||
export declare function addrContainsAccountKey(wasm: WasmContract.WasmModuleProxy, address: string, targetAccountKey: StakeCredential, acceptTypeMismatch: boolean): Promise<boolean>; | ||
export declare const derivePublicByAddressing: (addressing: Addressing, startingFrom: { | ||
key: Bip32PublicKey; | ||
level: number; | ||
}) => Promise<WasmContract.Bip32PublicKey>; |
@@ -12,3 +12,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.addrContainsAccountKey = exports.filterAddressesByStakingKey = exports.toHexOrBase58 = exports.normalizeToAddress = void 0; | ||
exports.derivePublicByAddressing = exports.addrContainsAccountKey = exports.filterAddressesByStakingKey = exports.toHexOrBase58 = exports.normalizeToAddress = void 0; | ||
function normalizeToAddress(wasm, addr) { | ||
@@ -84,1 +84,12 @@ return __awaiter(this, void 0, void 0, function* () { | ||
exports.addrContainsAccountKey = addrContainsAccountKey; | ||
const derivePublicByAddressing = (addressing, startingFrom) => __awaiter(void 0, void 0, void 0, function* () { | ||
if (startingFrom.level + 1 < addressing.startLevel) { | ||
throw new Error('derivePublicByAddressing: keyLevel < startLevel'); | ||
} | ||
let derivedKey = startingFrom.key; | ||
for (let i = startingFrom.level - addressing.startLevel + 1; i < addressing.path.length; i++) { | ||
derivedKey = yield derivedKey.derive(addressing.path[i]); | ||
} | ||
return derivedKey; | ||
}); | ||
exports.derivePublicByAddressing = derivePublicByAddressing; |
@@ -15,3 +15,3 @@ "use strict"; | ||
const addresses_1 = require("./addresses"); | ||
const bech32_1 = require("bech32"); | ||
// import { bech32 } from 'bech32' | ||
const ledgerjs_hw_app_cardano_1 = require("@cardano-foundation/ledgerjs-hw-app-cardano"); | ||
@@ -29,26 +29,27 @@ const transformToLedgerInputs = (inputs) => { | ||
exports.transformToLedgerInputs = transformToLedgerInputs; | ||
const areAddressesTheSame = (wasm, addr1, addr2) => __awaiter(void 0, void 0, void 0, function* () { | ||
const addrToHex = (addr) => __awaiter(void 0, void 0, void 0, function* () { | ||
const addrBech32 = bech32_1.bech32.decodeUnsafe(addr, addr.length); | ||
let hex; | ||
if (addrBech32) { | ||
hex = Buffer.from(bech32_1.bech32.fromWords(addrBech32.words)).toString('hex'); | ||
} | ||
else if (yield wasm.ByronAddress.isValid(addr)) { | ||
hex = Buffer.from(yield wasm.ByronAddress.fromBase58(addr) | ||
.then(b => b.toAddress()) | ||
.then(a => a.toBytes())).toString('hex'); | ||
} | ||
else if (/^[0-9a-f]+$/i.test(addr.toLowerCase())) { | ||
hex = addr; | ||
} | ||
else { | ||
throw new Error('compareAddresses::addrToHex: unexpected address format - should be either hex, base58 (Byron) or bech32'); | ||
} | ||
return hex.toLowerCase(); | ||
}); | ||
const addr1Hex = yield addrToHex(addr1); | ||
const addr2Hex = yield addrToHex(addr2); | ||
return addr1Hex === addr2Hex; | ||
}); | ||
// const areAddressesTheSame = async ( | ||
// wasm: WasmContract.WasmModuleProxy, | ||
// addr1: string, | ||
// addr2: string | ||
// ) => { | ||
// const addrToHex = async (addr: string) => { | ||
// const addrBech32 = bech32.decodeUnsafe(addr, addr.length) | ||
// let hex: string | ||
// if (addrBech32) { | ||
// hex = Buffer.from(bech32.fromWords(addrBech32.words)).toString('hex') | ||
// } else if (await wasm.ByronAddress.isValid(addr)) { | ||
// hex = Buffer.from(await wasm.ByronAddress.fromBase58(addr) | ||
// .then(b => b.toAddress()) | ||
// .then(a => a.toBytes())).toString('hex') | ||
// } else if (/^[0-9a-f]+$/i.test(addr.toLowerCase())) { | ||
// hex = addr | ||
// } else { | ||
// throw new Error('compareAddresses::addrToHex: unexpected address format - should be either hex, base58 (Byron) or bech32') | ||
// } | ||
// return hex.toLowerCase() | ||
// } | ||
// const addr1Hex = await addrToHex(addr1) | ||
// const addr2Hex = await addrToHex(addr2) | ||
// return addr1Hex === addr2Hex | ||
// } | ||
const transformToLedgerOutputs = (wasm, request) => __awaiter(void 0, void 0, void 0, function* () { | ||
@@ -60,3 +61,3 @@ const result = []; | ||
const jsAddr = yield (0, addresses_1.toHexOrBase58)(wasm, yield output.address()); | ||
const changeAddr = request.changeAddrs.find(change => areAddressesTheSame(wasm, jsAddr, change.address)); | ||
const changeAddr = request.changeAddrs.find(change => jsAddr === change.address); | ||
if (changeAddr != null) { | ||
@@ -63,0 +64,0 @@ (0, exports.verifyFromBip44Root)(changeAddr.addressing); |
@@ -0,1 +1,2 @@ | ||
/// <reference types="node" /> | ||
import * as WasmContract from '../wasm-contract'; | ||
@@ -20,1 +21,2 @@ import { AddressingAddress, CardanoAddressedUtxo, RegistrationStatus, RemoteUnspentOutput, Token } from '../models'; | ||
export declare function getDifferenceAfterTx(wasm: WasmContract.WasmModuleProxy, senderUtxos: CardanoAddressedUtxo[], txBody: WasmContract.TransactionBody, allUtxos: RemoteUnspentOutput[], stakingKey: PublicKey, defaultToken: Token): Promise<MultiToken>; | ||
export declare const generateRegistrationMetadata: (wasm: WasmContract.WasmModuleProxy, votingPublicKeyHex: string, stakingPublicKeyHex: string, rewardAddressHex: string, nonce: string, signer: (hashedMetadata: Buffer) => Promise<string>) => Promise<WasmContract.AuxiliaryData>; |
@@ -12,3 +12,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getDifferenceAfterTx = exports.createDelegationCertificate = exports.isBigNumZero = exports.cardanoValueFromRemoteFormat = exports.addUtxoInput = exports.minRequiredForChange = void 0; | ||
exports.generateRegistrationMetadata = exports.getDifferenceAfterTx = exports.createDelegationCertificate = exports.isBigNumZero = exports.cardanoValueFromRemoteFormat = exports.addUtxoInput = exports.minRequiredForChange = void 0; | ||
const models_1 = require("../models"); | ||
@@ -20,2 +20,3 @@ const addresses_1 = require("./addresses"); | ||
const multi_token_1 = require("../multi-token"); | ||
const hash_wasm_1 = require("hash-wasm"); | ||
function minRequiredForChange(wasm, txBuilder, changeAdaAddr, value, protocolParams) { | ||
@@ -213,1 +214,23 @@ return __awaiter(this, void 0, void 0, function* () { | ||
exports.getDifferenceAfterTx = getDifferenceAfterTx; | ||
const generateRegistrationMetadata = (wasm, votingPublicKeyHex, stakingPublicKeyHex, rewardAddressHex, nonce, signer) => __awaiter(void 0, void 0, void 0, function* () { | ||
const registrationData = yield wasm.encodeJsonStrToMetadatum(JSON.stringify({ | ||
'1': `0x${votingPublicKeyHex}`, | ||
'2': `0x${stakingPublicKeyHex}`, | ||
'3': `0x${rewardAddressHex}`, | ||
'4': `0x${nonce}` | ||
}), models_1.MetadataJsonSchema.BasicConversions); | ||
const generalMetadata = yield wasm.GeneralTransactionMetadata.new(); | ||
yield generalMetadata.insert(yield wasm.BigNum.fromStr(models_1.CatalystLabels.DATA.toString()), registrationData); | ||
const hashedMetadataStr = yield (0, hash_wasm_1.blake2b)(yield generalMetadata.toBytes(), 256); | ||
const hashedMetadata = Buffer.from(hashedMetadataStr, 'hex'); | ||
const signedHashedMetadata = yield signer(hashedMetadata); | ||
yield generalMetadata.insert(yield wasm.BigNum.fromStr(models_1.CatalystLabels.SIG.toString()), yield wasm.encodeJsonStrToMetadatum(JSON.stringify({ | ||
'1': `0x${signedHashedMetadata}` | ||
}), models_1.MetadataJsonSchema.BasicConversions)); | ||
const metadataList = yield wasm.MetadataList.new(); | ||
yield metadataList.add(yield wasm.TransactionMetadatum.fromBytes(yield generalMetadata.toBytes())); | ||
yield metadataList.add(yield wasm.TransactionMetadatum.newList(yield wasm.MetadataList.new())); | ||
const auxData = yield wasm.AuxiliaryData.fromBytes(yield metadataList.toBytes()); | ||
return auxData; | ||
}); | ||
exports.generateRegistrationMetadata = generateRegistrationMetadata; |
{ | ||
"name": "@emurgo/yoroi-lib-core", | ||
"version": "0.8.8-alpha.50", | ||
"version": "0.8.9-alpha.51", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
298732
5253