@solana-mobile/mobile-wallet-adapter-protocol-web3js 0.0.1-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2022 Solana Mobile Inc.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # `@solana-mobile/mobile-wallet-adapter-protocol-web3js`
2
+
3
+ This is a convenience wrapper that makes it easy to use common primitives from [@solana/web3.js](https://github.com/solana-labs/solana-web3.js) – such as `Transaction` and `Uint8Array` – with [@solana-mobile/mobile-wallet-adapter-protocol](https://github.com/solana-mobile/mobile-wallet-adapter/tree/main/js/packages/mobile-wallet-adapter-protocol).
@@ -0,0 +1,135 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var web3_js = require('@solana/web3.js');
6
+ var mobileWalletAdapterProtocol = require('@solana-mobile/mobile-wallet-adapter-protocol');
7
+
8
+ /******************************************************************************
9
+ Copyright (c) Microsoft Corporation.
10
+
11
+ Permission to use, copy, modify, and/or distribute this software for any
12
+ purpose with or without fee is hereby granted.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
15
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
17
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20
+ PERFORMANCE OF THIS SOFTWARE.
21
+ ***************************************************************************** */
22
+
23
+ function __awaiter(thisArg, _arguments, P, generator) {
24
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
25
+ return new (P || (P = Promise))(function (resolve, reject) {
26
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
27
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
28
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
29
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
30
+ });
31
+ }
32
+
33
+ function getBase64StringFromByteArray(byteArray) {
34
+ return window.btoa(String.fromCharCode.call(null, ...byteArray));
35
+ }
36
+ function getByteArrayFromBase64String(base64EncodedByteArray) {
37
+ return new Uint8Array(window
38
+ .atob(base64EncodedByteArray)
39
+ .split('')
40
+ .map((c) => c.charCodeAt(0)));
41
+ }
42
+ function transact(callback, config) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ const augmentedCallback = (walletAPI) => {
45
+ const augmentedAPI = ((apiCall) => __awaiter(this, void 0, void 0, function* () {
46
+ let latestBlockhashPromise;
47
+ function getLatestBlockhash(connection) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ if (latestBlockhashPromise == null) {
50
+ latestBlockhashPromise = connection.getLatestBlockhash({
51
+ commitment: connection.commitment,
52
+ });
53
+ }
54
+ return yield latestBlockhashPromise;
55
+ });
56
+ }
57
+ switch (apiCall.method) {
58
+ case 'authorize':
59
+ return yield walletAPI(apiCall);
60
+ case 'clone_authorization':
61
+ return yield walletAPI(apiCall);
62
+ case 'deauthorize':
63
+ return yield walletAPI(apiCall);
64
+ case 'reauthorize':
65
+ return yield walletAPI(apiCall);
66
+ case 'sign_and_send_transaction': {
67
+ const payloads = yield Promise.all(apiCall.transactions.map((transaction) => __awaiter(this, void 0, void 0, function* () {
68
+ if (transaction.feePayer == null) {
69
+ transaction.feePayer = apiCall.fee_payer;
70
+ }
71
+ if (transaction.recentBlockhash == null) {
72
+ const { blockhash } = yield getLatestBlockhash(apiCall.connection);
73
+ transaction.recentBlockhash = blockhash;
74
+ }
75
+ const serializedTransaction = transaction.serialize({
76
+ requireAllSignatures: false,
77
+ verifySignatures: false,
78
+ });
79
+ return serializedTransaction.toString('base64');
80
+ })));
81
+ let targetCommitment;
82
+ switch (apiCall.connection.commitment) {
83
+ case 'confirmed':
84
+ case 'finalized':
85
+ case 'processed':
86
+ targetCommitment = apiCall.connection.commitment;
87
+ break;
88
+ default:
89
+ targetCommitment = 'finalized';
90
+ }
91
+ const { signatures } = yield walletAPI({
92
+ method: 'sign_and_send_transaction',
93
+ auth_token: apiCall.auth_token,
94
+ commitment: targetCommitment,
95
+ payloads,
96
+ });
97
+ return signatures;
98
+ }
99
+ case 'sign_message': {
100
+ const payloads = apiCall.byteArrays.map(getBase64StringFromByteArray);
101
+ const { signed_payloads: base64EncodedSignedMessages } = yield walletAPI({
102
+ method: 'sign_message',
103
+ auth_token: apiCall.auth_token,
104
+ payloads,
105
+ });
106
+ const signedMessages = base64EncodedSignedMessages.map(getByteArrayFromBase64String);
107
+ return signedMessages;
108
+ }
109
+ case 'sign_transaction': {
110
+ const serializedTransactions = apiCall.transactions.map((transaction) => transaction.serialize({
111
+ requireAllSignatures: false,
112
+ verifySignatures: false,
113
+ }));
114
+ const payloads = serializedTransactions.map((serializedTransaction) => serializedTransaction.toString('base64'));
115
+ const { signed_payloads: base64EncodedCompiledTransactions } = yield walletAPI({
116
+ method: 'sign_transaction',
117
+ auth_token: apiCall.auth_token,
118
+ payloads,
119
+ });
120
+ const compiledTransactions = base64EncodedCompiledTransactions.map(getByteArrayFromBase64String);
121
+ const transactions = compiledTransactions.map(web3_js.Transaction.from);
122
+ return transactions;
123
+ }
124
+ default:
125
+ // If this switch is exhausive, this should be unreachable.
126
+ return ((_) => undefined)();
127
+ }
128
+ }));
129
+ return callback(augmentedAPI);
130
+ };
131
+ return yield mobileWalletAdapterProtocol.transact(augmentedCallback, config);
132
+ });
133
+ }
134
+
135
+ exports.transact = transact;
@@ -0,0 +1,135 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var web3_js = require('@solana/web3.js');
6
+ var mobileWalletAdapterProtocol = require('@solana-mobile/mobile-wallet-adapter-protocol');
7
+
8
+ /******************************************************************************
9
+ Copyright (c) Microsoft Corporation.
10
+
11
+ Permission to use, copy, modify, and/or distribute this software for any
12
+ purpose with or without fee is hereby granted.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
15
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
17
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20
+ PERFORMANCE OF THIS SOFTWARE.
21
+ ***************************************************************************** */
22
+
23
+ function __awaiter(thisArg, _arguments, P, generator) {
24
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
25
+ return new (P || (P = Promise))(function (resolve, reject) {
26
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
27
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
28
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
29
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
30
+ });
31
+ }
32
+
33
+ function getBase64StringFromByteArray(byteArray) {
34
+ return window.btoa(String.fromCharCode.call(null, ...byteArray));
35
+ }
36
+ function getByteArrayFromBase64String(base64EncodedByteArray) {
37
+ return new Uint8Array(window
38
+ .atob(base64EncodedByteArray)
39
+ .split('')
40
+ .map((c) => c.charCodeAt(0)));
41
+ }
42
+ function transact(callback, config) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ const augmentedCallback = (walletAPI) => {
45
+ const augmentedAPI = ((apiCall) => __awaiter(this, void 0, void 0, function* () {
46
+ let latestBlockhashPromise;
47
+ function getLatestBlockhash(connection) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ if (latestBlockhashPromise == null) {
50
+ latestBlockhashPromise = connection.getLatestBlockhash({
51
+ commitment: connection.commitment,
52
+ });
53
+ }
54
+ return yield latestBlockhashPromise;
55
+ });
56
+ }
57
+ switch (apiCall.method) {
58
+ case 'authorize':
59
+ return yield walletAPI(apiCall);
60
+ case 'clone_authorization':
61
+ return yield walletAPI(apiCall);
62
+ case 'deauthorize':
63
+ return yield walletAPI(apiCall);
64
+ case 'reauthorize':
65
+ return yield walletAPI(apiCall);
66
+ case 'sign_and_send_transaction': {
67
+ const payloads = yield Promise.all(apiCall.transactions.map((transaction) => __awaiter(this, void 0, void 0, function* () {
68
+ if (transaction.feePayer == null) {
69
+ transaction.feePayer = apiCall.fee_payer;
70
+ }
71
+ if (transaction.recentBlockhash == null) {
72
+ const { blockhash } = yield getLatestBlockhash(apiCall.connection);
73
+ transaction.recentBlockhash = blockhash;
74
+ }
75
+ const serializedTransaction = transaction.serialize({
76
+ requireAllSignatures: false,
77
+ verifySignatures: false,
78
+ });
79
+ return serializedTransaction.toString('base64');
80
+ })));
81
+ let targetCommitment;
82
+ switch (apiCall.connection.commitment) {
83
+ case 'confirmed':
84
+ case 'finalized':
85
+ case 'processed':
86
+ targetCommitment = apiCall.connection.commitment;
87
+ break;
88
+ default:
89
+ targetCommitment = 'finalized';
90
+ }
91
+ const { signatures } = yield walletAPI({
92
+ method: 'sign_and_send_transaction',
93
+ auth_token: apiCall.auth_token,
94
+ commitment: targetCommitment,
95
+ payloads,
96
+ });
97
+ return signatures;
98
+ }
99
+ case 'sign_message': {
100
+ const payloads = apiCall.byteArrays.map(getBase64StringFromByteArray);
101
+ const { signed_payloads: base64EncodedSignedMessages } = yield walletAPI({
102
+ method: 'sign_message',
103
+ auth_token: apiCall.auth_token,
104
+ payloads,
105
+ });
106
+ const signedMessages = base64EncodedSignedMessages.map(getByteArrayFromBase64String);
107
+ return signedMessages;
108
+ }
109
+ case 'sign_transaction': {
110
+ const serializedTransactions = apiCall.transactions.map((transaction) => transaction.serialize({
111
+ requireAllSignatures: false,
112
+ verifySignatures: false,
113
+ }));
114
+ const payloads = serializedTransactions.map((serializedTransaction) => serializedTransaction.toString('base64'));
115
+ const { signed_payloads: base64EncodedCompiledTransactions } = yield walletAPI({
116
+ method: 'sign_transaction',
117
+ auth_token: apiCall.auth_token,
118
+ payloads,
119
+ });
120
+ const compiledTransactions = base64EncodedCompiledTransactions.map(getByteArrayFromBase64String);
121
+ const transactions = compiledTransactions.map(web3_js.Transaction.from);
122
+ return transactions;
123
+ }
124
+ default:
125
+ // If this switch is exhausive, this should be unreachable.
126
+ return ((_) => undefined)();
127
+ }
128
+ }));
129
+ return callback(augmentedAPI);
130
+ };
131
+ return yield mobileWalletAdapterProtocol.transact(augmentedCallback, config);
132
+ });
133
+ }
134
+
135
+ exports.transact = transact;
@@ -0,0 +1,135 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var web3_js = require('@solana/web3.js');
6
+ var mobileWalletAdapterProtocol = require('@solana-mobile/mobile-wallet-adapter-protocol');
7
+
8
+ /******************************************************************************
9
+ Copyright (c) Microsoft Corporation.
10
+
11
+ Permission to use, copy, modify, and/or distribute this software for any
12
+ purpose with or without fee is hereby granted.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
15
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
17
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20
+ PERFORMANCE OF THIS SOFTWARE.
21
+ ***************************************************************************** */
22
+
23
+ function __awaiter(thisArg, _arguments, P, generator) {
24
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
25
+ return new (P || (P = Promise))(function (resolve, reject) {
26
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
27
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
28
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
29
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
30
+ });
31
+ }
32
+
33
+ function getBase64StringFromByteArray(byteArray) {
34
+ return window.btoa(String.fromCharCode.call(null, ...byteArray));
35
+ }
36
+ function getByteArrayFromBase64String(base64EncodedByteArray) {
37
+ return new Uint8Array(window
38
+ .atob(base64EncodedByteArray)
39
+ .split('')
40
+ .map((c) => c.charCodeAt(0)));
41
+ }
42
+ function transact(callback, config) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ const augmentedCallback = (walletAPI) => {
45
+ const augmentedAPI = ((apiCall) => __awaiter(this, void 0, void 0, function* () {
46
+ let latestBlockhashPromise;
47
+ function getLatestBlockhash(connection) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ if (latestBlockhashPromise == null) {
50
+ latestBlockhashPromise = connection.getLatestBlockhash({
51
+ commitment: connection.commitment,
52
+ });
53
+ }
54
+ return yield latestBlockhashPromise;
55
+ });
56
+ }
57
+ switch (apiCall.method) {
58
+ case 'authorize':
59
+ return yield walletAPI(apiCall);
60
+ case 'clone_authorization':
61
+ return yield walletAPI(apiCall);
62
+ case 'deauthorize':
63
+ return yield walletAPI(apiCall);
64
+ case 'reauthorize':
65
+ return yield walletAPI(apiCall);
66
+ case 'sign_and_send_transaction': {
67
+ const payloads = yield Promise.all(apiCall.transactions.map((transaction) => __awaiter(this, void 0, void 0, function* () {
68
+ if (transaction.feePayer == null) {
69
+ transaction.feePayer = apiCall.fee_payer;
70
+ }
71
+ if (transaction.recentBlockhash == null) {
72
+ const { blockhash } = yield getLatestBlockhash(apiCall.connection);
73
+ transaction.recentBlockhash = blockhash;
74
+ }
75
+ const serializedTransaction = transaction.serialize({
76
+ requireAllSignatures: false,
77
+ verifySignatures: false,
78
+ });
79
+ return serializedTransaction.toString('base64');
80
+ })));
81
+ let targetCommitment;
82
+ switch (apiCall.connection.commitment) {
83
+ case 'confirmed':
84
+ case 'finalized':
85
+ case 'processed':
86
+ targetCommitment = apiCall.connection.commitment;
87
+ break;
88
+ default:
89
+ targetCommitment = 'finalized';
90
+ }
91
+ const { signatures } = yield walletAPI({
92
+ method: 'sign_and_send_transaction',
93
+ auth_token: apiCall.auth_token,
94
+ commitment: targetCommitment,
95
+ payloads,
96
+ });
97
+ return signatures;
98
+ }
99
+ case 'sign_message': {
100
+ const payloads = apiCall.byteArrays.map(getBase64StringFromByteArray);
101
+ const { signed_payloads: base64EncodedSignedMessages } = yield walletAPI({
102
+ method: 'sign_message',
103
+ auth_token: apiCall.auth_token,
104
+ payloads,
105
+ });
106
+ const signedMessages = base64EncodedSignedMessages.map(getByteArrayFromBase64String);
107
+ return signedMessages;
108
+ }
109
+ case 'sign_transaction': {
110
+ const serializedTransactions = apiCall.transactions.map((transaction) => transaction.serialize({
111
+ requireAllSignatures: false,
112
+ verifySignatures: false,
113
+ }));
114
+ const payloads = serializedTransactions.map((serializedTransaction) => serializedTransaction.toString('base64'));
115
+ const { signed_payloads: base64EncodedCompiledTransactions } = yield walletAPI({
116
+ method: 'sign_transaction',
117
+ auth_token: apiCall.auth_token,
118
+ payloads,
119
+ });
120
+ const compiledTransactions = base64EncodedCompiledTransactions.map(getByteArrayFromBase64String);
121
+ const transactions = compiledTransactions.map(web3_js.Transaction.from);
122
+ return transactions;
123
+ }
124
+ default:
125
+ // If this switch is exhausive, this should be unreachable.
126
+ return ((_) => undefined)();
127
+ }
128
+ }));
129
+ return callback(augmentedAPI);
130
+ };
131
+ return yield mobileWalletAdapterProtocol.transact(augmentedCallback, config);
132
+ });
133
+ }
134
+
135
+ exports.transact = transact;
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,131 @@
1
+ import { Transaction } from '@solana/web3.js';
2
+ import { transact as transact$1 } from '@solana-mobile/mobile-wallet-adapter-protocol';
3
+
4
+ /******************************************************************************
5
+ Copyright (c) Microsoft Corporation.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted.
9
+
10
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
15
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16
+ PERFORMANCE OF THIS SOFTWARE.
17
+ ***************************************************************************** */
18
+
19
+ function __awaiter(thisArg, _arguments, P, generator) {
20
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
21
+ return new (P || (P = Promise))(function (resolve, reject) {
22
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
23
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
24
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
25
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
26
+ });
27
+ }
28
+
29
+ function getBase64StringFromByteArray(byteArray) {
30
+ return window.btoa(String.fromCharCode.call(null, ...byteArray));
31
+ }
32
+ function getByteArrayFromBase64String(base64EncodedByteArray) {
33
+ return new Uint8Array(window
34
+ .atob(base64EncodedByteArray)
35
+ .split('')
36
+ .map((c) => c.charCodeAt(0)));
37
+ }
38
+ function transact(callback, config) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ const augmentedCallback = (walletAPI) => {
41
+ const augmentedAPI = ((apiCall) => __awaiter(this, void 0, void 0, function* () {
42
+ let latestBlockhashPromise;
43
+ function getLatestBlockhash(connection) {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ if (latestBlockhashPromise == null) {
46
+ latestBlockhashPromise = connection.getLatestBlockhash({
47
+ commitment: connection.commitment,
48
+ });
49
+ }
50
+ return yield latestBlockhashPromise;
51
+ });
52
+ }
53
+ switch (apiCall.method) {
54
+ case 'authorize':
55
+ return yield walletAPI(apiCall);
56
+ case 'clone_authorization':
57
+ return yield walletAPI(apiCall);
58
+ case 'deauthorize':
59
+ return yield walletAPI(apiCall);
60
+ case 'reauthorize':
61
+ return yield walletAPI(apiCall);
62
+ case 'sign_and_send_transaction': {
63
+ const payloads = yield Promise.all(apiCall.transactions.map((transaction) => __awaiter(this, void 0, void 0, function* () {
64
+ if (transaction.feePayer == null) {
65
+ transaction.feePayer = apiCall.fee_payer;
66
+ }
67
+ if (transaction.recentBlockhash == null) {
68
+ const { blockhash } = yield getLatestBlockhash(apiCall.connection);
69
+ transaction.recentBlockhash = blockhash;
70
+ }
71
+ const serializedTransaction = transaction.serialize({
72
+ requireAllSignatures: false,
73
+ verifySignatures: false,
74
+ });
75
+ return serializedTransaction.toString('base64');
76
+ })));
77
+ let targetCommitment;
78
+ switch (apiCall.connection.commitment) {
79
+ case 'confirmed':
80
+ case 'finalized':
81
+ case 'processed':
82
+ targetCommitment = apiCall.connection.commitment;
83
+ break;
84
+ default:
85
+ targetCommitment = 'finalized';
86
+ }
87
+ const { signatures } = yield walletAPI({
88
+ method: 'sign_and_send_transaction',
89
+ auth_token: apiCall.auth_token,
90
+ commitment: targetCommitment,
91
+ payloads,
92
+ });
93
+ return signatures;
94
+ }
95
+ case 'sign_message': {
96
+ const payloads = apiCall.byteArrays.map(getBase64StringFromByteArray);
97
+ const { signed_payloads: base64EncodedSignedMessages } = yield walletAPI({
98
+ method: 'sign_message',
99
+ auth_token: apiCall.auth_token,
100
+ payloads,
101
+ });
102
+ const signedMessages = base64EncodedSignedMessages.map(getByteArrayFromBase64String);
103
+ return signedMessages;
104
+ }
105
+ case 'sign_transaction': {
106
+ const serializedTransactions = apiCall.transactions.map((transaction) => transaction.serialize({
107
+ requireAllSignatures: false,
108
+ verifySignatures: false,
109
+ }));
110
+ const payloads = serializedTransactions.map((serializedTransaction) => serializedTransaction.toString('base64'));
111
+ const { signed_payloads: base64EncodedCompiledTransactions } = yield walletAPI({
112
+ method: 'sign_transaction',
113
+ auth_token: apiCall.auth_token,
114
+ payloads,
115
+ });
116
+ const compiledTransactions = base64EncodedCompiledTransactions.map(getByteArrayFromBase64String);
117
+ const transactions = compiledTransactions.map(Transaction.from);
118
+ return transactions;
119
+ }
120
+ default:
121
+ // If this switch is exhausive, this should be unreachable.
122
+ return ((_) => undefined)();
123
+ }
124
+ }));
125
+ return callback(augmentedAPI);
126
+ };
127
+ return yield transact$1(augmentedCallback, config);
128
+ });
129
+ }
130
+
131
+ export { transact };
@@ -0,0 +1,131 @@
1
+ import { Transaction } from '@solana/web3.js';
2
+ import { transact as transact$1 } from '@solana-mobile/mobile-wallet-adapter-protocol';
3
+
4
+ /******************************************************************************
5
+ Copyright (c) Microsoft Corporation.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted.
9
+
10
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
15
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16
+ PERFORMANCE OF THIS SOFTWARE.
17
+ ***************************************************************************** */
18
+
19
+ function __awaiter(thisArg, _arguments, P, generator) {
20
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
21
+ return new (P || (P = Promise))(function (resolve, reject) {
22
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
23
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
24
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
25
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
26
+ });
27
+ }
28
+
29
+ function getBase64StringFromByteArray(byteArray) {
30
+ return window.btoa(String.fromCharCode.call(null, ...byteArray));
31
+ }
32
+ function getByteArrayFromBase64String(base64EncodedByteArray) {
33
+ return new Uint8Array(window
34
+ .atob(base64EncodedByteArray)
35
+ .split('')
36
+ .map((c) => c.charCodeAt(0)));
37
+ }
38
+ function transact(callback, config) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ const augmentedCallback = (walletAPI) => {
41
+ const augmentedAPI = ((apiCall) => __awaiter(this, void 0, void 0, function* () {
42
+ let latestBlockhashPromise;
43
+ function getLatestBlockhash(connection) {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ if (latestBlockhashPromise == null) {
46
+ latestBlockhashPromise = connection.getLatestBlockhash({
47
+ commitment: connection.commitment,
48
+ });
49
+ }
50
+ return yield latestBlockhashPromise;
51
+ });
52
+ }
53
+ switch (apiCall.method) {
54
+ case 'authorize':
55
+ return yield walletAPI(apiCall);
56
+ case 'clone_authorization':
57
+ return yield walletAPI(apiCall);
58
+ case 'deauthorize':
59
+ return yield walletAPI(apiCall);
60
+ case 'reauthorize':
61
+ return yield walletAPI(apiCall);
62
+ case 'sign_and_send_transaction': {
63
+ const payloads = yield Promise.all(apiCall.transactions.map((transaction) => __awaiter(this, void 0, void 0, function* () {
64
+ if (transaction.feePayer == null) {
65
+ transaction.feePayer = apiCall.fee_payer;
66
+ }
67
+ if (transaction.recentBlockhash == null) {
68
+ const { blockhash } = yield getLatestBlockhash(apiCall.connection);
69
+ transaction.recentBlockhash = blockhash;
70
+ }
71
+ const serializedTransaction = transaction.serialize({
72
+ requireAllSignatures: false,
73
+ verifySignatures: false,
74
+ });
75
+ return serializedTransaction.toString('base64');
76
+ })));
77
+ let targetCommitment;
78
+ switch (apiCall.connection.commitment) {
79
+ case 'confirmed':
80
+ case 'finalized':
81
+ case 'processed':
82
+ targetCommitment = apiCall.connection.commitment;
83
+ break;
84
+ default:
85
+ targetCommitment = 'finalized';
86
+ }
87
+ const { signatures } = yield walletAPI({
88
+ method: 'sign_and_send_transaction',
89
+ auth_token: apiCall.auth_token,
90
+ commitment: targetCommitment,
91
+ payloads,
92
+ });
93
+ return signatures;
94
+ }
95
+ case 'sign_message': {
96
+ const payloads = apiCall.byteArrays.map(getBase64StringFromByteArray);
97
+ const { signed_payloads: base64EncodedSignedMessages } = yield walletAPI({
98
+ method: 'sign_message',
99
+ auth_token: apiCall.auth_token,
100
+ payloads,
101
+ });
102
+ const signedMessages = base64EncodedSignedMessages.map(getByteArrayFromBase64String);
103
+ return signedMessages;
104
+ }
105
+ case 'sign_transaction': {
106
+ const serializedTransactions = apiCall.transactions.map((transaction) => transaction.serialize({
107
+ requireAllSignatures: false,
108
+ verifySignatures: false,
109
+ }));
110
+ const payloads = serializedTransactions.map((serializedTransaction) => serializedTransaction.toString('base64'));
111
+ const { signed_payloads: base64EncodedCompiledTransactions } = yield walletAPI({
112
+ method: 'sign_transaction',
113
+ auth_token: apiCall.auth_token,
114
+ payloads,
115
+ });
116
+ const compiledTransactions = base64EncodedCompiledTransactions.map(getByteArrayFromBase64String);
117
+ const transactions = compiledTransactions.map(Transaction.from);
118
+ return transactions;
119
+ }
120
+ default:
121
+ // If this switch is exhausive, this should be unreachable.
122
+ return ((_) => undefined)();
123
+ }
124
+ }));
125
+ return callback(augmentedAPI);
126
+ };
127
+ return yield transact$1(augmentedCallback, config);
128
+ });
129
+ }
130
+
131
+ export { transact };
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,24 @@
1
+ import { Connection, PublicKey, Transaction, TransactionSignature } from "@solana/web3.js";
2
+ import { AuthorizeAPI, AuthToken, CloneAuthorizationAPI, DeauthorizeAPI, ReauthorizeAPI, WalletAssociationConfig } from "@solana-mobile/mobile-wallet-adapter-protocol";
3
+ interface Web3MobileWalletAPI extends AuthorizeAPI, CloneAuthorizationAPI, DeauthorizeAPI, ReauthorizeAPI {
4
+ (apiCall: {
5
+ method: "sign_and_send_transaction";
6
+ auth_token: AuthToken;
7
+ connection: Connection;
8
+ fee_payer?: PublicKey;
9
+ transactions: Transaction[];
10
+ }): Promise<TransactionSignature[]>;
11
+ (apiCall: {
12
+ method: "sign_transaction";
13
+ auth_token: AuthToken;
14
+ transactions: Transaction[];
15
+ }): Promise<Transaction[]>;
16
+ (apiCall: {
17
+ method: "sign_message";
18
+ auth_token: AuthToken;
19
+ byteArrays: Uint8Array[];
20
+ }): Promise<Uint8Array[]>;
21
+ }
22
+ declare function transact<TReturn>(callback: (walletAPI: Web3MobileWalletAPI) => TReturn, config?: WalletAssociationConfig): Promise<TReturn>;
23
+ export { Web3MobileWalletAPI, transact };
24
+ //# sourceMappingURL=index.browser.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.browser.d.mts","sourceRoot":"","sources":["../../src/index.ts","../../src/transact.ts"],"names":[],"mappings":""}
@@ -0,0 +1,24 @@
1
+ import { Connection, PublicKey, Transaction, TransactionSignature } from "@solana/web3.js";
2
+ import { AuthorizeAPI, AuthToken, CloneAuthorizationAPI, DeauthorizeAPI, ReauthorizeAPI, WalletAssociationConfig } from "@solana-mobile/mobile-wallet-adapter-protocol";
3
+ interface Web3MobileWalletAPI extends AuthorizeAPI, CloneAuthorizationAPI, DeauthorizeAPI, ReauthorizeAPI {
4
+ (apiCall: {
5
+ method: "sign_and_send_transaction";
6
+ auth_token: AuthToken;
7
+ connection: Connection;
8
+ fee_payer?: PublicKey;
9
+ transactions: Transaction[];
10
+ }): Promise<TransactionSignature[]>;
11
+ (apiCall: {
12
+ method: "sign_transaction";
13
+ auth_token: AuthToken;
14
+ transactions: Transaction[];
15
+ }): Promise<Transaction[]>;
16
+ (apiCall: {
17
+ method: "sign_message";
18
+ auth_token: AuthToken;
19
+ byteArrays: Uint8Array[];
20
+ }): Promise<Uint8Array[]>;
21
+ }
22
+ declare function transact<TReturn>(callback: (walletAPI: Web3MobileWalletAPI) => TReturn, config?: WalletAssociationConfig): Promise<TReturn>;
23
+ export { Web3MobileWalletAPI, transact };
24
+ //# sourceMappingURL=index.browser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.browser.d.ts","sourceRoot":"","sources":["../../src/index.ts","../../src/transact.ts"],"names":[],"mappings":""}
@@ -0,0 +1,24 @@
1
+ import { Connection, PublicKey, Transaction, TransactionSignature } from "@solana/web3.js";
2
+ import { AuthorizeAPI, AuthToken, CloneAuthorizationAPI, DeauthorizeAPI, ReauthorizeAPI, WalletAssociationConfig } from "@solana-mobile/mobile-wallet-adapter-protocol";
3
+ interface Web3MobileWalletAPI extends AuthorizeAPI, CloneAuthorizationAPI, DeauthorizeAPI, ReauthorizeAPI {
4
+ (apiCall: {
5
+ method: "sign_and_send_transaction";
6
+ auth_token: AuthToken;
7
+ connection: Connection;
8
+ fee_payer?: PublicKey;
9
+ transactions: Transaction[];
10
+ }): Promise<TransactionSignature[]>;
11
+ (apiCall: {
12
+ method: "sign_transaction";
13
+ auth_token: AuthToken;
14
+ transactions: Transaction[];
15
+ }): Promise<Transaction[]>;
16
+ (apiCall: {
17
+ method: "sign_message";
18
+ auth_token: AuthToken;
19
+ byteArrays: Uint8Array[];
20
+ }): Promise<Uint8Array[]>;
21
+ }
22
+ declare function transact<TReturn>(callback: (walletAPI: Web3MobileWalletAPI) => TReturn, config?: WalletAssociationConfig): Promise<TReturn>;
23
+ export { Web3MobileWalletAPI, transact };
24
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/index.ts","../../src/transact.ts"],"names":[],"mappings":""}
@@ -0,0 +1,24 @@
1
+ import { Connection, PublicKey, Transaction, TransactionSignature } from "@solana/web3.js";
2
+ import { AuthorizeAPI, AuthToken, CloneAuthorizationAPI, DeauthorizeAPI, ReauthorizeAPI, WalletAssociationConfig } from "@solana-mobile/mobile-wallet-adapter-protocol";
3
+ interface Web3MobileWalletAPI extends AuthorizeAPI, CloneAuthorizationAPI, DeauthorizeAPI, ReauthorizeAPI {
4
+ (apiCall: {
5
+ method: "sign_and_send_transaction";
6
+ auth_token: AuthToken;
7
+ connection: Connection;
8
+ fee_payer?: PublicKey;
9
+ transactions: Transaction[];
10
+ }): Promise<TransactionSignature[]>;
11
+ (apiCall: {
12
+ method: "sign_transaction";
13
+ auth_token: AuthToken;
14
+ transactions: Transaction[];
15
+ }): Promise<Transaction[]>;
16
+ (apiCall: {
17
+ method: "sign_message";
18
+ auth_token: AuthToken;
19
+ byteArrays: Uint8Array[];
20
+ }): Promise<Uint8Array[]>;
21
+ }
22
+ declare function transact<TReturn>(callback: (walletAPI: Web3MobileWalletAPI) => TReturn, config?: WalletAssociationConfig): Promise<TReturn>;
23
+ export { Web3MobileWalletAPI, transact };
24
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts","../../src/transact.ts"],"names":[],"mappings":""}
@@ -0,0 +1,24 @@
1
+ import { Connection, PublicKey, Transaction, TransactionSignature } from "@solana/web3.js";
2
+ import { AuthorizeAPI, AuthToken, CloneAuthorizationAPI, DeauthorizeAPI, ReauthorizeAPI, WalletAssociationConfig } from "@solana-mobile/mobile-wallet-adapter-protocol";
3
+ interface Web3MobileWalletAPI extends AuthorizeAPI, CloneAuthorizationAPI, DeauthorizeAPI, ReauthorizeAPI {
4
+ (apiCall: {
5
+ method: "sign_and_send_transaction";
6
+ auth_token: AuthToken;
7
+ connection: Connection;
8
+ fee_payer?: PublicKey;
9
+ transactions: Transaction[];
10
+ }): Promise<TransactionSignature[]>;
11
+ (apiCall: {
12
+ method: "sign_transaction";
13
+ auth_token: AuthToken;
14
+ transactions: Transaction[];
15
+ }): Promise<Transaction[]>;
16
+ (apiCall: {
17
+ method: "sign_message";
18
+ auth_token: AuthToken;
19
+ byteArrays: Uint8Array[];
20
+ }): Promise<Uint8Array[]>;
21
+ }
22
+ declare function transact<TReturn>(callback: (walletAPI: Web3MobileWalletAPI) => TReturn, config?: WalletAssociationConfig): Promise<TReturn>;
23
+ export { Web3MobileWalletAPI, transact };
24
+ //# sourceMappingURL=index.native.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.native.d.ts","sourceRoot":"","sources":["../../src/index.ts","../../src/transact.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@solana-mobile/mobile-wallet-adapter-protocol-web3js",
3
+ "description": "A convenience wrapper that enables you to call Solana Mobile Stack protocol methods using objects from @solana/web3.js",
4
+ "version": "0.0.1-alpha.1",
5
+ "author": "Steven Luscher <steven.luscher@solanamobile.com>",
6
+ "repository": "https://github.com/solana-mobile/mobile-wallet-adapter",
7
+ "license": "Apache-2.0",
8
+ "type": "module",
9
+ "sideEffects": false,
10
+ "main": "lib/cjs/index.js",
11
+ "react-native": "lib/cjs/index.native.js",
12
+ "module": "lib/esm/index.mjs",
13
+ "types": "lib/types/index.d.ts",
14
+ "browser": {
15
+ "./lib/cjs/index.js": "./lib/cjs/index.browser.js",
16
+ "./lib/esm/index.mjs": "./lib/esm/index.browser.mjs"
17
+ },
18
+ "exports": {
19
+ "./package.json": "./package.json",
20
+ ".": {
21
+ "import": "./lib/esm/index.mjs",
22
+ "require": "./lib/esm/index.cjs"
23
+ }
24
+ },
25
+ "files": [
26
+ "lib",
27
+ "LICENSE"
28
+ ],
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "scripts": {
33
+ "clean": "shx rm -rf lib/*",
34
+ "build": "yarn clean && rollup --config ../../rollup.config.ts --configPlugin rollup-plugin-ts",
35
+ "build:watch": "yarn clean && rollup --config ../../rollup.config.ts --configPlugin rollup-plugin-ts --watch",
36
+ "postbuild": "echo '{\"type\":\"commonjs\"}' | npx json > lib/cjs/package.json && echo '{\"type\":\"module\"} ' | npx json > lib/esm/package.json",
37
+ "prepublishOnly": "agadoo"
38
+ },
39
+ "peerDependencies": {
40
+ "@solana/web3.js": "^1.48.0"
41
+ },
42
+ "dependencies": {
43
+ "@solana-mobile/mobile-wallet-adapter-protocol": "^0.0.1-alpha.1"
44
+ },
45
+ "devDependencies": {
46
+ "agadoo": "^2.0.0"
47
+ },
48
+ "gitHead": "9af0298849fd011355f15ad4391063de343094ec"
49
+ }