Ledger device: Invalid data received (0x6a80) #404 - javascript

When I try to transfer chainlink on goerli network with ledger I get this error:
TransportStatusError: Ledger device: Invalid data received (0x6a80)
contractAddress: 0x326c977e6efc84e512bb9c30f76e30c160ed06fb
I can send dai but when i wanna send chainlink I get this error.
My environment:
windows
Device (Nano S)
I'm using this libraries :
{
"#ledgerhq/hw-app-eth": "^6.31.0",
"#ledgerhq/hw-transport-webhid": "^6.27.11",
}
this is the transaction:
{
"gasLimit": "0x8792",
"to": "0x326c977e6efc84e512bb9c30f76e30c160ed06fb",
"data": "0xa9059cbb00000000000000000000000014be4dc6e2e7562eb80f20fa4f8dbd072b8a56670000000000000000000000000000000000000000000000000de0b6b3a7640000",
"nonce": "0x4b",
"chainId": "0x5",
"type": 2,
"maxPriorityFeePerGas": "0x59682f00",
"maxFeePerGas": "0x101f0aef2a"
}
and this is the code
const web3 = new Web3(getNet(secondAddress));
const abi = getAbi8();
const contract = new web3.eth.Contract(abi, contractAddress.toLowerCase(), { from: wallet.address });
const decimals = await contract.methods.decimals().call();
const value = amount * (10 ** decimals);
const gas = await contract.methods.transfer(toAddress.toLowerCase(), String(value)).estimateGas({ from: wallet.address });
const gasPrice = await web3.eth.getGasPrice();
const transactionCount = await web3.eth.getTransactionCount(wallet.address, 'pending');
const chainId = await getChainId();
let feeData = await getFeeData(secondAddress);
let transaction = {
gasLimit: ethers.utils.hexlify(gas),
to: contractAddress,
data: (contract.methods.transfer(toAddress.toLowerCase(), web3.utils.toHex(value)).encodeABI()),
gasPrice: web3.utils.toHex(Math.floor(gasPrice * 1.3)),
nonce: web3.utils.toHex(transactionCount),
chainId: web3.utils.toHex(chainId),
type: 2
}
if (feeData.maxPriorityFeePerGas) {
transaction.maxPriorityFeePerGas = web3.utils.toHex(feeData.maxPriorityFeePerGas);
delete transaction.gasPrice;
}
if (feeData.maxFeePerGas) {
transaction.maxFeePerGas = web3.utils.toHex(feeData.maxFeePerGas);
}
if (!feeData.maxPriorityFeePerGas) {
transaction.gasPrice = feeData.gasPrice;
}
const gasData = localStorage.getItem('user-gas');
if (gasData) {
const _gasData = JSON.parse(gasData);
if (_gasData.gasLimit) {
transaction.gas = web3.utils.toHex(_gasData?.gasLimit);
}
if (_gasData.gasPrice && !feeData.maxPriorityFeePerGas) {
transaction.gasPrice = web3.utils.toHex(_gasData?.gasPrice * 10 ** 9);
}
if (_gasData.maxGas) {
transaction.maxFeePerGas = web3.utils.toHex(_gasData?.maxGas * 10 ** 9);
}
if (_gasData.maxPriorityFee) {
transaction.maxPriorityFeePerGas = web3.utils.toHex(_gasData?.maxPriorityFee * 10 ** 9);
}
}
if (customGas && !feeData.maxFeePerGas) {
/// convert gasprice from gwei to wei
transaction.gasPrice = web3.utils.toHex(customGas.gasPrice);
}
else if (customGas && feeData.maxFeePerGas) {
transaction.maxPriorityFeePerGas = web3.utils.toHex(customGas.priorityFeePerGas);
}
console.log(transaction)
const serializedTx = ethers.utils.serializeTransaction(transaction).slice(2)
await forgetDevice()
let appEth
try {
let transport = await Transport.request();
appEth = await new Eth(transport)
}
catch (err) {
throw 'Unable to connect with your ledger device , make sure your device is connected and keep the app open';
}
let path = `/44'/60'/${wallet.id - 1}'/0/0`;
if (wallet.ledgerType == 3) {
path = `44'/60'/0'/0/${wallet.id - 1}`;
}
else if (wallet.ledgerType == 2) {
path = `m/44'/60'/0'/${wallet.id - 1}`;
}
const resolution = await ledgerService.resolveTransaction(
serializedTx,
appEth.loadConfig,
{
externalPlugins: true,
erc20: true,
nft: true,
}
);
const signature = await appEth.signTransaction(path, serializedTx, resolution)
signature.r = "0x" + signature.r;
signature.s = "0x" + signature.s;
signature.v = parseInt("0x" + signature.v);
signature.from = wallet.address;
const signedTx = ethers.utils.serializeTransaction(transaction, signature);
return new Promise((resolve, reject) => {
web3.eth.sendSignedTransaction(signedTx, (err, hash) => {
if (!err) {
resolve(hash);
}
else {
reject(err)
}
})
})

Related

SQS messages are coming back to DLQ?

I have written the following script to move messages from DLQ to MainQ.
'use strict'
import { readFileSync } from "fs";
import {
DeleteMessageCommand,
SQSClient,
GetQueueUrlCommand,
SendMessageCommand,
ReceiveMessageCommand,
GetQueueAttributesCommand
} from '#aws-sdk/client-sqs';
const REGION = 'us-east-1';
const sqs = new SQSClient({ region: REGION });
async function getMessageCount(queueUrl) {
const params = { AttributeNames: ['ApproximateNumberOfMessages'], QueueUrl: queueUrl };
try {
const data = await sqs.send(new GetQueueAttributesCommand(params));
// console.log(data);
return data.Attributes.ApproximateNumberOfMessages;
} catch (err) {
console.error(err);
}
}
async function reprocessMessages(dlqQueue, mainQueue) {
try {
const response1 = await sqs.send(new GetQueueUrlCommand({ QueueName: dlqQueue }));
const response2 = await sqs.send(new GetQueueUrlCommand({ QueueName: mainQueue }));
const dlqQueueUrl = response1.QueueUrl;
const mainQueueUrl = response2.QueueUrl;
const messageCount = await getMessageCount(dlqQueueUrl);
console.log(`Message count ${messageCount}`);
const visibiltyTimeout = Math.ceil(messageCount / 100) * 2;
let count = 0;
while (count <= 50) {
count += await moveMessage(mainQueueUrl, dlqQueueUrl, 5);
}
console.log(`Moved ${count} messages`);
return "Completed";
} catch (err) {
console.error(err);
}
}
async function moveMessage(mainQueueUrl, dlqQueueUrl, visibiltyTimeout) {
try {
const receiveMessageParams = {
MaxNumberOfMessages: 9,
MessageAttributeNames: ['All'],
QueueUrl: dlqQueueUrl,
VisibilityTimeout: visibiltyTimeout,
WaitTimeSeconds: 1
};
const receiveData = await sqs.send(new ReceiveMessageCommand(receiveMessageParams));
// console.log(receiveData);
if (!receiveData.Messages) {
// console.log("finished");
return 0;
}
const messages = [];
receiveData.Messages.forEach(msg => {
messages.push({ id: msg.MessageId, msgAttributes: msg.MessageAttributes, body: msg.Body, receiptHandle: msg.ReceiptHandle });
});
const sendMsg = async ({ id, msgAttributes, body, _ }) => {
const sendMessageParams = {
MessageId: id,
MessageAttributes: msgAttributes,
MessageBody: body,
QueueUrl: mainQueueUrl
};
const sentData = await sqs.send(new SendMessageCommand(sendMessageParams));
// console.log("Success, message sent. MessageID: ", sentData.MessageId);
return 'Success';
};
const deleteMsg = async ({ id, receiptHandle }) => {
const deleteMessageParams = {
MessageId: id,
QueueUrl: dlqQueueUrl,
ReceiptHandle: receiptHandle
};
const deleteData = await sqs.send(new DeleteMessageCommand(deleteMessageParams));
// console.log("Message deleted", deleteData);
return 'Deleted';
};
const sent = await Promise.all(messages.map(sendMsg));
console.log(sent);
const deleted = await Promise.all(messages.map(deleteMsg));
console.log(deleted);
console.log(sent.length);
return sent.length;
// return 1;
} catch (err) {
console.log(err);
}
}
function main() {
const queues = readFileSync('queues.txt', 'utf8').split('\n');
queues.map(async elem => {
const [dlqQueue, mainQueue] = elem.split(' ');
console.log(`Moving messages from ${dlqQueue} to ${mainQueue}`);
const response = await reprocessMessages(dlqQueue, mainQueue);
console.log(response);
});
}
main()
This script moves the message from DLQ to MainQ and shows the new count of the DLQ decreased by the number of messages. However, when those messages in MainQ fail they again come back to DLQ thus keeping the count same as before reprocessing. But, after a while I see that the messages in DLQ increase by the same amount as the number of messages processed.
For instance for the below case I moved 54 messages and the screenshots for the queue counts are attached after each stage.

Why I am getting error of "Insufficient Margin" on placing Binance API futures order?

import { createRequire, runMain } from "module";
const require = createRequire(import.meta.url);
const Binance = require("node-binance-api");
const binance = new Binance().options({
APIKEY: "<API-KEY>",
APISECRET: "<API-SECRET>",
useServerTime: true,
});
async function Trade(interval, { symbol, EnterAt, Type }) {
try {
if ((interval == "1h" || interval == "4h") & (Type == "SHORT")) {
let response = await binance.futuresBalance();
let pair = response.filter((el) => el.asset == "USDT");
const leverage = 6;
let quantity = Math.round(
((parseFloat(pair[0].availableBalance) * leverage) / EnterAt).toFixed(2)
);
let takeProfit = EnterAt - (EnterAt * 0.6) / 100;
let stopMarket = EnterAt + (EnterAt * 1.1) / 100;
// Adjust leverage
let adjustLeverage = await binance.futuresLeverage(symbol, leverage);
// Futures Market Sell
let short = await binance.futuresMarketSell(symbol, quantity);
// Set TAKE PROFIT
let TP = await binance.futuresOrder("BUY", symbol, quantity, false, {
type: "TAKE_PROFIT_MARKET",
stopPrice: takeProfit.toFixed(2),
closePosition: true,
});
// Set STOP LOSS
let SL = await binance.futuresOrder("BUY", symbol, quantity, false, {
type: "STOP_MARKET",
workingType: "MARK_PRICE",
stopPrice: stopMarket.toFixed(2),
closePosition: true,
});
console.log("SHORT: ", short);
console.log("TP: ", TP);
console.log("SL: ", SL);
}
} catch (error) {
console.log(error.message);
}
}
function wrapUpTrade(interval, { symbol, EnterAt }) {
binance.useServerTime(() =>
Trade(interval, { symbol: symbol, EnterAt: EnterAt })
);
}
// wrapUpTrade("1h", { symbol: "XRPUSDT", shortAt: 0.774 });
export { wrapUpTrade };
This code is only for futures Market Sell (sell short). Why do I get an error of Insufficient Margin on placing an order of coin having a price of $4.5 and I have $2.9 in my wallet? On leveraging(6x) I will be having (6 * $2.9 = $17.4) that is greater than $4.5 still I get the error of Insufficient error. Function Trade takes some arguments:
Interval: "4h" or "1h"
symbol: "PAIR/(USDT)" // BTCUSDT, ETHUSDT
EnterAt: It is used to calculate Stoploss and TakeProfit. The type is int.
Type: "SHORT", means futuresSell

send data file to another computer with RTCPeerConnection?

how can I change the code so that the computer1 code connects to the code of computer 2(computer1 and computer2 are not the same computers but on the same network).
It works locally but not when it's two different computers
computer 1 and computer2 code that is used for the connection is defined below
this is the code that does the networking on computer1
async function createConnection() {
abortButton.disabled = false;
sendFileButton.disabled = true;
localConnection = new RTCPeerConnection();//this is the line I think I need to change
console.log('Created local peer connection object localConnection');
sendChannel = localConnection.createDataChannel('sendDataChannel');
sendChannel.binaryType = 'arraybuffer';
console.log('Created send data channel');
sendChannel.addEventListener('open', onSendChannelStateChange);
sendChannel.addEventListener('close', onSendChannelStateChange);
sendChannel.addEventListener('error', onError);
localConnection.addEventListener('icecandidate', async event => {
console.log('Local ICE candidate: ', event.candidate);
await localConnection.addIceCandidate(event.candidate);
});
this is the part of the program that have the role to receive the request and the file data on the computer2
async function server(){
remoteConnection = new RTCPeerConnection();
alert("start");
console.log('Created remote peer connection object remoteConnection');
remoteConnection.addEventListener('icecandidate', async event => {
console.log('Remote ICE candidate: ', event.candidate);
await localConnection.addIceCandidate(event.candidate);
});
remoteConnection.addEventListener('datachannel', receiveChannelCallback);
}
the code that I modified(main.js)
/* eslint no-unused-expressions: 0 */
/*
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree.
*/
'use strict';
let localConnection;
let remoteConnection;
let sendChannel;
let receiveChannel;
let fileReader;
const bitrateDiv = document.querySelector('div#bitrate');
const fileInput = document.querySelector('input#fileInput');
const abortButton = document.querySelector('button#abortButton');
const downloadAnchor = document.querySelector('a#download');
const sendProgress = document.querySelector('progress#sendProgress');
const receiveProgress = document.querySelector('progress#receiveProgress');
const statusMessage = document.querySelector('span#status');
const sendFileButton = document.querySelector('button#sendFile');
let receiveBuffer = [];
let receivedSize = 0;
let bytesPrev = 0;
let timestampPrev = 0;
let timestampStart;
let statsInterval = null;
let bitrateMax = 0;
server();
sendFileButton.addEventListener('click', () => createConnection());
fileInput.addEventListener('change', handleFileInputChange, false);
abortButton.addEventListener('click', () => {
if (fileReader && fileReader.readyState === 1) {
console.log('Abort read!');
fileReader.abort();
}
});
async function handleFileInputChange() {
const file = fileInput.files[0];
if (!file) {
console.log('No file chosen');
} else {
sendFileButton.disabled = false;
}
}
async function server(){
//const servers = {
//iceServers: [
// {
// urls: ['stun:stun1.l.google.com:19302', //'stun:stun2.l.google.com:19302'],
// },
//],
//iceCandidatePoolSize: 10,
//};
remoteConnection = new RTCPeerConnection();
alert("start");
console.log('Created remote peer connection object remoteConnection');
remoteConnection.addEventListener('icecandidate', async event => {
console.log('Remote ICE candidate: ', event.candidate);
await localConnection.addIceCandidate(event.candidate);
});
remoteConnection.addEventListener('datachannel', receiveChannelCallback);
}
async function createConnection() {
abortButton.disabled = false;
sendFileButton.disabled = true;
//const servers = {
//iceServers: [
// {
// urls: ['stun:stun1.l.google.com:19302', //'stun:stun2.l.google.com:19302'],
// },
//],
//iceCandidatePoolSize: 10,
//};
localConnection = new RTCPeerConnection();
console.log('Created local peer connection object localConnection');
sendChannel = localConnection.createDataChannel('sendDataChannel');
sendChannel.binaryType = 'arraybuffer';
console.log('Created send data channel');
sendChannel.addEventListener('open', onSendChannelStateChange);
sendChannel.addEventListener('close', onSendChannelStateChange);
sendChannel.addEventListener('error', onError);
localConnection.addEventListener('icecandidate', async event => {
console.log('Local ICE candidate: ', event.candidate);
await localConnection.addIceCandidate(event.candidate);
});
try {
const offer = await localConnection.createOffer();
await gotLocalDescription(offer);
} catch (e) {
console.log('Failed to create session description: ', e);
}
fileInput.disabled = true;
}
function sendData() {
const file = fileInput.files[0];
console.log(`File is ${[file.name, file.size, file.type, file.lastModified].join(' ')}`);
// Handle 0 size files.
statusMessage.textContent = '';
downloadAnchor.textContent = '';
if (file.size === 0) {
bitrateDiv.innerHTML = '';
statusMessage.textContent = 'File is empty, please select a non-empty file';
closeDataChannels();
return;
}
sendProgress.max = file.size;
receiveProgress.max = file.size;
const chunkSize = 16384;
fileReader = new FileReader();
let offset = 0;
fileReader.addEventListener('error', error => console.error('Error reading file:', error));
fileReader.addEventListener('abort', event => console.log('File reading aborted:', event));
fileReader.addEventListener('load', e => {
console.log('FileRead.onload ', e);
sendChannel.send(e.target.result);
offset += e.target.result.byteLength;
sendProgress.value = offset;
if (offset < file.size) {
readSlice(offset);
}
});
const readSlice = o => {
console.log('readSlice ', o);
const slice = file.slice(offset, o + chunkSize);
fileReader.readAsArrayBuffer(slice);
};
readSlice(0);
}
function closeDataChannels() {
console.log('Closing data channels');
sendChannel.close();
console.log(`Closed data channel with label: ${sendChannel.label}`);
sendChannel = null;
if (receiveChannel) {
receiveChannel.close();
console.log(`Closed data channel with label: ${receiveChannel.label}`);
receiveChannel = null;
}
localConnection.close();
remoteConnection.close();
localConnection = null;
remoteConnection = null;
console.log('Closed peer connections');
// re-enable the file select
fileInput.disabled = false;
abortButton.disabled = true;
sendFileButton.disabled = false;
}
async function gotLocalDescription(desc) {
await localConnection.setLocalDescription(desc);
console.log(`Offer from localConnection\n ${desc.sdp}`);
await remoteConnection.setRemoteDescription(desc);
try {
const answer = await remoteConnection.createAnswer();
await gotRemoteDescription(answer);
} catch (e) {
console.log('Failed to create session description: ', e);
}
}
async function gotRemoteDescription(desc) {
await remoteConnection.setLocalDescription(desc);
console.log(`Answer from remoteConnection\n ${desc.sdp}`);
await localConnection.setRemoteDescription(desc);
}
function receiveChannelCallback(event) {
console.log('Receive Channel Callback');
receiveChannel = event.channel;
receiveChannel.binaryType = 'arraybuffer';
receiveChannel.onmessage = onReceiveMessageCallback;
receiveChannel.onopen = onReceiveChannelStateChange;
receiveChannel.onclose = onReceiveChannelStateChange;
receivedSize = 0;
bitrateMax = 0;
downloadAnchor.textContent = '';
downloadAnchor.removeAttribute('download');
if (downloadAnchor.href) {
URL.revokeObjectURL(downloadAnchor.href);
downloadAnchor.removeAttribute('href');
}
}
function onReceiveMessageCallback(event) {
console.log(`Received Message ${event.data.byteLength}`);
receiveBuffer.push(event.data);
receivedSize += event.data.byteLength;
receiveProgress.value = receivedSize;
// we are assuming that our signaling protocol told
// about the expected file size (and name, hash, etc).
const file = fileInput.files[0];
if (receivedSize === file.size) {
const received = new Blob(receiveBuffer);
receiveBuffer = [];
downloadAnchor.href = URL.createObjectURL(received);
downloadAnchor.download = file.name;
downloadAnchor.textContent =
`Click to download '${file.name}' (${file.size} bytes)`;
downloadAnchor.style.display = 'block';
const bitrate = Math.round(receivedSize * 8 /
((new Date()).getTime() - timestampStart));
bitrateDiv.innerHTML =
`<strong>Average Bitrate:</strong> ${bitrate} kbits/sec (max: ${bitrateMax} kbits/sec)`;
if (statsInterval) {
clearInterval(statsInterval);
statsInterval = null;
}
closeDataChannels();
}
}
function onSendChannelStateChange() {
if (sendChannel) {
const {readyState} = sendChannel;
console.log(`Send channel state is: ${readyState}`);
if (readyState === 'open') {
sendData();
}
}
}
function onError(error) {
if (sendChannel) {
console.error('Error in sendChannel:', error);
return;
}
console.log('Error in sendChannel which is already closed:', error);
}
async function onReceiveChannelStateChange() {
if (receiveChannel) {
const readyState = receiveChannel.readyState;
console.log(`Receive channel state is: ${readyState}`);
if (readyState === 'open') {
timestampStart = (new Date()).getTime();
timestampPrev = timestampStart;
statsInterval = setInterval(displayStats, 500);
await displayStats();
}
}
}
// display bitrate statistics.
async function displayStats() {
if (remoteConnection && remoteConnection.iceConnectionState === 'connected') {
const stats = await remoteConnection.getStats();
let activeCandidatePair;
stats.forEach(report => {
if (report.type === 'transport') {
activeCandidatePair = stats.get(report.selectedCandidatePairId);
}
});
if (activeCandidatePair) {
if (timestampPrev === activeCandidatePair.timestamp) {
return;
}
// calculate current bitrate
const bytesNow = activeCandidatePair.bytesReceived;
const bitrate = Math.round((bytesNow - bytesPrev) * 8 /
(activeCandidatePair.timestamp - timestampPrev));
bitrateDiv.innerHTML = `<strong>Current Bitrate:</strong> ${bitrate} kbits/sec`;
timestampPrev = activeCandidatePair.timestamp;
bytesPrev = bytesNow;
if (bitrate > bitrateMax) {
bitrateMax = bitrate;
}
}
}
}
Thanks for helping
You can read about peer connections where every peer connection is handled by the RTCPeerConnection object and it defines how the peer connection is set up and how it should include the information about the ICE servers to use.
You can define the ICE servers as following:
localConnection = new RTCPeerConnection({iceServers: [{ url: "stun:"+ ip +":8003" }]})
or
localConnection = new RTCPeerConnection({iceServers: [{ url: + ip + ':port' }]})
or
var servers = {
iceTransportPolicy: 'relay',
iceServers: [{
urls: "turn:[" + ip + "]:3478",
username: "username",
credential: "password"
}
]};
var localConnection = new RTCPeerConnection(servers);
Moreover, You can find the full code here or
here under the folder name filetransfer .

How to fetch resource thorugh Expo/React Native

Using this code:
handleShortCapture = async () => {
try {
const photoData = await this.camera.takePictureAsync();
this.setState({ capturing: false, captures: [photoData, ...this.state.captures] })
} catch (e) {
ToastAndroid.show(e)
}
const shibeAmount = Math.floor(Math.random() * 3 + 1);
Speech.speak(`shibe amount: ${shibeAmount}`)
try {
const shibes = await fetch(`https://cors-anywhere.herokuapp.com/https://shibe.online/api/shibes?count=${shibeAmount}urls=false&httpsUrls=false`)
const jsonShibes = await shibes.json()
Speech.speak(jsonShibes.length)
} catch(e) {
Speech.speak(e.message)
}
};
I can hear the shibe amount phrase but can't hear the response from the fetch request; What am I missing?

How do you download an attachment with Gmail API with nodejs?

Im trying to make a node app on my local computer that does this:
1) Looks at my gmail inbox.
2) If an email has a certain label and an attachment, download it.
Im on step 2.
The part Im confused about is the part about 'parts'.
https://developers.google.com/gmail/api/v1/reference/users/messages/attachments/get
Googles sample code:
function getAttachments(userId, message, callback) {
var parts = message.payload.parts;
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
if (part.filename && part.filename.length > 0) {
var attachId = part.body.attachmentId;
var request = gapi.client.gmail.users.messages.attachments.get({
'id': attachId,
'messageId': message.id,
'userId': userId
});
request.execute(function(attachment) { // <--- that attachment param
callback(part.filename, part.mimeType, attachment);
});
}
}
}
It seems that 'attachment' contains the attachment data.
Here is my code:
const gmail = google.gmail({version: 'v1', auth});
gmail.users.messages.list({
userId: 'me',
'q':'subject:my-test has:nouserlabels has:attachment'
}, (err, res) => {
if (err) return console.log('1 The API returned an error: ' + err);
const msgs = res.data.messages;
if(typeof msgs !== 'undefined'){
console.log('Messages:');
msgs.forEach( msg => {
console.log(`- ${msg.id}`);
gmail.users.messages.get({
userId: 'me',
id: msg.id,
format:'full'
},(err, res) => {
if (err) return console.log('2 The API returned an error: ' + err);
// Wheres the data????? //
console.log('A')
console.log(res.data.payload.parts)
console.log('B')
console.log(res.data.payload.parts[1])
console.log('C')
console.log(res.data.payload.parts[1].body)
})
});
} else {
console.log('no messages found')
}
});
I've just used this to retrieve the docs from my favorite accountant.
const fsPromises = require('fs/promises');
const path = require('path');
const {google} = require('googleapis');
const readline = require('readline');
const fs = require('fs');
.... code from https://developers.google.com/gmail/api/quickstart/nodejs for auth
const fromEmail = process.argv[2];
const shortFromEmail = fromEmail.substring(0, fromEmail.indexOf('#'));
/**
* Lists the labels in the user's account.
*
* #param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
async function main(auth) {
const gmail = google.gmail({version: 'v1', auth})
// console.log('list messages with attachments auth: ' + JSON.stringify(auth))
const {data} = await gmail.users.messages.list({
userId: 'me',
'q':`from:${fromEmail} has:nouserlabels has:attachment`,
// maxResults: 5
});
console.log(data);
let {messages} = data;
await messages.reduce(async function (accProm, messageId) {
await accProm;
const {data: message} = await gmail.users.messages.get({
userId: 'me',
id: messageId.id,
format: 'full'
});
// console.log(JSON.stringify(message, null, 2));
let messagePayloadParts = message.payload.parts
const attachments = await messagePayloadParts.reduce(async function (acc2Prom, part) {
const acc2 = await acc2Prom;
if (!part.body.size || part.body.attachmentId == undefined) {
return acc2;
}
const internalDate = new Date(parseInt(message.internalDate, 10));
const datestring = internalDate.getFullYear() + " " + (internalDate.getMonth()+1).toString().padStart(2, "0") + " " + internalDate.getDate().toString().padStart(2, "0");
console.log(datestring, shortFromEmail, part.filename, part)
let fileExt;
switch (part.mimeType) {
case 'application/octet-stream': fileExt = ''; break;
case 'application/pdf': fileExt = 'pdf'; break;
case 'message/rfc822': fileExt = 'eml'; break;
case 'image/jpeg': fileExt = 'jpg'; break;
case 'image/png': fileExt = 'png'; break;
case 'application/msword': fileExt = 'doc'; break;
default: console.error('unknownMimeType', part.mimeType); boom;
}
const dirname = `${datestring} ${shortFromEmail}`;
const filename = part.filename ? part.filename : `noname_${part.body.attachmentId.substring(0,8)}.${fileExt}`
const attachmentPath = path.join(dirname, filename);
const s = await fsPromises.stat(attachmentPath).catch(e => undefined);
if (s) { return acc2 };
const {data: attachment} = await gmail.users.messages.attachments.get({
userId: 'me',
messageId: message.id,
id: part.body.attachmentId,
});
const { size, data: dataB64 } = attachment;
await fsPromises.mkdir(dirname, {recursive: true});
await fsPromises.writeFile(attachmentPath, Buffer.from(dataB64, 'base64'));
return acc2;
// console.log('callback: ' + JSON.stringify(attachments))
}, Promise.resolve([]));
}, Promise.resolve());
}

Categories