Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change serial number from bytes to string #944

Merged
merged 4 commits into from
Dec 17, 2024

Conversation

oliveromahony
Copy link
Contributor

@oliveromahony oliveromahony commented Dec 10, 2024

Proposed changes

Report the serial number as a string, helps with serialisation / deserialisation

Note: the below example uses built in encoding/json but for protos then google.golang.org/protobuf/encoding/protojson is recommended.

package main

import (
	"crypto/rand"
	"encoding/json"
	"fmt"
	"math/big"
)

func main() {
	// Step 1: Generate a big integer (or use your own big.Int)
	limit := new(big.Int).Lsh(big.NewInt(1), 63)      // 1 << 63
	originalBigInt, _ := rand.Int(rand.Reader, limit) // Random big integer less than 1 << 63
	fmt.Println("Original BigInt:", originalBigInt)

	// Step 2: Encode the text representation to JSON
	jsonData, err := json.Marshal(originalBigInt.String())
	if err != nil {
		panic(err)
	}
	fmt.Println("JSON Encoded Data:", string(jsonData))

	// Step 3: Decode JSON back to the text representation
	var decodedText string
	if err := json.Unmarshal(jsonData, &decodedText); err != nil {
		panic(err)
	}
	fmt.Println("Decoded Text:", decodedText)

	// Verify correctness
	if decodedText == originalBigInt.String() {
		fmt.Println("Values match!")
	}
}

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have read the CONTRIBUTING document
  • I have run make install-tools and have attached any dependency changes to this pull request
  • If applicable, I have added tests that prove my fix is effective or that my feature works
  • If applicable, I have checked that any relevant tests pass after adding my changes
  • If applicable, I have updated any relevant documentation (README.md)
  • If applicable, I have tested my cross-platform changes on Ubuntu 22, Redhat 8, SUSE 15 and FreeBSD 13

@oliveromahony oliveromahony requested a review from a team as a code owner December 10, 2024 15:05
@github-actions github-actions bot added chore Pull requests for routine tasks documentation Improvements or additions to documentation labels Dec 10, 2024
@oliveromahony oliveromahony changed the title change serial from bytes to string Change serial number from bytes to string Dec 10, 2024
@oliveromahony oliveromahony added the v3.x Issues and Pull Requests related to the major version v3 label Dec 10, 2024
@oliveromahony oliveromahony merged commit 6a82a2e into v3 Dec 17, 2024
20 checks passed
@oliveromahony oliveromahony deleted the change-bytes-to-string-sn branch December 17, 2024 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore Pull requests for routine tasks documentation Improvements or additions to documentation v3.x Issues and Pull Requests related to the major version v3
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants