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

React TextDecoder error #150

Closed
AskBid opened this issue May 1, 2021 · 2 comments
Closed

React TextDecoder error #150

AskBid opened this issue May 1, 2021 · 2 comments

Comments

@AskBid
Copy link

AskBid commented May 1, 2021

TypeError: TextDecoder is not a constructor
(anonymous function)
node_modules/@emurgo/cardano-serialization-lib-nodejs/cardano_serialization_lib.js:26

I get this error after installing in react the 4 serialisation libraries in a React app.

yarn add @emurgo/cardano-serialization-lib-asmjs
yarn add @emurgo/cardano-serialization-lib-browser
yarn add @emurgo/cardano-serialization-lib-nodejs
yarn add @emurgo/react-native-haskell-shelley

trying to convert addr1 into stake1 with this:

const cardano = require('@emurgo/cardano-serialization-lib-nodejs')
// import {BaseAddress} from '@emurgo/cardano-serialization-lib-browser'

export default function stake1_from_addr1() {
	// Build base address
	let addr = cardano.Address.from_bech32('addr1q9whnenj7f83wvqtqw756drkd85pynz2hxrxz8szd4k5yd9uxl00u6yewtjluzse4x8mpd9x0k6x6zjmtsxtaamrtyjq7f78vk')
	let base_addr = cardano.BaseAddress.from_address(addr)

	// Extract stake credential               
	let stake_cred = base_addr.stake_cred()

	// Log hex address
	console.log(Buffer.from(stake_cred.to_keyhash().to_bytes().buffer).toString("hex"))
	// bc37defe689972e5fe0a19a98fb0b4a67db46d0a5b5c0cbef7635924
	// same address in hex format.

	// Build reward address (add 0xe1 prefix to 28 last bytes of stake credential one) 
	let reward_addr_bytes = new Uint8Array(29)                                          
	reward_addr_bytes.set([0xe1], 0)                                                
	reward_addr_bytes.set(stake_cred.to_bytes().slice(4, 32), 1)           
	let reward_addr = cardano.RewardAddress.from_address(cardano.Address.from_bytes(reward_addr_bytes))

	// Log bech32 address         
	console.log(reward_addr.to_address().to_bech32())
	// stake1ux7r0hh7dzvh9e07pgv6nraskjn8mdrdpfd4cr977a34jfquwqrj9
}
@v-almonacid
Copy link
Contributor

You are using @emurgo/cardano-serialization-lib-nodejs in a React environment, which I assume is for a frontend application running on a browser. You don't need to add those 4 dependencies, only 1 of them, and the best candidates for a browser environment are:

  • @emurgo/cardano-serialization-lib-asmjs (pure Javascript)
  • @emurgo/cardano-serialization-lib-browser (WASM)

The error your are getting means that TextDecoder is not built-in within your javascript environment. It's a default node library that usually needs to be polyfilled in a browser JS environment.

@SebastienGllmt
Copy link
Contributor

You may require a polyfill for your environment: https://github.com/rustwasm/wasm-bindgen/blob/master/guide/src/reference/browser-support.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants