|  | 
|  | 1 | +import * as Core from "craminiumlab-wallet-core"; | 
|  | 2 | +import { TW } from "craminiumlab-wallet-core"; | 
|  | 3 | +import { initWasm } from "craminiumlab-wallet-core"; | 
|  | 4 | +import Long from "long"; | 
|  | 5 | +export { initWasm }; | 
|  | 6 | +export class WalletCoreWrapper { | 
|  | 7 | +    constructor(core, _tw = TW) { | 
|  | 8 | +        const { CoinType, HexCoding, AnySigner, TransactionCompiler, DataVector, PrivateKey } = core; | 
|  | 9 | +        this.CoinType = CoinType; | 
|  | 10 | +        this.HexCoding = HexCoding; | 
|  | 11 | +        this.AnySigner = AnySigner; | 
|  | 12 | +        this.TransactionCompiler = TransactionCompiler; | 
|  | 13 | +        this.DataVector = DataVector; | 
|  | 14 | +        this.PrivateKey = PrivateKey; | 
|  | 15 | +        this.TW = _tw; | 
|  | 16 | +    } | 
|  | 17 | +    static async init() { | 
|  | 18 | +        const core = await Core.initWasm(); | 
|  | 19 | +        return new WalletCoreWrapper(core); | 
|  | 20 | +    } | 
|  | 21 | +    encodeHex(data) { | 
|  | 22 | +        return this.HexCoding.encode(data); | 
|  | 23 | +    } | 
|  | 24 | +    createWithData(hexStr) { | 
|  | 25 | +        const data = this.HexCoding.decode(hexStr); | 
|  | 26 | +        const privateKey = this.PrivateKey.createWithData(data); | 
|  | 27 | +        return privateKey.data(); | 
|  | 28 | +    } | 
|  | 29 | +    anySign(input, coin) { | 
|  | 30 | +        return this.AnySigner.sign(input, coin); | 
|  | 31 | +    } | 
|  | 32 | +    preImageHashes(coin, txInput) { | 
|  | 33 | +        const result = this.TransactionCompiler.preImageHashes(coin, txInput); | 
|  | 34 | +        console.log('RESULT', result, result.length); | 
|  | 35 | +        return result; | 
|  | 36 | +    } | 
|  | 37 | +    compileWithSignatures(coinType, txInputData, signatures, publicKeys) { | 
|  | 38 | +        const sigs = signatures.map((s) => this.HexCoding.decode(s)); | 
|  | 39 | +        const pubs = publicKeys.map((p) => this.HexCoding.decode(p)); | 
|  | 40 | +        const sigVec = this.makeDataVector(sigs); | 
|  | 41 | +        const pubVec = this.makeDataVector(pubs); | 
|  | 42 | +        return this.TransactionCompiler.compileWithSignatures(coinType, txInputData, sigVec, pubVec); | 
|  | 43 | +    } | 
|  | 44 | +    makeDataVector(items) { | 
|  | 45 | +        const vec = this.DataVector.create(); | 
|  | 46 | +        items.forEach((i) => vec.add(i)); | 
|  | 47 | +        return vec; | 
|  | 48 | +    } | 
|  | 49 | +    buildAptosUnsignedTx(jsonStr) { | 
|  | 50 | +        const req = JSON.parse(jsonStr); | 
|  | 51 | +        const input = { | 
|  | 52 | +            chainId: req.chainId, | 
|  | 53 | +            sender: req.sender, | 
|  | 54 | +            sequenceNumber: Long.fromString(String(req.sequenceNumber)), | 
|  | 55 | +            expirationTimestampSecs: Long.fromString(String(req.ttl)), | 
|  | 56 | +            gasUnitPrice: Long.fromString(String(req.gasUnitPrice)), | 
|  | 57 | +            maxGasAmount: Long.fromString(String(req.maxGasAmount)), | 
|  | 58 | +            transfer: this.TW.Aptos.Proto.TransferMessage.create({ | 
|  | 59 | +                to: req.toAddress, | 
|  | 60 | +                amount: Long.fromString(String(req.amount)), | 
|  | 61 | +            }), | 
|  | 62 | +        }; | 
|  | 63 | +        return this.TW.Aptos.Proto.SigningInput.encode(input).finish(); | 
|  | 64 | +    } | 
|  | 65 | +    buildAptosUnsignedMessage(txInput) { | 
|  | 66 | +        const preimage = this.preImageHashes(this.CoinType.aptos, txInput); | 
|  | 67 | +        const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage); | 
|  | 68 | +        return out.data; | 
|  | 69 | +    } | 
|  | 70 | +    buildCardanoUnsignedTx(jsonStr) { | 
|  | 71 | +        const req = JSON.parse(jsonStr); | 
|  | 72 | +        const transfer = this.TW.Cardano.Proto.Transfer.create({ | 
|  | 73 | +            toAddress: req.toAddress, | 
|  | 74 | +            changeAddress: req.changeAddress, | 
|  | 75 | +            amount: req.amount, | 
|  | 76 | +            useMaxAmount: false, | 
|  | 77 | +        }); | 
|  | 78 | +        const utxos = req.utxos.map((utxo) => this.TW.Cardano.Proto.TxInput.create({ | 
|  | 79 | +            outPoint: this.TW.Cardano.Proto.OutPoint.create({ | 
|  | 80 | +                txHash: utxo.tx_hash, | 
|  | 81 | +                outputIndex: utxo.output_index, | 
|  | 82 | +            }), | 
|  | 83 | +            address: utxo.address, | 
|  | 84 | +            amount: utxo.amount, | 
|  | 85 | +            tokenAmount: null | 
|  | 86 | +        })); | 
|  | 87 | +        const input = { | 
|  | 88 | +            transferMessage: transfer, | 
|  | 89 | +            utxos: utxos, | 
|  | 90 | +            ttl: new Long(req.ttl), | 
|  | 91 | +        }; | 
|  | 92 | +        return this.TW.Cardano.Proto.SigningInput.encode(input).finish(); | 
|  | 93 | +    } | 
|  | 94 | +    buildCardanoUnsignedMessage(txInput) { | 
|  | 95 | +        const preimage = this.preImageHashes(this.CoinType.cardano, txInput); | 
|  | 96 | +        const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage); | 
|  | 97 | +        return out.dataHash; | 
|  | 98 | +    } | 
|  | 99 | +    buildCosmosUnsignedTx(jsonStr) { | 
|  | 100 | +        const req = JSON.parse(jsonStr); | 
|  | 101 | +        console.log(req); | 
|  | 102 | +        const message = this.TW.Cosmos.Proto.Message.create({ | 
|  | 103 | +            sendCoinsMessage: this.TW.Cosmos.Proto.Message.Send.create({ | 
|  | 104 | +                fromAddress: req.fromAddress, | 
|  | 105 | +                toAddress: req.toAddress, | 
|  | 106 | +                amounts: [ | 
|  | 107 | +                    this.TW.Cosmos.Proto.Amount.create({ | 
|  | 108 | +                        denom: req.denom, | 
|  | 109 | +                        amount: req.amount, | 
|  | 110 | +                    }), | 
|  | 111 | +                ] | 
|  | 112 | +            }) | 
|  | 113 | +        }); | 
|  | 114 | +        const input = { | 
|  | 115 | +            chainId: req.chainId, | 
|  | 116 | +            accountNumber: req.accountNumber, | 
|  | 117 | +            sequence: req.sequence, | 
|  | 118 | +            messages: [message], | 
|  | 119 | +            memo: req.memo, | 
|  | 120 | +            publicKey: this.HexCoding.decode(req.publicKey), | 
|  | 121 | +            fee: this.TW.Cosmos.Proto.Fee.create({ | 
|  | 122 | +                gas: req.gas, | 
|  | 123 | +                amounts: [ | 
|  | 124 | +                    this.TW.Cosmos.Proto.Amount.create({ | 
|  | 125 | +                        denom: req.gasDenom, | 
|  | 126 | +                        amount: req.gasAmount, | 
|  | 127 | +                    }) | 
|  | 128 | +                ] | 
|  | 129 | +            }) | 
|  | 130 | +        }; | 
|  | 131 | +        return this.TW.Cosmos.Proto.SigningInput.encode(input).finish(); | 
|  | 132 | +    } | 
|  | 133 | +    buildCosmosUnsignedMessage(txInput) { | 
|  | 134 | +        const preimage = this.preImageHashes(this.CoinType.cosmos, txInput); | 
|  | 135 | +        const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage); | 
|  | 136 | +        return out.dataHash; | 
|  | 137 | +    } | 
|  | 138 | +    buildNearUnsignedTx(jsonStr) { | 
|  | 139 | +        const req = JSON.parse(jsonStr); | 
|  | 140 | +        console.log(req); | 
|  | 141 | +        const action = this.TW.NEAR.Proto.Action.create({ | 
|  | 142 | +            transfer: this.TW.NEAR.Proto.Transfer.create({ | 
|  | 143 | +                deposit: this.HexCoding.decode(req.transferAmount) | 
|  | 144 | +            }) | 
|  | 145 | +        }); | 
|  | 146 | +        const input = { | 
|  | 147 | +            signerId: req.signerId, | 
|  | 148 | +            nonce: Long.fromString(String(req.nonce)), | 
|  | 149 | +            receiverId: req.receiverId, | 
|  | 150 | +            blockHash: this.HexCoding.decode(req.blockHash), | 
|  | 151 | +            actions: [action], | 
|  | 152 | +            publicKey: this.HexCoding.decode(req.publicKey), | 
|  | 153 | +        }; | 
|  | 154 | +        console.log(input); | 
|  | 155 | +        return this.TW.NEAR.Proto.SigningInput.encode(input).finish(); | 
|  | 156 | +    } | 
|  | 157 | +    buildNearUnsignedMessage(txInput) { | 
|  | 158 | +        const preimage = this.preImageHashes(this.CoinType.near, txInput); | 
|  | 159 | +        const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage); | 
|  | 160 | +        return out.dataHash; | 
|  | 161 | +    } | 
|  | 162 | +    buildStellarUnsignedTx(jsonStr) { | 
|  | 163 | +        const req = JSON.parse(jsonStr); | 
|  | 164 | +        console.log(req); | 
|  | 165 | +        const opPayment = TW.Stellar.Proto.OperationPayment.create({ | 
|  | 166 | +            destination: req.destination, | 
|  | 167 | +            amount: Long.fromString(String(req.amount)), | 
|  | 168 | +        }); | 
|  | 169 | +        const memoText = this.TW.Stellar.Proto.MemoText.create({ | 
|  | 170 | +            text: req.memoText | 
|  | 171 | +        }); | 
|  | 172 | +        const input = { | 
|  | 173 | +            fee: req.fee, | 
|  | 174 | +            account: req.account, | 
|  | 175 | +            passphrase: 'Public Global Stellar Network ; September 2015', | 
|  | 176 | +            sequence: Long.fromString(String(req.sequence)), | 
|  | 177 | +            opPayment: opPayment, | 
|  | 178 | +            memoText: memoText | 
|  | 179 | +        }; | 
|  | 180 | +        console.log(input); | 
|  | 181 | +        return this.TW.Stellar.Proto.SigningInput.encode(input).finish(); | 
|  | 182 | +    } | 
|  | 183 | +    buildStellarUnsignedMessage(txInput) { | 
|  | 184 | +        const preimage = this.preImageHashes(this.CoinType.stellar, txInput); | 
|  | 185 | +        const out = this.TW.TxCompiler.Proto.PreSigningOutput.decode(preimage); | 
|  | 186 | +        return out.dataHash; | 
|  | 187 | +    } | 
|  | 188 | +    marshalInput(input) { | 
|  | 189 | +        return input.finish(); | 
|  | 190 | +    } | 
|  | 191 | +} | 
0 commit comments