Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 6eac7bc

Browse files
authored
fix: add support for browser modules to @solana/spl-token (#1337)
* fix: add support for browser modules * chore: format and lint * chore: fix flow
1 parent a7f16f7 commit 6eac7bc

15 files changed

+2135
-118
lines changed

token/js/.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/lib
2+
/test/dist

token/js/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = {
55
browser: true,
66
es6: true,
77
node: true,
8+
mocha: true,
89
},
910
extends: [
1011
'eslint:recommended',

token/js/.mocharc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
// Configure Node.js tests
4+
module.exports = {
5+
require: ['@babel/register', 'esm'],
6+
};

token/js/babel.config.json

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
{
22
"presets": [
3-
[
4-
"@babel/preset-env",
5-
{
6-
"targets": {
7-
"node": "10"
8-
}
9-
}
10-
],
11-
"@babel/preset-flow"
3+
["@babel/preset-env"],
4+
["@babel/preset-flow"]
125
],
136
"plugins": [
14-
"@babel/plugin-transform-runtime",
157
"@babel/plugin-proposal-class-properties"
168
]
179
}

token/js/client/layout.js

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @flow
22

33
import * as BufferLayout from 'buffer-layout';
4-
import {Buffer} from 'buffer';
54

65
/**
76
* Layout for a public key
@@ -16,33 +15,3 @@ export const publicKey = (property: string = 'publicKey'): Object => {
1615
export const uint64 = (property: string = 'uint64'): Object => {
1716
return BufferLayout.blob(8, property);
1817
};
19-
20-
/**
21-
* Layout for a Rust String type
22-
*/
23-
export const rustString = (property: string = 'string'): Object => {
24-
const rsl = BufferLayout.struct(
25-
[
26-
BufferLayout.u32('length'),
27-
BufferLayout.u32('lengthPadding'),
28-
BufferLayout.blob(BufferLayout.offset(BufferLayout.u32(), -8), 'chars'),
29-
],
30-
property,
31-
);
32-
const _decode = rsl.decode.bind(rsl);
33-
const _encode = rsl.encode.bind(rsl);
34-
35-
rsl.decode = (buffer, offset) => {
36-
const data = _decode(buffer, offset);
37-
return data.chars.toString('utf8');
38-
};
39-
40-
rsl.encode = (str, buffer, offset) => {
41-
const data = {
42-
chars: Buffer.from(str, 'utf8'),
43-
};
44-
return _encode(data, buffer, offset);
45-
};
46-
47-
return rsl;
48-
};

token/js/client/token.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ export const TOKEN_PROGRAM_ID: PublicKey = new PublicKey(
2727
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
2828
);
2929

30+
/**
31+
* Unfortunately, BufferLayout.encode uses an `instanceof` check for `Buffer`
32+
* which fails when using `publicKey.toBuffer()` directly because the bundled `Buffer`
33+
* class in `@solana/web3.js` is different from the bundled `Buffer` class in this package
34+
*/
35+
function pubkeyToBuffer(publicKey: PublicKey): typeof Buffer {
36+
return Buffer.from(publicKey.toBuffer());
37+
}
38+
3039
/**
3140
* 64-bit value
3241
*/
@@ -1300,9 +1309,9 @@ export class Token {
13001309
{
13011310
instruction: 0, // InitializeMint instruction
13021311
decimals,
1303-
mintAuthority: mintAuthority.toBuffer(),
1312+
mintAuthority: pubkeyToBuffer(mintAuthority),
13041313
option: freezeAuthority === null ? 0 : 1,
1305-
freezeAuthority: (freezeAuthority || new PublicKey()).toBuffer(),
1314+
freezeAuthority: pubkeyToBuffer(freezeAuthority || new PublicKey()),
13061315
},
13071316
data,
13081317
);
@@ -1544,7 +1553,7 @@ export class Token {
15441553
instruction: 6, // SetAuthority instruction
15451554
authorityType: AuthorityTypeCodes[authorityType],
15461555
option: newAuthority === null ? 0 : 1,
1547-
newAuthority: (newAuthority || new PublicKey()).toBuffer(),
1556+
newAuthority: pubkeyToBuffer(newAuthority || new PublicKey()),
15481557
},
15491558
data,
15501559
);

0 commit comments

Comments
 (0)