2023-11-02 SteemLife Development JournalsteemCreated with Sketch.

in Steem Dev3 years ago (edited)

Implementing Image Upload Above, I referenced the code from the steemit/condenser repository.
Here is a link to the code related to image uploads: https://github.com/steemit/condenser/blob/a9f0bd5ae3b8bfc116b07801d78797d0b05ade99/src/app/redux/UserSaga.js#L740-L861.

Currently, I'm using steemjs to do transaction signing, but unfortunately, steemjs doesn't provide image(binary) signing capabilities.
Here's a link to the signTransaction function in the steemjs code for that: https://github.com/steemit/steem-js/blob/c71fb595cc68f57c1e4b564c948b1d326db4fab3/src/auth/index.js#L105-L120

To solve this problem, I found a solution in "How to upload images to Steemit from Flutter" which I wrote two years ago.

To use this method, I unarchived the steemdart_ecc repository that I had archived last year and updated the Dart version.

The steemdart_ecc package can be installed as follows:

flutter pub add steemdart_ecc

However, a word of caution, the steemdart_ecc package only has some signatures implemented, so it is not recommended to use it in production.

To use the steemdart_ecc package, import it as shown below:

import 'package:steemdart_ecc/steemdart_ecc.dart'

Here is a function for signing an image:

// Signing an image
SteemSignature signImage(
 Uint8List imageBytes,
 String password,
) {
 final prefix = Uint8List.fromList(utf8.encode('ImageSigningChallenge'));
 final buff = Uint8List.fromList([...prefix, ...imageBytes]);
 final privateKey = SteemPrivateKey.fromString(password);
 final signature = privateKey.sign(buff);
 return signature;
}

Additionally, you can create a function to generate an image upload URL:

// Signing images and generating image upload URL
String makeSteemImageUploadUrl(
 Uint8List bytes,
 String username,
 String key,
) {
 final signature = signImage(bytes, key).toHex();
 final uploadUrl = "https://steemitimages.com/$username/$signature";
 return uploadUrl;
}

Write the image upload function like this:

Future<StreamedResponse> uploadImage(
    XFile image,
    String username,
    String key,
  ) async {
    final buffer = await image.readAsBytes();
    final uploadUrl = makeSteemImageUploadUrl(buffer, username, key);
    final request = MultipartRequest("POST", Uri.parse(uploadUrl))
      ..files.add(
        MultipartFile.fromBytes(
          "file",
          buffer,
          filename: image.name,
          contentType: MediaType.parse(image.mimeType ?? "image/jpeg"),
        ),
      );
    final response = await request.send();
    return response;
  }

To upload images from Flutter, we used the http package.


To summarize, I used the Dart language and Flutter framework for image signing and uploading, wrote an image signing function and an image upload URL function, and used the http package to handle image uploads. I am now able to use this function to sign the image and upload it to Steemit.


I'm a full-stack developer, working on various apps and projects.
And I'm constantly sharing articles related to development.
If you're curious about the project, you can learn more about it through the following links: [Steemlife] Dreaming of a free life on Steem

Sort:  

[광고] STEEM 개발자 커뮤니티에 참여 하시면, 다양한 혜택을 받을 수 있습니다.

Upvoted! Thank you for supporting witness @jswit.

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.100
BTC 63135.76
ETH 1788.05
USDT 1.00
SBD 0.38